Components in Gui, how to place?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Components in Gui, how to place?

  1. #1
    Join Date
    Sep 2004
    Posts
    59

    Components in Gui, how to place?

    Hello,

    I have a problem placing a label above a textArea, i tried allot of things but i dont know how to do it, the current gui places the label next to the text area and not above.


    code:


    public Container createContentPane() {
    //Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();

    //Create a scrolled text area.
    output = new JTextArea("",5, 30);

    output.setEditable(false);
    sp1 = new JScrollPane(output);

    //Create a scrolled text area.
    char_info = new JTextArea("Character info",30, 15);
    char_info.setEditable(false);
    sp2 = new JScrollPane(char_info);

    //Add the text area to the content pane.
    contentPane.add(sp1, BorderLayout.CENTER);

    contentPane.add(p1, BorderLayout.EAST);
    p1.add(info, BorderLayout.NORTH);
    p1.add(sp2, BorderLayout.SOUTH);

    contentPane.add(p2, BorderLayout.SOUTH);


    return contentPane;
    }
    My own java video tutorials, feel free to watch it.
    http://www.engineeringserver.com/for...amming-b311.0/ Need java help? For beginning and intermediate java coders!

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    try using more then one JPanel.
    To make this work you'll prolly need 3 JPanels, all with different layouts.
    The top panel will have a border layout. On this you place the two other panels. One containing the label and one containing the text area. The panel containing the label will be placed on the root panel to the NORTH. The text are will be located in the CENTER.
    This way, the text area will encompass the remaining area of the root panel.

    Now you can place the label to the left of the top panel easy.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If you don't care about resizing, e.g. the components will stay in the same place
    with the same size when the container (frame) is resized, you could specify a null
    layout and use the setBounds(x,y,width,height) method for each component you
    add to the container (xy-layout).
    eschew obfuscation

  4. #4
    Join Date
    Jul 2005
    Posts
    22
    Honestly, BorderLayout()s are great when you are just starting out and using multiple panels or only want to have components ordered is a simplistic manner. On the other hand, I have found them to be very much a pain. Although I'm sure that I am just missing some capable layout that will do what I want, here is my solution (it is basically the same as the above post):

    Code:
    // import statements here
    
    public class someClass extends JFrame {
        private JTextField tField;
        private JButton b1, b2;
    
        public someClass() {
            //code to initiate frame and other vars needed
            createComponents();
            this.show();
        }
    
        public void createComponents() {
            Container c = getContentPane();
            c.setLayout(null);     //key code, a null layout, this means that you will
                                         //expected to set (by pixel) the location and dimensions
                                         //of each component.
            tField = new JTextField("Some initial text");
            tField.setBounds(10,10,300,20);  //seting tField by (x,y,width,height)
            b1 = new JButton("Button Text");
            b1.setBounds(10,40,135,20);      //set by same values
            b2 = new JButton("Button Text2");
            b2.setBounds(155,40,135,20);    //set by same values
            c.add(tField, null);
            c.add(b1, null);
            c.add(b2, null);
        }
    } // class someClass
    I hope this was helpful, and if I missed any important code, or you would like to to elaborate, please feel free to comment.
    Last edited by Zodoz; 07-25-2005 at 11:50 PM. Reason: c.add(component); might not have worked right.

Similar Threads

  1. Gui Freeze after re-connect
    By lstwil in forum Java
    Replies: 0
    Last Post: 05-29-2005, 09:54 AM
  2. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 07:53 AM
  3. Beta - Mabry .Net Internet Components
    By Zane Thomas in forum dotnet.announcements
    Replies: 0
    Last Post: 10-17-2001, 11:51 PM
  4. Internet Components - Beta
    By Zane Thomas in forum .NET
    Replies: 0
    Last Post: 10-17-2001, 03:37 AM
  5. Replies: 3
    Last Post: 03-17-2001, 04:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center