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
Related
So I have a main JPanel that I would want to use as a container for more panels. I would like the child panels to have the same width as its parent and I tried using GridBagLayout to achieve it:
private JPanel createPanels() {
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JPanel p1 = new JPanel();
p1.add(new JLabel("Test"));
p1.add(new JButton("+"));
content.add(p1, gbc);
return content;
}
With this piece of code, I get the following result:
As you can see, the panel that just got created doesn't fit the width of its container (green panel). I thought gbc.anchor = GridBagConstraints.LINE_START; and gbc.fill = GridBagConstraints.HORIZONTAL; lines would be in charge of doing that, but apparently they did not.
How can I achieve this?
I made a few changes to your code and created the following GUI.
In your code, you created a JPanel with a FlowLayout and placed it inside a JPanel with a GridBagLayout. The FlowLayout JPanel is considered one Swing component, so it won't adjust to the container size.
I added a main method and a JFrame to your code to show the difference.
Here's the complete runnable code that I think you want.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GridBagLayoutTest2 implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutTest2());
}
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createPanels(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createPanels() {
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.0;
content.add(new JLabel("Test"), gbc);
gbc.gridx++;
gbc.weightx = 1.0;
content.add(new JButton("+"), gbc);
return content;
}
}
I want to make the copyright in the image below in the center of the frame.
I tried to add SwingConstants.CENTER and Component.CENTER_ALIGNMENT but neither worked.
Can someone please help me with this one? Thanks in advance.
enter image description here
This is my code:
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JTextField text1;
JTextField text2;
JLabel label1;
JLabel label2;
JButton encrypt;
JButton decrypt;
JLabel copyright;
//constructor
public MyFrame(){
super("Encryption and Decryption");
setLayout(new FlowLayout()); //add first text field
label1 = new JLabel("enter a text: ");
add(label1);
text1= new JTextField(10);
add(text1);
//add second text field
label2 = new JLabel("result: ");
add(label2);
text2= new JTextField(10);
add(text2);
encrypt = new JButton("encrypt!");
add(encrypt);
decrypt = new JButton("decrypt!");
add(decrypt);
copyright = new JLabel(" © Project realized by Ayoub Touti ");
add(copyright);
//organizing elements in a panel
JPanel panel = new JPanel(new GridLayout(4,2,12,6));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(encrypt);
panel.add(decrypt);
panel.add(copyright);
add(panel);
}
}
//////////////////////////////
import javax.swing.*;
public class main {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (500, 200);
frame.setResizable(true);
frame.setVisible (true);
frame.setLocationRelativeTo(null); // to open the window in the main screen
}
}
Use a GridBagLayout (or a combination of layouts through compound components, slightly demonstrated below)
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
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() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = gbc.LINE_END;
gbc.insets = new Insets(0, 8, 0, 8);
add(new JLabel("enter a text:"), gbc);
gbc.gridy++;
add(new JLabel("result:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = gbc.LINE_START;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
JPanel actionPane = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = gbc.HORIZONTAL;
gbc.insets = new Insets(0, 0, 0, 0);
actionPane.add(new JButton("encyprt!"), gbc);
gbc.gridx++;
actionPane.add(new JButton("decyprt!"), gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.gridy = 2;
add(actionPane, gbc);
gbc.gridy++;
add(new JLabel("© Project realized by Ayoub Touti"), gbc);
}
}
}
See How to Use GridBagLayout for more details
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'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);
}
}
}