I'm trying to figure out the best way to align 2 sets of items in the center of a panel in a Java Swing application. The panel is in the North position of a BorderLayout whose width is determined by the JTextField in the Center position of the layout. The problem I'm having is, I have a set of labels and smaller text fields that I want to center so that the end of the label and the start of the first text field meet at the center of the panel.
I've tried GroupLayout, but ended up with the following result:
Note: The 2 text fields separated by a + are in a sub-panel.
What I'm trying to achieve is the following:
Apparently I'm either missing something, or this is far more complicated than necessary to do. I actually run into this issue a LOT! I'm surprised there isn't a special grid layout specifically for this.
Trying to do this with a GridLayout resulted in this:
So... what IS the easiest way to get the layout I'm looking for (second image)?
GroupLayout example code below:
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
frame.setContentPane(panel);
JPanel longText = new JPanel();
JPanel shortText = new JPanel();
JPanel mediumText = new JPanel();
longText.add(new TextField(5));
longText.add(new JLabel("+"));
longText.add(new TextField(5));
shortText.add(new TextField(5));
shortText.add(new JLabel("+"));
shortText.add(new TextField(5));
mediumText.add(new TextField(5));
mediumText.add(new JLabel("+"));
mediumText.add(new TextField(5));
JLabel lExample = new JLabel("Long text example:");
JLabel sExample = new JLabel("Short:");
JLabel mExample = new JLabel("Medium Example:");
JPanel subPanel = new JPanel();
GroupLayout layout = new GroupLayout(subPanel);
subPanel.setLayout(layout);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.CENTER)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(lExample)
.addComponent(sExample)
.addComponent(mExample))
.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(longText)
.addComponent(shortText)
.addComponent(mediumText))))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(lExample)
.addComponent(longText))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(sExample)
.addComponent(shortText))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(mExample).addComponent(mediumText))
);
JTextArea textArea = new JTextArea() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600,300);
}
};
textArea.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setAutoscrolls(true);
panel.add(subPanel,BorderLayout.NORTH);
panel.add(textArea,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
Consider using a GridBagLayout, as it gives you a lot more control over the placement of individual components and respects the preferred size of the components where it can (unless you override them through the use of the GridBagConstraints)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LayoutExample {
public static void main(String[] args) {
new LayoutExample();
}
public LayoutExample() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JLabel longText = new JLabel("Long Text Example");
JLabel shortText = new JLabel("Short Example");
JLabel medText = new JLabel("Medium Example");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(longText, gbc);
addFields(gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.EAST;
add(shortText, gbc);
addFields(gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.EAST;
add(medText, gbc);
addFields(gbc);
}
protected void addFields(GridBagConstraints gbc) {
JTextField field1 = new JTextField("0", 5);
field1.setEnabled(false);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx++;
add(field1, gbc);
gbc.gridx++;
gbc.insets = new Insets(0, 4, 0, 4);
add(new JLabel("+"), gbc);
JTextField field2 = new JTextField(5);
gbc.gridx++;
add(field2, gbc);
}
}
}
Related
everyone. I would like to have custom JPanels ( I prefer absolute position layout for my use) in GridLayout. GridLayout is in ScrollPane.
public class App extends JFrame {
public App() {
super("bamlOperator");
JPanel panel = new JPanel(new GridLayout(0, 1));
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(new MyCustomPanel());
panel.add(new MyCustomPanel());
panel.add(new MyCustomPanel());
panel.add(new MyCustomPanel());
panel.add(new MyCustomPanel());
panel.add(new MyCustomPanel());
pack();
setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
And MyPanel :
public class MyCustomPanel extends JPanel {
private JLabel aaa = new JLabel("aaa:");
private JLabel bbb = new JLabel("bbb");
private JLabel ccc = new JLabel("ccc:");
public MyCustomPanel() {
setPreferredSize(new Dimension(100,100));
JPanel amlPanel = new JPanel();
amlPanel.setLayout(null);
amlPanel.setBounds(0,0,100,100);
aaa.setBounds(10,20,30,40);
amlPanel.add(aaa);
bbb.setBounds(20,30,40,50);
amlPanel.add(bbb);
ccc.setBounds(30,40,50,60);
amlPanel.add(ccc);
add(amlPanel);
}
}
But it doesnt work.
I said, I prefer absolute position layout but I know It is bad practice. I can use another but I need JPanel something like this :
Project of JPanel
So, your fundamental problem is you're mixing absolute layouts with layout managers - the problem is MyCustomPanel isn't providing any sizing hints which the layout manager can use to make better decisions about how best to layout your component. So, if you really want to use absolute layouts, you're going to have to do ALL the work that the layout management API would have done for you
Can you tell me which layout manager will be the best for my use?
All of them. Don't get fixated on a single layout manager achieving everything your want, instead, combine them together to produce the results you're after.
I don't have your "exact" requirements, but I was able to achieve this by using BorderLayout and GridBagLayout
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
// You could use a GridLayout, but GridBagLayout will
// honour the preferred sizs of each component
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
add(new LeftPane(), gbc);
add(new MiddleLeftPane(), gbc);
add(new MiddlePane(), gbc);
add(new RightPane(), gbc);
}
}
public class LeftPane extends JPanel {
public LeftPane() {
setLayout(new GridBagLayout());
JPanel main = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
for (int index = 0; index < 6; index++) {
if (index % 2 == 0) {
gbc.anchor = GridBagConstraints.LINE_START;
} else {
gbc.anchor = GridBagConstraints.LINE_END;
}
main.add(new JLabel("Label"), gbc);
}
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(main, gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
add(new JButton("Button"));
}
}
public class MiddleLeftPane extends JPanel {
public MiddleLeftPane() {
setLayout(new BorderLayout());
BufferedImage img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.drawLine(0, 0, 200, 200);
g2d.drawLine(200, 0, 0, 200);
g2d.dispose();
JLabel label = new JLabel(new ImageIcon(img));
label.setBorder(new LineBorder(Color.GRAY));
add(label);
}
}
public class RightPane extends JPanel {
public RightPane() {
setLayout(new BorderLayout());
BufferedImage img = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.drawLine(0, 0, 200, 200);
g2d.drawLine(200, 0, 0, 200);
g2d.dispose();
JLabel label = new JLabel(new ImageIcon(img));
label.setBorder(new LineBorder(Color.GRAY));
add(label);
}
}
public class MiddlePane extends JPanel {
public MiddlePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
add(new JButton("Button"), gbc);
gbc.gridx++;
add(new JButton("Button"), gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 2;
add(new JButton("Button"), gbc);
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(new JTextArea("Text Area", 5, 10)), gbc);
}
}
}
You are using a null layout for layout of your custom panel. When you add this inside another panel of grid layout, since the sizes are not set, it would not be painted. Try using a proper layout for your custom panel as well.
And, see the answer for Using a JPanel with a null layout .
I am in the beginning process of coding a matrix project for my coding class. I am currently just trying to get the GUI looking the way I want it. Here is a quick sketch of how I want it to look:
https://drive.google.com/file/d/1Hmt-9DNH6CGtV1eBJYSOu72Iu2cAqPe8/view?usp=sharing
(The pic is too big to upload so linking the Drive was the next best option.)
Basically, The user would enter in the rows and columns of their 2 matrices, and press the button to calculate. The program would ensure the 2 matrices were compatible, prompt for specific numbers, and then the final calculations would appear in the labels.
The issue is I'm still pretty inexperienced in Java, and this is my first time working with more than 1 JPanel. Here is the code I've written for my Gui so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class GuiWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame myGUI = new JFrame();
myGUI.setTitle("Matrix Project");
myGUI.setSize(600,600);
myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1); //borders
JLabel AddLabel = new JLabel(); //creates JLabels
AddLabel.setBorder(border);
JLabel SubLabel = new JLabel();
SubLabel.setBorder(border);
JLabel MultLabel = new JLabel();
MultLabel.setBorder(border);
JPanel main = new JPanel(); //Adds JLabel panel
myGUI.add(main);
main.setLayout(new GridLayout(1, 3));
main.setAlignmentY(Component.TOP_ALIGNMENT);
main.add(MultLabel);
main.add(SubLabel);
main.add(AddLabel);
JTextField row1txt = new JTextField(); //textfields to input rows and columns
row1txt.setText("Matrix 1 rows...");
JTextField column1txt = new JTextField();
column1txt.setText("Matrix 1 columns...");
JTextField row2txt = new JTextField();
row2txt.setText("Matrix 2 rows...");
JTextField column2txt = new JTextField();
column2txt.setText("Matrix 2 columns...");
JPanel input = new JPanel(); //adds textfield panel
myGUI.add(input);
input.setLayout(new GridLayout (2, 2, 3, 3));
input.add(row1txt);
input.add(column1txt);
input.add(row2txt);
input.add(column2txt);
JButton Calc = new JButton(); //button to eventually calculate
Calc.setText("Calculate");
JPanel CalcButton = new JPanel(); //adds button panel
myGUI.add(CalcButton);
CalcButton.add(Calc);
myGUI.setVisible(true); //instantiates the gui
}
}
When I load the code, All I see is the button. This makes sense. I have nothing splitting up the 3 JPanels into different sections, so Java is just sort of laying them on top of each other. So here, of course, is the question: How would you split these Jpanels up into separate sections of the JFrame so it could look at least sort of like my sketch?
Any help is appreciated. Thanks!
The first thing you want to do is have a read through Laying Out Components Within a Container.
You could probably do this using compounding GridLayouts, but to my money, GridBagLayout offers more flexibility.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.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 GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.weighty = 0.5;
add(new TopPane(), gbc);
gbc.weightx = 0.5;
gbc.gridy++;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
add(new CheckerPane(), gbc);
gbc.gridx++;
add(new JButton("Button"), gbc);
}
}
public class TopPane extends JPanel {
public TopPane() {
setLayout(new GridLayout(0, 3));
add(new JLabel("label1", JLabel.CENTER));
add(new JLabel("label2", JLabel.CENTER));
add(new JLabel("label3", JLabel.CENTER));
}
}
public class CheckerPane extends JPanel {
public CheckerPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new JCheckBox("Tick box 1"), gbc);
gbc.gridx++;
add(new JCheckBox("Tick box 2"), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JCheckBox("Tick box 3"), gbc);
gbc.gridx++;
add(new JCheckBox("Tick box 4"), gbc);
}
}
}
I am new to swing and trying to make a simple quiz game just for practice reasons. What I want to make is like this:
This was my first implementation but there I extends JFrame and make all of this in one frame then i realize i need to use cardlayout to change from the main menu to the playing scene so i split it to 1 class main frame and other classes for the mainmenu, play scene, game over scene, etc. So my MainMenu now extends JPanel and when I add buttons to the button panel and then add buttonpanel to the mainpanel i got this:
Its like the panel containing label of the main menu and the button panel are next to each other but i need them like in the first picture. Here is my MainMenu class:
public class MainMenu extends JPanel{
private JLabel menuTitle;
private JPanel menuTitlePanel;
private JPanel buttonPanel;
public MainMenu(){
this.setBackground(new Color(0, 128, 43));
//ADDING THE TITLE
menuTitle = new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>");
menuTitle.setForeground(Color.BLACK);
menuTitlePanel = new JPanel(new GridBagLayout());
menuTitlePanel.setBackground(new Color(0, 128, 43));
GridBagConstraints gbcTitle = new GridBagConstraints();
gbcTitle.weightx = 0;
gbcTitle.weighty = 0;
gbcTitle.gridx = 0;
gbcTitle.gridy = 0;
gbcTitle.gridwidth = 3;
gbcTitle.insets = new Insets(70, 0, 0, 0);
menuTitlePanel.add(menuTitle, gbcTitle);
this.add(menuTitlePanel, BorderLayout.NORTH);
buttonPanel = new JPanel(new GridBagLayout());
buttonPanel.setBackground(new Color(0, 128, 43));
JButton startButton = new JButton("Start");
GridBagConstraints gbcStart = new GridBagConstraints();
gbcStart.gridx = 1;
gbcStart.gridy = 1;
gbcStart.ipadx = 50;
gbcStart.ipady = 10;
gbcStart.gridwidth = 3;
gbcStart.insets = new Insets(10, 0, 0, 0);
buttonPanel.add(startButton, gbcStart);
this.add(buttonPanel, BorderLayout.CENTER);
}
}
Any help will be appreciated.
So, you basically have two basic groups, the title and the buttons, these two need to be managed individually, as they have different layout requirements (primarily, the buttons been laid out in the middle).
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class TestMenu {
public static void main(String[] args) {
new TestMenu();
}
public TestMenu() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new MenuPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MenuPane extends JPanel {
public MenuPane() {
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;
add(new JLabel("<html><h1><strong><i>Krisko Beatz Quiz</i></strong></h1><hr></html>"), gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPanel buttons = new JPanel(new GridBagLayout());
buttons.add(new JButton("Start"), gbc);
buttons.add(new JButton("Show scores"), gbc);
buttons.add(new JButton("Help"), gbc);
buttons.add(new JButton("Exit"), gbc);
gbc.weighty = 1;
add(buttons, gbc);
}
}
}
That should help with the current question, but you'll probably also want to have a look at How to use CardLayout for details about how you could switch between different views
I see you haven't set a layout for MainMenu, perhaps try Boxlayout and see if it offers the result you are after:
public MainMenu(){
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
...
this.add(menuTitlePanel, Component.CENTER_ALIGNMENT);
...
this.add(buttonPanel, Component.CENTER_ALIGNMENT);
...
}
I am having to migrate from Python TKinter to Java Swing due to a change of jobs.
The problem I have is:
How do I position a panel containing objects such as labels and text fields in the top left hand corner of JTabbedFrame.
If the frame itself is bigger than the panel , the JPanel is centering by default on the page.
Here is my code so far:
public class GUI {
//Constructor
GUI(){
//Craete a new frame
//borderlayout
JFrame jfrm = new JFrame("Tabbed Demo");
//Intialize size
jfrm.setSize(500, 250);
//Terminate program
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create Tabbed Frame
JTabbedPane jtp = new JTabbedPane(JTabbedPane.TOP);
//Create a panel to place on tabbed page
JPanel jpnl = new JPanel();
jpnl.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5,5,5,5);
jpnl.setOpaque(true);
jpnl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//create a 3 labels horizontally
gbc.gridx=1;
gbc.gridy=0;
JLabel title= new JLabel(" Tracking System ");
title.setFont(new Font("Serif", Font.PLAIN, 40));
jpnl.add(title,gbc);
gbc.gridx=0;
gbc.gridy=1;
JLabel subtitle= new JLabel("First");
subtitle.setFont(new Font("Serif", Font.PLAIN, 18));
jpnl.add(subtitle,gbc);
gbc.gridx=1;
gbc.gridy=1;
JTextArea firstText = new JTextArea("Type Here!");
firstText.setFont(new Font("Serif", Font.PLAIN, 18));
jpnl.add(firstText,gbc);
gbc.gridx=0;
gbc.gridy=2;
JLabel subtitle2= new JLabel("Last");
jpnl.add(subtitle2,gbc);
subtitle2.setFont(new Font("Serif", Font.PLAIN, 18));
//add to Tab
jtp.addTab("Demo", jpnl);
//make visible
jfrm.getContentPane().add(jtp);
jfrm.setVisible(true);
}
public static void main(String[] args) {
// launch app
//create the frame on the event dispaytching thread
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new GUI();
}
});
}
}
The result is shown below
What I want to do is move the location of the JPanel to the top left no matter how big the frame is, eg location 0,0 of the tabbed page.Override the centring of the JPanel.
I am using the GridBaglayout manager.
Not to discount the power of something like MigLayout, but if you're stuck on a project which limits third party libraries, then knowing how to manipulate the default layout managers is one of the most important skills you can develop
You could simply add a "filler" component into you layout, setting it's weightx and weighty properties to fill the available space
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestLayout {
public static void main(String[] args) {
new TestLayout();
}
public TestLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.gridwidth = 2; //GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
JLabel title = new JLabel("Tracking System");
title.setFont(title.getFont().deriveFont(40f));
add(title, gbc);
gbc.gridy++;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First: "), gbc);
gbc.gridy++;
add(new JLabel("First: "), gbc);
gbc.gridx++;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridx++;
gbc.gridy++;
gbc.weightx = 1;
gbc.weighty = 1;
// Blank/filler component
add(new JLabel(), gbc);
}
}
}
By changing
gbc.gridwidth = 2; //GridBagConstraints.REMAINDER;
to
gbc.gridwidth = GridBagConstraints.REMAINDER;
I can effect the position of the title as well
But you can also effect it by using the anchor property. Which you use will come down to your needs
Have a look at How to Use GridBagLayout for more details
As the title said, I'm trying to put 4 different JLabels at each of the corners of a JFrame. I want them to stay there forever even if I try to resize the JFrame
I've tried using a layout manager but I just can't get it right.
ImageIcon icon;
JLabel labelNW = new JLabel();
JLabel labelNE = new JLabel();
JLabel labelSW = new JLabel();
JLabel labelSE = new JLabel();
URL buttonURL = InputOutputTest.class.getResource("images/square_dot.gif");
if(buttonURL != null){
icon = new ImageIcon(buttonURL);
labelNW.setIcon(icon);
labelNE.setIcon(icon);
labelSW.setIcon(icon);
labelSE.setIcon(icon);
}
window.add(labelNW, BorderLayout.NORTH);
//window.add(labelNE, BorderLayout.EAST);
//window.add(labelSW, BorderLayout.WEST);
window.add(labelSE, BorderLayout.SOUTH);
This code takes care of the north and south of the left side. I'm probably approaching this wrong though.
I also tried GridLayout (2,2) but they weren't at the corners and there's a huge gap on the right side.
You will want to nest JPanels each using its own layout. In fact you could do this by nesting JPanels that all use BorderLayout.
Going to check if GridBagLayout can do it in one shot.... hang on...
Yep GridBagLayout does it too:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class GridBagExample {
public static void main(String[] args) {
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(
0, 0, 0, 0), 0, 0);
mainPanel.add(new JLabel("Left Upper"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHEAST;
mainPanel.add(new JLabel("Right Upper"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.SOUTHWEST;
mainPanel.add(new JLabel("Left Lower"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
mainPanel.add(new JLabel("Right Lower"), gbc);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
Edit
Now for the BorderLayout example:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class BorderLayoutExample {
public static void main(String[] args) {
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.add(new JLabel("North East"), BorderLayout.EAST);
northPanel.add(new JLabel("North West"), BorderLayout.WEST);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(new JLabel("South East"), BorderLayout.EAST);
southPanel.add(new JLabel("South West"), BorderLayout.WEST);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(southPanel, BorderLayout.SOUTH);
JFrame frame = new JFrame("BorderLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
I find that only GroupLayout gives me the control I need for precisely laid out components. This should do the trick. You need to make sure the gap in between has a very large Maximum value (i.e. Short.MAX_VALUE), but you can set the minimum and preferred sizes to whatever you want.
public class LabelFrame extends JFrame {
public LabelFrame() {
JPanel contentPane = new JPanel();
JLabel labelNW = new JLabel();
JLabel labelNE = new JLabel();
JLabel labelSW = new JLabel();
JLabel labelSE = new JLabel();
URL buttonURL = InputOutputTest.class.getResource("images/square_dot.gif");
if(buttonURL != null){
icon = new ImageIcon(buttonURL);
labelNW.setIcon(icon);
labelNE.setIcon(icon);
labelSW.setIcon(icon);
labelSE.setIcon(icon);
}
GroupLayout layout = new GroupLayout(contentPane);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(labelNW)
.addComponent(labelSW))
.addGap(20,50,Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(labelNE)
.addComponent(labelSE))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addComponent(labelNW)
.addComponent(labelNE))
.addGap(20,50,Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.TRAILING)
.addComponent(labelSW)
.addComponent(labelSE))
);
contentPane.setLayout(layout);
setContentPane(contentPane);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
LabelFrame frame = new LabelFrame();
frame.setVisible(true);
}
});
}
}
I've tried using a layout manager..
Layout managers are wonderful at what they do, but are perhaps the wrong tool for this job. Consider using a custom border instead. Here is an example.