I am trying to make a GUI but I can't get the layout setup properly
-
I'll suggest you to set bounds to each component. It will give a desired look to your application. – Prasad Oct 04 '13 at 07:42
-
I have not learned bounds yet in my class. – tur sander Oct 04 '13 at 07:44
-
I don't suggest to use bounds more than necessary – RamonBoza Oct 04 '13 at 07:56
-
@tursander : What exactly do you want to change in the output (as shown in the image) ? If you just wanted it to look a bit more nice, then I can provide an example code that you can have a look at and ask questions anytime. You can use `GridBagLayout` for this sort of a thingy. Ahha, the answer is very much given already :-) – nIcE cOw Oct 04 '13 at 13:42
3 Answers
This can be done using a compound or nested layout consisting of a BorderLayout
, with a FlowLayout
in the PAGE_START
constraint, and a GridLayout
for the two text areas in the CENTER
constraint.
Something like this:
OTOH, you might swap out the FlowLayout
for a JToolBar
(looks nicer), and the GridLayout
for a JSplitPane
(more usable, since the panes can be set to whatever size the user needs at that moment).
Given the example picture (..paints a thousand words) now in the question, it seems clear the top area consisting of 4 rows of label, text field, button, button would be best done in a GroupLayout
. Either that or 3 GridLayout
instances (one each for the labels, fields & buttons), in the LINE_START
, CENTER
and LINE_END
of another BorderLayout
.
Here is an example of the latter:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class ToolBarAnd2AreasLayout {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new TitledBorder("BorderLayout()"));
JPanel controls = new JPanel(new BorderLayout(4,4));
controls.setBorder(new TitledBorder("BorderLayout(4,4)"));
JPanel labels = new JPanel(new GridLayout(0,1));
labels.setBorder(new TitledBorder("GridLayout(0,1)"));
controls.add(labels, BorderLayout.LINE_START);
JPanel fields = new JPanel(new GridLayout(0,1));
fields.setBorder(new TitledBorder("GridLayout(0,1)"));
controls.add(fields, BorderLayout.CENTER);
JPanel buttons = new JPanel(new GridLayout(0,2,2,2));
buttons.setBorder(new TitledBorder("GridLayout(0,2,2,2)"));
controls.add(buttons, BorderLayout.LINE_END);
for (int ii=0; ii<4; ii++) {
labels.add(new JLabel("Label " + (ii+1)));
fields.add(new JTextField(5));
buttons.add(new JButton("Button " + ((ii*2) + 1)));
buttons.add(new JButton("Button " + ((ii*2) + 2)));
}
gui.add(controls, BorderLayout.PAGE_START);
JPanel input = new JPanel(new GridLayout(0,1,2,2));
input.setBorder(new TitledBorder(
"GridLayout(0,1,2,2)"));
for (int ii=0; ii<2; ii++) {
input.add(new JScrollPane(new JTextArea(5,35)));
}
gui.add(input, BorderLayout.CENTER);
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}

- 168,117
- 40
- 217
- 433
-
I have tried doing this but it isn't working out for me. I have added the newest code I have right now. I basically just need the JLabels and JTextFields to match up better like yours. – tur sander Oct 04 '13 at 15:32
-
Had a quick look at your code and it seems you went 'one `GridLayout` too far'. I was going to look at fixing it, but ..got lazy. Instead I've added the code used for the 2nd screenshot as an edit to the answer. See how you go with that. – Andrew Thompson Oct 04 '13 at 15:39
-
Updated. I am not stuck on the button. I can't add the f.code into the main method because it doesn't recognize the f.add(gui). I am confused on this. I have tried moving it down into the main and everything. The only way it works is the way it is now. Unless I am missing something. – tur sander Oct 04 '13 at 16:23
-
I'm not sure what most of that is supposed to mean, given your latest code compiles cleanly and displays much as mine did. What's the problem? – Andrew Thompson Oct 04 '13 at 16:29
first of all you need to define the layout for the main frame.
try adding this line at the top of your Vigenere constructor
public Vigenere() {
setLayout(new GridLayout(2, 1));
JPanel topPanel = new JPanel(new GridLayout(1, 2));
JPanel p1 = new JPanel(new GridLayout(5, 9));
p1.add(new JLabel("Source File"));
p1.add(jtfSourceFile);
p1.add(new JLabel("Results File"));
p1.add(jtfResultsFile);
p1.add(new JLabel("Key Code"));
p1.add(jtfKeyCode);
p1.add(new JLabel("Compare"));
p1.add(jtfCompare);
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtOpen);
p2.add(jbtSave);
p2.add(jbtKey);
p2.add(jbtCompare);
p2.add(jbtEncrypt);
p2.add(jbtDecrypt);
p2.add(jbtClear);
p2.add(jbtQuit);
topPanel.add(p1);
topPanel.add(p2);
JPanel p3 = new JPanel(new GridLayout(2, 1));
jtfSource.setBorder(BorderFactory.createLineBorder(Color.black));
jtfResults.setBorder(BorderFactory.createLineBorder(Color.black));
p3.add(jtfSource);
p3.add(jtfResults);
add(topPanel);
//add(p2);
add(p3);
pack();
}
and continue from there ;)

- 8,898
- 6
- 36
- 48
-
I just updated the code. I am not getting all my panels in the frame but they are not displaying how they should. – tur sander Oct 04 '13 at 07:43
-
can you do a wireframe with gimp, or paint, to show how exactly you want it? – RamonBoza Oct 04 '13 at 07:56
-
http://i.imgur.com/tT300Xb.png Here is the layout I am trying to go for. – tur sander Oct 04 '13 at 07:59
-
@tursander edited, it seems similiar you just need to touch on bounds and sized now ;) – RamonBoza Oct 04 '13 at 08:03
-
-
not exactly, but now he has to play with which layout to use in each panel to expand correctly. bounds would be used to have the same size on text areas for example, and that information will only be set on LookDefaults. – RamonBoza Oct 04 '13 at 08:22
Try using GridBagLayout with GridBagConstraints. When you know to use it, is the only you need: http://www-mips.unice.fr/Doc/Java/Tutorial/uiswing/layout/gridbagExample.html

- 1,757
- 10
- 11