Results 1 to 4 of 4

Thread: Positioning components?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Positioning components?

    Im trying to create and layout controls
    dynamically but i can't figure out how
    to set the x and y alignments.
    I tried using setalignmentX and using
    setPosition(Point); Any ideas?
    Code:
      import java.awt.*; 
      import javax.swing.*;
      import java.awt.event.*; 
    
    
       public class Layout{
         public static void main(String [] args){
       
         int width = 50;
         int space = 10;
         float x = 0; 
         float y = 0;
         JFrame jf = new JFrame("Hello");
         Dimension d; 
         Container c = jf.getContentPane();
         c.setLayout(null);
         jf.addWindowListener(new WindowMonitor()); 
         jf.setSize(600,450); 
         jf.setVisible(true);   
    
         JButton[] jb = new JButton[20];
         for(int i = 0; i < jb.length; ++i){
           jb[i] = new JButton(new Integer(i).toString());
           d = new Dimension(50,50);
           jb[i].setSize(d);
            
        }
          for(int i = 0; i < jb.length; ++i){
         
           if(x > jf.getWidth() - (width + space)){
            x = 10;
            y = 60;
           jb.setalignmentX = x;     
           jb.setalignmentY = y;       
    
           c.add(jb[i]); 
           }else{x += (width + space);
               jb.setalignmentX = x;     
                c.add(jb[i]); 
         }
        }
       }
      }
    
     class WindowMonitor extends WindowAdapter {
         public void windowClosing(WindowEvent we){
           System.exit(0);
        } 
       }

  2. #2
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    have you tried setBounds?

    I'm quite interested in this as I haven't had a chance of playing with the layout manager set to null.

    DO keep us updated!
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  3. #3
    Lively Member
    Join Date
    Feb 2001
    Location
    Jutland, Denmark
    Posts
    71
    Here you hav an example:
    jScrollPane2.setVerticalScrollBarPolicy
    (javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane2.setBorder(BorderFactory.createTitledBorder("Tekst"));
    jScrollPane2.setBounds(new Rectangle(0, 108, 634, 156));
    jScrollPane2.getViewport().add(jTextArea2, null);
    this.getContentPane().add(jScrollPane2, null);
    jTextArea2.setWrapStyleWord(true);
    jTextArea2.setLineWrap(true);
    jTextArea2.setBorder(BorderFactory.createLineBorder(Color.black, 1));

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Thanks guys The methods worked good. I have
    the JFrame set to a certain width which gives the
    right most JButtons a nice space from the edge of
    the frame. But i have to work on the line
    " if(x > (jf.getWidth() - (width + space))){"
    a bit so i can have consistent space from the
    edge of the frame regardless of the JFrame size.
    Code:
     
      import java.awt.*; 
      import javax.swing.*;
      import java.awt.event.*; 
    
    
       public class Layout{
         public static void main(String [] args){
         
         int height  = 50;   
         int width = 50;
         int space = 10;
         int x = 0; 
         int y = 10;
         JFrame jf = new JFrame("Hello");
         
         Container c = jf.getContentPane();
         c.setLayout(null);
         jf.addWindowListener(new WindowMonitor()); 
         jf.setSize(560,400);
         jf.setResizable(false); 
         jf.setVisible(true);   
    
         JButton[] jb = new JButton[54];
         for(int i = 0; i < jb.length; ++i){
           jb[i] = new JButton(new Integer(i).toString());
           jb[i].setSize(width, height);
            
        }
          for(int i = 0; i < jb.length; ++i){
    
          if(x == 0){
             x = 10;
             jb[i].setBounds(new Rectangle(x, y, width, height));
             c.add(jb[i]);
             continue; 
          }
            x += (width + space);
            jb[i].setBounds(new Rectangle(x, y, width, height));      
            c.add(jb[i]); 
     
            if(x > (jf.getWidth() - (width + space))){
             x = 10;
             y += (height + space);
             jb[i].setBounds(new Rectangle(x, y, width, height));      
             c.add(jb[i]);
           } 
         }
        }
       }
      
    
     class WindowMonitor extends WindowAdapter {
         public void windowClosing(WindowEvent we){
           System.exit(0);
        } 
       }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width