Results 1 to 7 of 7

Thread: JFrame Layouts

  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default JFrame Layouts

    I always struggle with the layouts of my JFrame. I usually refer to this, which is a great reference, but nonetheless I find it difficult to control the overall layouts.

    I have one panel defined as a GridLayout(0,4). This displays a single row of one label and three radio buttons. Below this, I'd like to add a TextArea component that will hold a description depending on which radio button is selected. Two things:

    1. I'd like to include this in the panel I mentioned, is it possible to have one row with four columns, and another with one? So the TextArea would take up the whole second row?
    2. Generally speaking - does the above sound like it would be difficult? Or would the listeners notice the change of selection right away to adjust the text in the TextArea?

    Thanks much.

  2. #2
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: JFrame Layouts

    Start with a JPanel set to grid layout of (0, 2). To this, make a new panel and add the label and the ButtonGroup that holds the JRadioButtons. Add this to the first panels first row panel.add(thatPanelIJustDescribed("1"). Then you will make a TextArea component and add this to the panel with the grid layouts second row.
    As for the actionListener for changing the String you would have to make the actionListener of the JRadioButton have access to the instance of TextArea that you are inserting to the JFrame. This actionListener can be the same for all of the radio Buttons. You could extend actionListener and have that class hold an instance variable of String. Then in actionPerformed you just myTextArea.setText(instanceVariableString).

    Hope that was mostly clear and what you were asking. Good luck.

    EDIT: You could also use Action instead of ActionListener (basically a class that extends ActionListener but also has a putValue method and getValue method). Also I suggest looking up Grid Bag Layout despite your design not being so complex so you can know what to fall back to if your designs get very involved. You might also want to consider Tooltips supported by every JComponent.
    Last edited by typedef; 02-17-2014 at 01:51 AM.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: JFrame Layouts

    Tempdef -

    Thank you, that was very clear! I believe I understood it correctly, however the .add() results in a red underline recommending I create a method(string).

    I'll look around on the internet a bit using that concept and see if I can find something. In the meantime, here is my code:

    Java Code:
    		// Populate Holding Panel
    		jpHoldingPanel.setBorder(emptyBorder);
    		jpHoldingPanel.add(lblRoom);
    		jpHoldingPanel.add(jrbSuite);	// Radio buttons
    		jpHoldingPanel.add(jrbQueen);
    		jpHoldingPanel.add(jrbStandard);
    		jrbStandard.setSelected(true);	// Default
    		
    		// Populate Room Panel
    		jpRoomPanel.setBorder(emptyBorder);
    		jpRoomPanel.add(jpHoldingPanel("1"));
    		jpRoomPanel.add(jtaRoomDescriptions("2"));
    		jtaRoomDescriptions.disable();
    EDIT: jpRoomPanel.add(jpHoldingPanel, 1); seems to be the correct syntax. At firs I was getting an illegal placement error, changed the positions to 0 & 1 and it's correct. Going to play around now to add a border so it doesn't look so funny! Thank you very much.
    Last edited by mallorz; 02-17-2014 at 02:38 AM.

  4. #4
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: JFrame Layouts

    Cool remember to keep the instance of the text area visible. One thing that you might find interesting to look into for events (such as ActionListener) is inner classes (specifically anonymous inner classes). This will allow you to more closely relate the components.

  5. #5
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: JFrame Layouts

    I set the text area so it was not editable. Worked out quite nice thus far for the most part. The one issue is that I think it made both rows the same width as the text area, so there is a large gap between the room choices and the description box. Is there a way to change this? I did a quick search and someone else mentioned the Grid Bag Layout as you did but they made it sound difficult to use.

  6. #6
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: JFrame Layouts

    After thinking about this a bit, I might as well just break it up into the two separate panels. Why make this more difficult for myself?! I was simply trying to cut down on creating panels because that's primarily how I organize these things (JFrames).

  7. #7
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: JFrame Layouts

    I'm a bit new myself but I think your on the right track (not sure as I'm not implementing this myself). Label to top panel, buttons to a inner panel, and finally add that inner panel to the outer panel. If not try sizing the TextArea to your liking for adjustment. Since JPanel is a FlowLayout it will all be spaced equally in the end.
    As for the thing about Grid Bag Layout, yes its difficult to use. You should consider writing a wrapper for GridBagConstraints to make it easier. In the book I'm learning from Core Java Volume I they make a wrapper called GDC so you don't have to set all the settings as GridBagConstraintsName.Field. You can just use a default constructor or a method that returns the GDC so you can link multiple set methods for GridBagConstraint fields. (....GENIUS.... lol)

Similar Threads

  1. java layouts
    By Diriglontz in forum New To Java
    Replies: 5
    Last Post: 01-09-2014, 10:22 AM
  2. Layouts
    By tnrh1 in forum AWT / Swing
    Replies: 3
    Last Post: 12-06-2011, 09:00 PM
  3. Swing layouts
    By jayragz in forum AWT / Swing
    Replies: 2
    Last Post: 07-08-2011, 12:10 PM
  4. problems with layouts !
    By ron2794 in forum NetBeans
    Replies: 1
    Last Post: 12-15-2010, 02:23 AM
  5. Layouts ?
    By plodos in forum Eclipse
    Replies: 3
    Last Post: 07-30-2007, 04:03 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
  •