GridBagLayout: Setting the position of components - java

I read some questions which stated that GBL is better than the normal GridLayout for positioning the components.
Links:
first , second, Oracle Tutorial
Well so I tried to add some components (buttons) to my GBL but it somehow always centers my components. The only way to seperate the 2 Buttons is to place Dummies into my project which looks awful.
public class GridGUI extends JPanel {
private JButton button;
public GridGUI() {
setLayout(new GridBagLayout());
GridBagConstraints comps = makeGbc(0,1);
JPanel panel = new JPanel();
button = new JButton("Datei speichern");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
panel.add(button);
add(panel,comps);
comps = makeGbc(1,1);
button = new JButton("Datei laden");
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(new JPanel());
panel.add(button);
add(panel,comps);
}
private GridBagConstraints makeGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = x;
gbc.weighty = 1.0;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = (x == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
gbc.fill = GridBagConstraints.HORIZONTAL;
return gbc;
}
public static void showGUI() {
JFrame frame = new JFrame("Pop-Art-Collage");
frame.getContentPane().add(new GridGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
showGUI();
}
});
}
}
But I want them to appear at the top of the GUI and not in the center.
Also I tried to resize the buttons by using button.setSize(x,y) but they always stayed the same.
Thanks in advance
Edit:
Okay I tried now the solution I got but I got into some other problems.
GridBagConstraints comps = makeGbc(0,1,20,50,0,0);
button = new JButton("Datei speichern");
button.setPreferredSize(new Dimension(200,25));
add(button,comps);
JButton button2 = new JButton("Datei laden");
button2.setPreferredSize(new Dimension(100,25));
comps = makeGbc(1,1,20,150,0,100);
add(button2,comps);
Here is my problem that I set the prefered size for both buttons the same but it still doesnt show correctly for the second one.
(the 4 Numbers after x,y of makeGbc are the Insets values top,left,bottom,right)

