2

I have this layout that I need to program for an assignment and this is the first time I have used layout managers in the GUI so I'm having problems getting the layout to match. I need your help

I Have two tabs labeled Account creation and Account transfer (those are ok) what I need is to have the JLabel (Account ID) and the first text field in one line then the next JLabel (Amount) and text field in the line under the first. Under that needs to be the JButton Centered (Create an Account). Lastly the JTextArea (No account) needs to be in a column to the right in the empty space separate from the labels, text fields, and button.

enter image description here

Here is the code I have started with:

public CreatePanel(Vector accountList, TransferPanel transferPanel)
{
this.accountList = accountList;
this.transferPanel = transferPanel;

JLabel l1 = new JLabel("Account ID");
JTextField t1 = new JTextField();
JLabel l2 = new JLabel("Amount");
JTextField t2 = new JTextField();
JButton b1 = new JButton("Create an Account");
JTextArea a1 = new JTextArea("No Account");

JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,3));
panel1.add(l1);
panel1.add(t1);
panel1.add(l2);
panel1.add(t2);
panel1.add(b1, BorderLayout.SOUTH);
b1.setVerticalAlignment(JLabel.CENTER);


JPanel panel2 = new JPanel();
panel2.add(a1);
a1.setSize(200, 300);

add(panel1);
add(panel2);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
tyler_7
  • 63
  • 1
  • 9
  • Can you upload the images of what you want and what you get to another site, then link the image URL in your post. Someone will may edit them into your post for you – Java Devil Oct 01 '13 at 20:00
  • *"I can not load an image.."* Use [ASCII art](http://en.wikipedia.org/wiki/ASCII_art). – Andrew Thompson Oct 01 '13 at 20:02
  • try this URL http://s768.photobucket.com/user/knightredin/media/Untitled_zpsa80a08bc.png.html – tyler_7 Oct 01 '13 at 20:05
  • @JavaDevil http://s768.photobucket.com/user/knightredin/media/Untitled_zpsa80a08bc.png.html – tyler_7 Oct 01 '13 at 20:07
  • 1
    OK I'm confused. Is that screenshot what the assignment specifies to do, or is it what you see so far? BTW - Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Oct 01 '13 at 20:11
  • I'm thinking GridBagLayout – MadProgrammer Oct 01 '13 at 20:12
  • @AndrewThompson this is what the assignment specifies to do. And the assignment is to run the class in an applet. – tyler_7 Oct 01 '13 at 20:32
  • 1
    Note that you can nest multiple layouts as shown in [this excellent example](http://stackoverflow.com/a/5630271/1076463) – Robin Oct 01 '13 at 21:22

2 Answers2

3

This is how I would approach it. Instead of adding the outer panel to a frame though, it would be added to a tab of the tabbed pane.

Assignment 6 screenshot

The above is an example of a nested or compound layout. The titled borders show the layouts used and the arguments (if any) used to construct them.

The size of the button is suggested by the content (the text). The sizes of the text fields and text area is suggested in the constructor (which itself has been included as the text value).

To get the TALL effect in the text fields, set an HUGE font but use less columns for the constructor.

See also

Another nested layout.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1, I liked this idea of specifying the parameters in the TitledBorder :-) – nIcE cOw Oct 02 '13 at 14:11
  • 1
    @nIcEcOw I was going to post source code (without titled borders), but given the nature of the post (learning how to use layouts) I felt a description would be more optimal. And 'a picture (of a layout) paints a thousand words (when the layouts are detailed in a titled border)'.. ;) – Andrew Thompson Oct 02 '13 at 14:15
  • This is perfect, exactly what I need and I do appreciate not just putting source code in because naturally I would of just copied and pasted the code and not truly learned the material. I will get started on this now. – tyler_7 Oct 02 '13 at 22:51
  • *"I do appreciate not just putting source code.."* You just made my day. +1 for your attitude - I think you'll go far (and be well regarded when you get there). – Andrew Thompson Oct 02 '13 at 23:10
2

GridBagLayout is the most powerful Layout that you can use to easily implement grid-like displays. It's a layout with n rows and m columns where each cell is customizable independently of the others in several aspects. In this layout you have to attach a GridBagConstraints object to each panel.add(JComponent, Constraints) as constraints. In the tutorial it's clearly specified what is customizable. It might look a little harsh at the beginning but once you get the hang of it it's great. It's powerful and flexible and you don't have to worry about the uncustomizable restrictions you might encounter with other Layouts.

In your layout, the most inconvenient thing I see is having the "Account ID" label aligned by its center with the TextField AND with the empty space over both of them. It would be easier if the label was aligned with the bottom of the TextField. To solve this I'll assume that the Label and the TextField are inside a panel I constructed beforehand that aligns each other correctly (easily with BorderLayout or GridBagLayout... or anything really) and I'll just place the panels in the Layout.

Then I see this Layout as a GridBagLayout with 3 rows and 2 columns that looks like this:

GridBagLayout rows and columns

This is an overview of how I'd put the constraints to specify each component in the layout.

Panel 1 (Account ID Label + TextField)

gridx = 0
gridy = 0
weighty = 0.5
weightx = 0.5
anchor = PAGE_END
fill = HORIZONTAL

Panel 2 (Amount Label + TextField)

gridx = 0
gridy = 1
weighty = 0.0
fill = HORIZONTAL

Button

gridx = 0
gridy = 2
anchor = PAGE_START
weighty = 0.5

TextArea

gridx = 1
gridy = 0
gridheight = 3
weightx = 0.5
fill = BOTH

There is a couple of details I overlooked but the core issues can be aproached with these constraints. The least obvious thing to learn about the GridBagLayout is how do weights work in complex cases, for example what happens when there are several different weightx values in the same column. Does it count the max? or the sum?...

For the sake of discussion, you could avoid having those panels using an extra initial row with an invisible component with weighty > 0 and then having 2 columns: one for the JLabels and the other one for the JTextFields, with the apropiate anchors; the button would have gridwith = 2... but that's totally unnecesary, go for the two auxiliar panels.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
DSquare
  • 2,458
  • 17
  • 19
  • Thank you for your response I have a working model now and I do like how @andrewthompson set up his model – tyler_7 Oct 02 '13 at 22:49