I have some questions on positioning components and some questions on text fields and text areas (Java Swing). Any help is greatly appreciated.
Right now I am trying to have two text fields beside each other with a different label above each describing what that text field does. To achieve this I have placed them in a GridLayout(2, 2).
Is this the best way? It is the only way I know to have a label directly over another component. Is there a better way? What about if there is just one label above one button. Is it sensible to position this through a GridLayout(2, 1)? I am visually impaired so I do not think positioning buttons just by their pixel position is an option unless there is a simple way to place components at a relative number of pixels to another component.
That leads me to my next question.
What is the best way to have the same UI as above but with another component (button) centered under it. Essentially the UI should compose of two Named text fields with a calculate button under. The way I did this is by putting the above components in a panel, and adding that plus the calculate button to a surrounding panel with a GridLayout(2, 1). The problem is that the button becomes as big as the panel above it (I'm assuming). How can I adjust this and still have the button perfectly aligned under the panel of text fields/labels? Similarly with labels above text areas. The label should be small but have a larger space for the text area under.
(text field):
Again referring to the UI above, if the user types many characters into the first text field, will the letters go over the text field on the right? If so how can I prevent this?
If I append text to a text area and it is already full, will it automatically allow the user to scroll? If not what is a simple way to make the text area scrollable?
Right now I am not setting a size of the text area. Does it just grow as I add text? Does it have a default size as in number of characters?
There are a number of layout managers that might be capable of providing you with what you need.
MigLayout
JGoodies FormLayout
GridBagLayout
For, GridBagLayout would be my choice (I'm biased, as I've been using this layout manager for the past 12 years ;))
public class TestLayout17 {
public static void main(String[] args) {
new TestLayout17();
}
public TestLayout17() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JLabel("Label 1"), gbc);
gbc.gridx++;
add(new JLabel("Label 2"), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JTextField(10), gbc);
gbc.gridx++;
add(new JTextField(10), gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 2;
add(new JButton("Click"), gbc);
}
}
}
I also agree with Eng.Fouad's suggestion of using compound containers to make your life easier in the long run
You might find Laying Out Components Within a Container a worth while read.
Right now I am trying to have two text fields beside each other with a
different label above each describing what that textfield does. To
achieve this I have placed them in a GridLayout(2, 2). Is this the
best way? It is the only way I know to have a label directly over
another component. Is there a better way? What about if there is just
one label above one button. Is it sensible to position this through a
GridLayout(2, 1)?
Myself, I always do it via nested panels with BorderLayout. For example:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
Note that, you can manipulate the gaps between the components with setting empty borders. Also, you may use BorderLayout.LINE_START and BorderLayout.LINE_END instead of using BorderLayout.WEST and BorderLayout.EAST, and this will add support for RTL languages (e.g Arabic).
That leads me to my next question. What is the best way to have the
same UI as above but with another component (button) centred under it.
Essentially the UI should compose of two Named text fields with a
calculate button under. The way I did this is by putting the above
components in a panel, and adding that plus the calculate button to a
surrounding panel with a GridLayout(2, 1). The problem is that the
button becomes as big as the panel above it (I'm assuming). How can I
adjust this and still have the button perfectly aligned under the
panel of textfields/labels?
I would do it via nested panels as I did earlier, but now the bottom panel has a FlowLayout layout manager to get a good size for the button:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
panOuter.add(panLeft, BorderLayout.WEST);
panOuter.add(panRight, BorderLayout.EAST);
panOuter.add(panBottom, BorderLayout.SOUTH);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
Similarly with labels above text areas. The label should be small but
have a larger space for the text area under.
I would suggest you to use TitledBorder:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panConsole = new JPanel(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("The Console");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
JTextArea txtConsole = new JTextArea(5, 10);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
panConsole.add(txtConsole, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
third (text field): Again referring to the UI above, if the user types
many characters into the first text field, will the letters go over
the text field on the right? If so how can I prevent this?
Try the above code, and see how it acts :)
Fourth: If I append text to a text area and it is already full, will
it automatically allow the user to scroll? If not what is a simple way
to make the text area scrollable?
You need to use something called JScrollPane:
JFrame frame = new JFrame("The Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel(new BorderLayout());
JPanel panLeft = new JPanel(new BorderLayout());
panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panRight = new JPanel(new BorderLayout());
panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panBottom = new JPanel(); // default is FlowLayout
panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panInput = new JPanel(new BorderLayout());
panInput.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panConsole = new JPanel(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createTitledBorder("The Console");
Border theBorder = BorderFactory.createCompoundBorder(outsideBorder, insideBorder);
panConsole.setBorder(theBorder);
panInput.add(panLeft, BorderLayout.WEST);
panInput.add(panRight, BorderLayout.EAST);
panInput.add(panBottom, BorderLayout.SOUTH);
panOuter.add(panInput, BorderLayout.NORTH);
panOuter.add(panConsole, BorderLayout.CENTER);
JLabel lblLeft = new JLabel("Label 1", JLabel.CENTER);
JLabel lblRight = new JLabel("Label 2", JLabel.CENTER);
JTextField txtLeft = new JTextField(10);
JTextField txtLright = new JTextField(10);
JButton btnBottom = new JButton("Press it!");
JTextArea txtConsole = new JTextArea(5, 10);
JScrollPane srcPane = new JScrollPane(txtConsole,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panLeft.add(lblLeft, BorderLayout.NORTH);
panLeft.add(txtLeft, BorderLayout.CENTER);
panRight.add(lblRight, BorderLayout.NORTH);
panRight.add(txtLright, BorderLayout.CENTER);
panBottom.add(btnBottom);
panConsole.add(srcPane, BorderLayout.CENTER);
frame.setContentPane(panOuter);
frame.pack();
frame.setVisible(true);
I hope I answered all of your questions :)
Related
I am not sure how I could create a panel like this. I could have the main panel as a borderlayout and set the login screen panel to the page_end but then the forums and faqs also have to be on the page_end..... somehow the login screen panel and the forums and faqs panel has to share the page_end together. Is there someway I could do this or maybe some BETTER way? This has been confusing me for about 2 hours and I don't understand how I would do this.
Right now I have 3 panels and 1 frame. 1 is the main panel that is added to the main frame. The 2 other panels are the loginscreen panel and the forums and faqs panel. Here is the code.
private void createView() {
//Created essential details for the frame
JFrame frame = new JFrame();
frame.setTitle("Name of the game");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Defining layouts and panels + giving them layouts
JPanel pMain = new JPanel();
frame.getContentPane().add(pMain);
pMain.setLayout(new BorderLayout());
JPanel pLogin = new JPanel(new GridBagLayout());
pMain.add(pLogin, BorderLayout.PAGE_END);
JPanel pInfo = new JPanel(new GridBagLayout());
pMain.add(pInfo, BorderLayout.PAGE_END);
frame.setVisible(true);
}
Here is the component layout
Source
JFrame frame = new JFrame();
frame.setTitle("Name of the game");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Defining layouts and panels + giving them layouts
JPanel pMain = new JPanel();
frame.getContentPane().add(pMain);
pMain.setLayout(new BorderLayout());
JPanel bottomComponentsPanel = new JPanel(new GridBagLayout());
JPanel pLogin = new JPanel();
pLogin.setBackground(Color.ORANGE);
pLogin.setPreferredSize(new Dimension(100, 100));
JPanel pInfo = new JPanel(new GridBagLayout());
pInfo.setBackground(Color.ORANGE);
pInfo.setPreferredSize(new Dimension(70, 70));
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.gridx = 0;
constraints.gridy = 0;
bottomComponentsPanel.add(pLogin, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
bottomComponentsPanel.add(pInfo, constraints);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(bottomComponentsPanel);
pMain.add(bottomPanel, BorderLayout.SOUTH);
frame.setVisible(true);
Display
I am trying to design a layout which contains a form and couple of items. but I found it too hard to put items in right places.
In the following image, the right frame is what I am aiming to design and the left on is what I could made.
And this is the code for the right frame:
public class GUI extends JFrame{
public GUI(){
JFrame frame = new JFrame("frame");
frame.setSize(600, 600);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Title"), BorderLayout.NORTH);
JPanel formPanel = new JPanel(new GridLayout(1,2));
panel.add(formPanel);
TitledBorder formPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
formPanel.setBorder(formPanelTitle);
//LEFT PANEL
JPanel labelsPanel = new JPanel(new GridLayout(4,1));
TitledBorder labelsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
labelsPanel.setBorder(labelsPanelTitle);
labelsPanel.add(new JLabel("Label 1"));
labelsPanel.add(new JLabel("Label 2"));
labelsPanel.add(new JLabel("Label 3"));
labelsPanel.add(new JLabel("Label 4"));
formPanel.add(labelsPanel);
//RIGHT PANEL
JPanel fieldsPanel = new JPanel(new GridLayout(4,1));
TitledBorder fieldsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
fieldsPanel.setBorder(fieldsPanelTitle);
fieldsPanel.add(new JTextField("Label 1"));
fieldsPanel.add(new JTextField("Label 2"));
fieldsPanel.add(new JTextField("Label 3"));
fieldsPanel.add(new JTextField("Label 4"));
formPanel.add(fieldsPanel);
//BOTTOM PANEL
JPanel bottomPanel = new JPanel(new GridLayout(2,1));
TitledBorder BottomPanelTitle = BorderFactory.createTitledBorder("GridLayout(2,1)");
bottomPanel.setBorder(BottomPanelTitle);
panel.add(bottomPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JButton("Browse"));
buttonPanel.add(new JLabel("Label"));
TitledBorder buttonPanelTitle = BorderFactory.createTitledBorder("FlowLayout()");
buttonPanel.setBorder(buttonPanelTitle);
bottomPanel.add(buttonPanel);
JPanel secondButtonPanel = new JPanel(new GridLayout(1,2));
secondButtonPanel.add(new JButton("Back"));
secondButtonPanel.add(new JButton("Next"));
TitledBorder secondButtonPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
secondButtonPanel.setBorder(secondButtonPanelTitle);
bottomPanel.add(secondButtonPanel);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new GUI();
}
}
I am not sure if the code is really optimal, since there are a lot of inner panels and made it too complicated. Also I could not put items in the places I wanted to. Is there any suggestion or idea to make this layout look better?
Create a JPanel, using GridBagLayout and add your labels/fields to it, this forms the "center" portion of your layout.
Create a JPanel and add the Browse button a JLabel to it. Using GridBagConstraints#gridwidth set to REMAINDER, add this to your first panel
Create a JPanel, using BorderLayout, add the first panel to the CENTER position. Add the title Label to the NORTH position, you may need to adjust it's horizontalAlignment property
Create a JPanel using FlowLayout, aligned to the RIGHT and add your "Back" and "Next" buttons to it. Add this to the SOUTH position of the previous panel.
Check out Laying Out Components Within a Container for more details
I've been at this for a good while now, but I cannot seem to get a hand of it. I'm trying to produce a JPanel that has a JTextArea above and two JLabels below, but my JLabel ends up on the left side of my JTextArea and I cannot make the other appear.
Here's my code (sorry for the display stuff- just filler really):
public JPanel contentPane() {
JPanel something = new JPanel();
String information = "Please";
info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}
Please and thank you to anyone who takes the time to help.
When you create you something, you don't specify any layout manager, but later on you attempt to add one to something using BorderLayout constants -- which will not work, since the default layout manager for a JPanel is FlowLayout.
Try this instead;
JPanel something = new JPanel(new BorderLayout());
I need to add JLabel object to specific component of my panel. I use setLabelFor method but this one adds label nex to component. How to change it to set label on top of component?
TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleField);
sampleLabel.setLabelFor(sampleField);
panel.add(sampleLabel);
^ this one puts sampleLabel next to sampleField. How to put sampleLabel on top of sampleField?
Thanks from advance.
#Edit:
I use something like this:
public TabBody()
{
setLayout(new BorderLayout());
nameField = new TextField();
//nameField.setSize(new Dimension(200, 200));
//revalidate();
nameLabel = new JLabel("name test");
amountLabel = new JLabel("amount test");
amountField = new TextField();
unitsBox = new JComboBox(units);
unitsBox.setSelectedIndex(3);
nameLabel.setLabelFor(nameField);
amountLabel.setLabelFor(amountField);
add(nameLabel, BorderLayout.NORTH);
add(amountLabel, BorderLayout.NORTH);
add(nameField);
add(amountField);
add(unitsBox);
}
and outcome is:
And I need something like this:
Your JPanel use FlowLayout by default, that place component one by one in a row. Because of you have that effect.
You need to use a proper LayoutManager for example use BorderLayout :
import java.awt.BorderLayout;
import java.awt.TextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Example {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleField);
sampleLabel.setLabelFor(sampleField);
panel.add(sampleLabel,BorderLayout.NORTH);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
If you need to place components in grid, one by one for example try to use GridBagLayout
JPanel panel = new JPanel(new GridBagLayout());
JTextField sampleField = new JTextField(5);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleLabel,c);
c.gridy++;
panel.add(sampleField,c);
c.gridy++;
panel.add(new JLabel("sample text 2"),c);
c.gridy++;
panel.add(new JTextField(5),c);
c.gridy++;
panel.add(new JTextField(5),c);
manage positions with gridx and gridy properties of GridBagConstraints
.
You must use Layout for arranging your components. For JPanel by deafult it is FlowLayout which arranges your component next to one-another. You can use GridLayout or GridBagLayout
Here is the sample code:
TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.setLayout(new GridBagLayout());
sampleLabel.setLabelFor(sampleField);
GridBagConstraints = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
panel.add(sampleLabel, gbc);
gbc.gridY=1;
panel.add(sampleField, gbc);
Here's what I actually want to put on a panel:
First logical block:
radio button 1 text field icon button
radio button 2 text field icon button
check box
Second logical block:
Label Spinner
Button
My first decision is to make Vertical Box Layout and put there two Horizontal Box Layouts - for each logical block. But the problem is with these blocks, what layouts to choose to describe this structure? I dislike GridBagLayout - it is very composite and difficult to understand, especially when code isn't yours. For the moment I see that Flow Layout and Grid Layout can be used. But Grid Layout, for example, stretches buttons to the width of a cell and if a button is with icon only it, it looks very strange then.
Hope you can advise me something.
For the first case you can use a simple GridLayout on the JPanel with 3 Rows each having a separate JPanel with FlowLayout having constraints, FLowLayout.LEFT. Have a look at this code example :
import java.awt.*;
import javax.swing.*;
public class ExampleLayout
{
private void displayGUI()
{
JFrame frame = new JFrame("Example Layout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 1, 5, 5));
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
JTextField tfield1 = new JTextField(10);
JButton button1 = new JButton("Button 1");
topPanel.add(rbut1);
topPanel.add(tfield1);
topPanel.add(button1);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JRadioButton rbut2 = new JRadioButton("RadioButton 2", false);
JTextField tfield2 = new JTextField(10);
JButton button2 = new JButton("Button 2");
middlePanel.add(rbut2);
middlePanel.add(tfield2);
middlePanel.add(button2);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JCheckBox cbox = new JCheckBox("CheckBox 1", false);
bottomPanel.add(cbox);
contentPane.add(topPanel);
contentPane.add(middlePanel);
contentPane.add(bottomPanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ExampleLayout().displayGUI();
}
});
}
}
OUTPUT :
And for the Second case, simply add first two components to the JPanel having default Layout. And for the third components, simply add components on to a JPanel having GridBagLayout, with no constraints.
EDIT #1 :
Or you can use this approach, for your second block.
import java.awt.*;
import javax.swing.*;
public class ExampleLayout
{
private void displayGUI()
{
JFrame frame = new JFrame("Example Layout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JPanel basePanel = new JPanel();
basePanel.setLayout(new GridLayout(0, 1, 5, 5));
JPanel topPanel = new JPanel();
//topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JLabel label1 = new JLabel("Label 1", JLabel.CENTER);
JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
topPanel.add(label1);
topPanel.add(rbut1);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new GridBagLayout());
JButton button1 = new JButton("Button 1");
middlePanel.add(button1);
basePanel.add(topPanel);
basePanel.add(middlePanel);
contentPane.add(basePanel, BorderLayout.PAGE_START);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ExampleLayout().displayGUI();
}
});
}
}