Your buttons are in the middle, not on the top, because you place it there:
gbc.anchor = (x == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
GridBagConstraints.LINE_END and GridBagConstraints.LINE_START puts components in the centere. Try with:
gbc.anchor = (x == 0) ? GridBagConstraints.FIRST_LINE_START : GridBagConstraints.FIRST_LINE_END;
Also:
try to set size of buttons with setPreferredSize(), GBL with frame.pack() should respect choosen sizes,
you add your buttons to new JPanels, and then to container with GBL, so it place your buttons in center. Add your buttons directly to GridGUI, and it will be placed according to GridBagConstraints(), for example:
button = new JButton("Datei laden");
add(button,makeGbc(1,1));
you use one class field JButton button, for two different buttons, this way you are not able to change first button state, after you change button reference - if you try to change for example text in button, from different part of application, by using button class field you can access only second button.
EDIT
I am not sure what effect you exactly want, but I would try to add your GridGUI first to new JPanel, then to JFrame (in main method):
JFrame frame = new JFrame("Pop-Art-Collage");
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //add FlowLayout.LEFT,
panel.add(new GridGUI());
frame.getContentPane().add(panel);
Now, the FlowLayout should keep all components to left, and you can control the space between buttons by changing insets. For first button, a would set all inputs to 0, and in second one, the new Insets(0,x,0,0), where x is desired distance. Like this:
add(button,makeGbc(0,1,0,0,0,0));
add(button2,makeGbc(0,1,0,150,0,0));
But I am not sure, how this set of layouts will behave when you add more components, so the easier way, is to use only frame.pack(), without frame.setSize(), and then extend it by adding new components. Then it should be easier to predict how changes will work.
Also pay attention to a fact, that in a example code above, you set different setPreferredSize() for both buttons.

Related

Java Swing trying to add a stationary JButton over a JScrollPane

I am trying to make a text editor with a button that appears at the bottom right of the editor regardless if you scroll up or down and appears over the text area
import javax.swing.*;
import java.awt.*;
public class Problem{
public static void main(String[] args){
//Setting up the frame
JFrame window = new JFrame();
window.setSize(600, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Making the LayeredPane
JLayeredPane LP = new JLayeredPane();
LP.setLayout(new BorderLayout());
//Making the ScrollPane and JTextArea
JTextArea textArea = new JTextArea(100,50);
textArea.setText("Test Text");
JScrollPane back = new JScrollPane();
back.setViewportView(textArea);
//Making the panel that appears in the front of the text
JPanel front = new JPanel();
front.setLayout(null);
front.setBackground(new Color(0,0,0,0));
front.setOpaque(false);
JButton button = new JButton("test");
button.setBounds(200,200,50,20);
front.add(button);
LP.add(back,BorderLayout.CENTER);
LP.setLayer(back,0,0);
LP.add(front,BorderLayout.CENTER);
LP.setLayer(front,1,0);
window.add(LP);
window.setVisible(true);
}
}
I am seeing just the JButton with a white background, if I don't add the second layer "front" I see my back JScrollPane with the JTextArea
Caveat
I'm not a fan of this is idea. It's not a "common" UX concept that many desktop users would be presented with and there are a number of, arguably, better solutions which leverage the pre-existing experience of users.
This requires some "hacking" to get to work, so, there's no guarantee that it will work on all platforms or continue to work into the future.
Why doesn't it work?
This is a rather technical question which delves deep into the core of how Swing, and in particular, the JScrollPane work. Let's just say, I don't have the time or desire to dig into, but I know the JScrollPane is heavy optimised, which may be affecting the way in which anything which overlays it gets updated - or it could just be the way that the painting system works.
Runnable example...
This takes the idea by camickr (all credit to him), but instead of using a OverlayLayout, makes use of a GridBagLayout to position the button. Why? Because the GridBagLayout gives me more control over the position of the button - it's a personal thing.
import java.awt.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(40, 40);
try (Reader reader = new InputStreamReader(getClass().getResourceAsStream("/resources/StarWarsNewHope.txt"))) {
textArea.read(reader, "A long list");
} catch (IOException exp) {
exp.printStackTrace();
}
JButton button = new JButton("Am I in your way yet");
JPanel contentPane = new JPanel() {
#Override
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
// Change this to reposition the button some where else
gbc.anchor = GridBagConstraints.FIRST_LINE_END;
gbc.insets = new Insets(32, 32, 32, 32);
gbc.ipadx = 16;
gbc.ipady = 16;
contentPane.add(button, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane, gbc);
add(contentPane);
}
}
}
You should probably also look at How to Use Scroll Panes and the section on Providing Custom Decorations for some alternatives
Swing is designed/optimized to display/paint components in 2 dimensions. The vast majority of layout managers will make sure that the components don't overlap.
This means that you can't use a layout manager on your layered pane (if you want the components to overlap). Instead, you must manually set the size/location of components on each layer.
When you use a JLayeredPane the painting of components on each layer is managed so that the higher layer is painted last.
So your code might be changed to something like:
import java.awt.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setSize(600, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Making the LayeredPane
JLayeredPane LP = new JLayeredPane();
//Making the ScrollPane and JTextArea
JTextArea textArea = new JTextArea(20,40);
textArea.setText("Test Text");
textArea.setSize( textArea.getPreferredSize() );
JScrollPane back = new JScrollPane( textArea);
back.setSize( textArea.getSize() );
JButton button = new JButton("test");
button.setBounds(200,200,50,20);
LP.add(back, new Integer(0));
LP.add(button, new Integer(1));
window.add(LP);
window.setVisible(true);
}
}
There is one layout manager in the JDK, the OverlayLayout, which is designed to stack components on top of one another. However, even this layout manager does not paint components properly when the components overlap. The trick when using this layout manager is to override the isOptimizedDrawing() method of the panel using the layout manager to make sure all components are repainted all the time. In this case, make sure the bottom panel is always painted before the top panel.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Main2
{
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel()
{
#Override
public boolean isOptimizedDrawingEnabled()
{
return false;
}
};
contentPane.setLayout( new OverlayLayout(contentPane) );
JPanel top = new JPanel( new GridBagLayout() );
top.setBorder( new EmptyBorder(0, 0, 16, 16) );
top.setOpaque(false);
top.setAlignmentX(1.0f);
top.setAlignmentY(1.0f);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.LAST_LINE_END;
JButton button = new JButton("test");
top.add(button, gbc);
contentPane.add(top);
//Making the ScrollPane and JTextArea
JTextArea textArea = new JTextArea(10,25);
textArea.setText("Test Text");
JScrollPane back = new JScrollPane( textArea);
back.setAlignmentX(1.0f);
back.setAlignmentY(1.0f);
contentPane.add(back);
window.add(contentPane);
window.pack();
window.setVisible(true);
}
}
The benefit of this approach is that the button will move as the frame is resized.
However, as a user I would still get annoyed with a button appearing over top of the text in my text area.
Edit:
If you really need components to overlap then I would suggest you could:
look at MadProgrammers solution to use a GridBagLayout. This approach gives far more control over the alignment of the components
check out the Overlap Layout which also provides more flexibility when aligning overlapping components
It should be noted that both above approaches may still require you to override the isOptimizedDrawEnabled(...) method to make sure components are painted properly. I am not aware of any layout manager the allows you to overlap components and works without this override.

How to stop JTextField and JTextLabel from truncating on JPanel

I have JTextFields and JLabels added to my JPanel (from left to right) every time the JButton is pressed. However, every new JTextField and JLabelthat is added becomes smaller and smaller. How do I fix this?
Also I would like to add a JScrollPane to the JPanel but having problems doing so.
public class MyExample
{
// Field members
static JPanel panel = new JPanel();
static Integer indexer = 1;
static List<JLabel> listOfLabels = new ArrayList<JLabel>();
static List<JTextField> listOfTextFields = new ArrayList<JTextField>();
static JScrollPane scrollPane = new JScrollPane( panel );
public static void main(String[] args)
{
// Construct frame
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
frame.setPreferredSize(new Dimension(2000, 2000));
frame.setTitle("My Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollPane);
// Frame constraints
GridBagConstraints frameConstraints = new GridBagConstraints();
// Construct button
JButton addButton = new JButton("Add");
addButton.addActionListener(new ButtonListener());
// Add button to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 0;
frame.add(addButton, frameConstraints);
// Construct panel
panel.setPreferredSize(new Dimension(1000, 1000));
panel.setLayout(new GridBagLayout());
panel.setBorder(LineBorder.createBlackLineBorder());
// Add panel to frame
frameConstraints.gridx = 0;
frameConstraints.gridy = 1;
frameConstraints.weighty = 1;
frame.add(panel, frameConstraints);
// Pack frame
frame.pack();
// Make frame visible
frame.setVisible(true);
}
static class ButtonListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent arg0)
{
// Clear panel
panel.removeAll();
// Create label and text field
JTextField jTextField = new JTextField();
jTextField.setSize(100, 200);
listOfTextFields.add(jTextField);
listOfLabels.add(new JLabel("Label " + indexer));
// Create constraints
GridBagConstraints textFieldConstraints = new GridBagConstraints();
GridBagConstraints labelConstraints = new GridBagConstraints();
// Add labels and text fields
for(int i = 0; i < indexer; i++)
{
// Text field constraints
textFieldConstraints.gridx = i;
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
textFieldConstraints.weightx = 0.5;
textFieldConstraints.insets = new Insets(10, 10, 10, 10);
textFieldConstraints.gridy = 1;
// Label constraints
labelConstraints.gridx = i;
labelConstraints.gridy = 0;
labelConstraints.insets = new Insets(10, 10, 10, 10);
// Add them to panel
panel.add(listOfLabels.get(i), labelConstraints);
panel.add(listOfTextFields.get(i), textFieldConstraints);
}
// Align components
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = indexer;
c.weighty = 1;
panel.add(new JLabel(), c);
// Increment indexer
indexer++;
panel.updateUI();
}
}
}
However, every new JTextField and JLabelthat is added becomes smaller and smaller. How do I fix this?
panel.setPreferredSize(new Dimension(1000, 1000));
Don't set the preferred size. If the size is fixed then as you add more components they need to shrink to fit in the allowed space.
Don't attempt to set the size of any component. Let the layout manager do its job and determine the preferred size of the panel.
When creating a JTextField the code should be something like:
//JTextField jTextField = new JTextField();
JTextField jTextField = new JTextField(10);
This will allow the text field to determine its own preferred size to dispaly about 10 characters.
panel.updateUI();
Don't use updateUI(). That is used internally by Swing when you change the LAF. When you remove/add components you should be using:
panel.revalidate();
panel.repaint();
As for the JScrollPane with panel as a viewport, you should not be adding the panel to the frame at all - setting it as the viewport (like you're doing in the JScrollPane constructor) and adding the JScrollPane is sufficient. Adding the panel itself may be the cause of your problem.
As for the shrinking problem, I am still trying to understand your layout code - your use of GridBagLayout seems a tad overcomplicated to me. Maybe you can draw a simple sketch of how you would like the layout look?

Adding multiple JButtons moves other JButtons (Java)

The problem I've been having for a while is that whenever I add a JButton to a JPanel, any other JButton that I had on was shifted in that direction.
Here is the first code:
//imports
import java.awt.*;
import javax.swing.*;
public class Example {
public static void main(String[] args) {
// Create the frame and panel and the Grid Bag Constraints
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Create ONE JButton
JButton button1 = new JButton("Button1");
// Set the frame's properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.setVisible(true);
// Set Basic Grid Bag Constraints settings.
c.gridx = 0;
c.gridy = 0;
// Set the Insets
c.insets = new Insets(0, 0, 0, 0);
// Add the Grid Bag Constraints and button1 the panel
panel.add(button1, c);
}
}
Everything seems to working right?
Well if we add a second button:
//imports
import java.awt.*;
import javax.swing.*;
public class Example {
public static void main(String[] args) {
// Create the frame and panel and the Grid Bag Constraints
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Create TWO JButtons
JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
// Set the frame's properties
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.setVisible(true);
// Set Basic Grid Bag Constraints settings.
c.gridx = 0;
c.gridy = 0;
// Set the Insets
c.insets = new Insets(0, 0, 0, 0);
// Add the Grid Bag Constraints and button1 the panel
panel.add(button1, c);
// Set the Insets
c.insets = new Insets(500, 0, 0, 0);
// Add the Grid Bag Constraints and button2 the panel
panel.add(button2, c);
}
}
Then button1 moves down towards button2. Does anyone know why and/or a fix for it?
EDIT: What I am asking, is how do you add another button without moving the other buttons.
I don't know what your intent is. You only state it doesn't do what you expect. So I can't give you an exact solution.
In any case start by reading the section from the Swing tutorial on How to Use GridBagLayout for working examples using a GridBagLayout.
I see two issues:
You never change the gridx/y values. Both components are added to grid (0, 0) which is not how the GridBagLayout should work. Every component needs to be added to a different cell.
The Insets value of (500, ....) seems very big.

GridBagLayout compnent's position not working

Hi i am beginner to java and doing small GUI using GridBagLayout. See the attached code and also the output . what i want is to place the JButtons on top left corner as per the position assigned in gridx and gridy . But it placing components in center instead of top left as expected , if i use Insets , gridx /gridy all that is working but not from proper coordinates so please see attached code and image and guide me about it
public rect()
{
JPanel panel = new JPanel( new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JButton nb1= new JButton("Button1 ");
JButton nb2= new JButton("Button2 ");
gbc.gridx=0;
gbc.gridy=0 ;
panel.add(nb1, gbc);
gbc.gridx=1;
gbc.gridy=1;
panel.add(nb2, gbc);
panel.setVisible(true);
JFrame frame = new JFrame("Address Book ");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300 );
frame.add(panel);
frame.setVisible(true);
}
OUTPUT : want these buttons on top left please guide me
I think the problem is more the use of setSize(..), you should rather use appropriate LayoutManager and call pack() on JFrame instance after adding all components to JFrame and before setting JFrame visible, also no need for panel.setVisible(..):
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JPanel panel = new JPanel(new GridBagLayout());
JButton nb1 = new JButton("Button1 ");
JButton nb2 = new JButton("Button2 ");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(nb1, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(nb2, gbc);
JFrame frame = new JFrame("Address Book ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}

BoxLayout misunderstanding strut

I'm prgramming a simple input diagram in Swing. I use boxLayout to create a simple GUI of user input. Problem is that creating a horizontal strut between the JPanel of all the labels and the JPanel of the JTextFields causes the whole panel to shift downwards (weird) this is the whole panel:
private JPanel secondCard() {
//main panel. set the boxlayout
secondCard = new JPanel();
secondCard.setLayout(new BoxLayout(secondCard,BoxLayout.Y_AXIS));
// create vertical strut for looks
secondCard.add(Box.createVerticalStrut(20));
// create title. center it.
JLabel title = new JLabel("Configure main network parameters ");
title.setAlignmentX(CENTER_ALIGNMENT);
secondCard.add(title);
// create vertical strut for looks
secondCard.add(Box.createVerticalStrut(20));
// create panel for the description labels
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel,BoxLayout.Y_AXIS));
labelPanel.setAlignmentX(LEFT_ALIGNMENT);
JLabel inPut =new JLabel("number of inputs");
inPut.setAlignmentX(LEFT_ALIGNMENT);
labelPanel.add(inPut);
inPut =new JLabel("number of outputs");
inPut.setAlignmentX(LEFT_ALIGNMENT);
labelPanel.add(inPut);
inPut =new JLabel("number of layers");
inPut.setAlignmentX(LEFT_ALIGNMENT);
labelPanel.add(inPut);
JPanel textFieldPanel = new JPanel();
textFieldPanel.setLayout(new BoxLayout(textFieldPanel,BoxLayout.Y_AXIS));
textFieldPanel.setAlignmentX(LEFT_ALIGNMENT);
JTextField inputTextField = new JTextField();
inputTextField.setAlignmentX(LEFT_ALIGNMENT);
textFieldPanel.add(inputTextField);
inputTextField.setMinimumSize(new Dimension(0,0));
inputTextField = new JTextField();
inputTextField.setAlignmentX(LEFT_ALIGNMENT);
textFieldPanel.add(inputTextField);
inputTextField.setMinimumSize(new Dimension(0,0));
inputTextField = new JTextField();
inputTextField.setAlignmentX(LEFT_ALIGNMENT);
textFieldPanel.add(inputTextField);
inputTextField.setMinimumSize(new Dimension(0,0));
textFieldPanel.setMaximumSize(new Dimension(50, labelPanel.getMaximumSize().height));
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel,BoxLayout.X_AXIS));
inputPanel.setAlignmentX(CENTER_ALIGNMENT);
inputPanel.add(labelPanel);
//this is the problem strut!! it causes inputPanel to shift downwards
inputPanel.add(Box.createHorizontalStrut(20));
inputPanel.add(textFieldPanel);
secondCard.add(inputPanel);
return secondCard;
}
without the strut it looks like:
With strut it looks like (I know I suck at picture editing):
You are adding a Box strut to a BoxLayout.
As the javadoc states, createHorizontalStrut(int width):
Creates an invisible, fixed-width component. In a horizontal box, you
typically use this method to force a certain amount of space between
two components. In a vertical box, you might use this method to force
the box to be at least the specified width. The invisible component
has no height unless excess space is available, in which case it takes
its share of available space, just like any other component that has
no maximum height.
As such, it is filling the height between your title JLabel and the bottom of the JPanel.
You might want to consider using Box.createRigidArea(new Dimension(20, height)) instead, where height could be specified or set to the height of labelPanel.
Or, you could reconsider the layout for your JPanel - take a look at the visual guide.
For future reference, if you cannot make sense of your Swing layout, try putting adding a coloured LineBorder to the JComponents you're unsure of. In this case, the Box struts are not JComponents but Components, so you'd have to put them into a JPanel, but this would at least have shown you what space each component was taking up in your top-level JPanel.
use Cardlayout for wizard logics
put JLabel(Configure ...., JLabel.CENTER) to the BorderLayout.NORTH
put JPanel with JButtons to the BorderLayout.SOUTH
put JPanel with SpringLayout, GridLayout, or GridBagLayout to the BorderLayout.CENTER
Top-Level Container have got implemened BorderLayout by default, then there no reason to re_define BorderLayout
above mentioned steps are called NestedLayout
alternative are put all JComponents by using GridBagLayout, SpringLayout or todays MigLayout to the one JPanel, but why bothering
Example of a nested layout, one using BorderLayout, FlowLayout (JPanel's default), and GridBagLayout:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class LayoutFoo {
private static final String TITLE = "Configure Main Foobar Parameters";
private static final String[] LABEL_TEXTS = {
"Number of Spams", "Number of Frapzats", "Number of Zignuts"
};
private static final int TEXTFIELD_SIZE = 10;
private static final Insets WEST_INSETS = new Insets(5, 5, 5, 10);
private static final Insets EAST_INSETS = new Insets(5, 10, 5, 5);
private static final int EB_GAP = 5;
private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();
public JPanel getConfigFooPanel() {
JPanel textFieldPanel = new JPanel(new GridBagLayout());
for (int i = 0; i < LABEL_TEXTS.length; i++) {
addTextAndField(textFieldPanel, LABEL_TEXTS[i], i);
}
int blVertGap = 20;
JPanel borderLayoutPanel = new JPanel(new BorderLayout(0, blVertGap));
borderLayoutPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP,
EB_GAP, EB_GAP));
JLabel titleLabel = new JLabel(TITLE, JLabel.CENTER);
borderLayoutPanel.add(titleLabel, BorderLayout.PAGE_START);
borderLayoutPanel.add(textFieldPanel, BorderLayout.CENTER);
JPanel outerWrapperFlowPanel = new JPanel();
outerWrapperFlowPanel.add(borderLayoutPanel);
return outerWrapperFlowPanel;
}
public String getFieldText(String labelText) {
JTextField field = textFieldMap.get(labelText);
if (field == null) {
return ""; // ?? throw exception
} else {
return field.getText();
}
}
private void addTextAndField(JPanel panel, String text, int i) {
JLabel label = new JLabel(text, JLabel.LEFT);
JTextField textField = new JTextField(TEXTFIELD_SIZE);
textFieldMap.put(text, textField);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = i;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = WEST_INSETS;
panel.add(label, gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = EAST_INSETS;
panel.add(textField, gbc);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("LayoutFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LayoutFoo().getConfigFooPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

Categories