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 .
Related
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
How do I get my label and combo box to appear vertical instead of horizontal in java? I tried setting the layout to null but it hides the label.
public PetrolApplication()
{
setLayout(new FlowLayout());
add(label);
add(label2);
Combo1 = new JComboBox(Arraycities);
Combo1.setBounds(50, 50, 100, 20);
Use a different layout manager - see A Visual Guide to Layout Managers
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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(16, 16, 16, 16));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add( new JLabel("I'm assuming you want something"), gbc);
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(new JLabel("Please make a selection"), gbc);
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
model.addElement("This is not what you are looking for");
model.addElement("This is something else");
model.addElement("Not what you think");
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx++;
add(new JComboBox<String>(model));
}
}
}
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
public class InputURL {
public InputURL() {
input();
}
private static JFrame mainFrame = Launcher.returnFrame();
private static JPanel mainPanel;
private void input(){
//Defining a Panel on which everything will be set
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
//To put the GridBagLayout Constraints
GridBagConstraints c = new GridBagConstraints();
//Set Panel Size
mainPanel.setSize(Constants.windowWidth,Constants.windowHeight);
//Setting the Input Box
JTextField inputURL = new JTextField();
c.fill = GridBagConstraints.CENTER;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
mainPanel.add(inputURL, c);
//Adding the start button
JButton submitURL = new JButton(Constants.SUBMIT_URL_BUTTON);
submitURL.addActionListener(new ActionListener() {
//The action performed by the start button
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
}
});
//The placement of the button
c.fill = GridBagConstraints.VERTICAL;
c.ipady = 50;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 2;
mainPanel.add(submitURL, c);
//Adding the components on Panel to Frame
mainFrame.add(mainPanel);
}
}
The ouput and expected output can be viewed here. Need help regarding the Output. E.G.:
I've tried using GridBagConstraints and for some reason I cannot play much with the UI.
A lot will come down to you final needs, for example, this just makes use of some of the basic properties of GridBagConstraints
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.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 {
private JTextField inputURL;
private JButton submitURL ;
public TestPane() {
inputURL = new JTextField(20);
submitURL = new JButton("Submit the URL");
submitURL.setMargin(new Insets(10, 10, 10, 10));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 10, 10, 10);
add(new JLabel("Header"), gbc);
gbc.gridy++;
gbc.insets = new Insets(0, 10, 20, 10);
add(inputURL, gbc);
gbc.gridy++;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weighty = 1.0;
add(submitURL, gbc);
}
}
}
A more complex solution might make use of compound layouts, using a mixture of BorderLayout, GridLayout and GridBagLayout
For example...
public class TestPane extends JPanel {
private JTextField inputURL;
private JButton submitURL ;
public TestPane() {
setLayout(new BorderLayout());
JLabel header = new JLabel("Header");
header.setHorizontalAlignment(JLabel.CENTER);
header.setBorder(new EmptyBorder(10, 10, 10, 10));
add(header, BorderLayout.NORTH);
inputURL = new JTextField(20);
submitURL = new JButton("Submit the URL");
submitURL.setMargin(new Insets(10, 10, 10, 10));
JPanel content = new JPanel(new GridLayout(2, 1));
JPanel field = new JPanel(new GridBagLayout());
field.add(inputURL);
content.add(field);
JPanel actions = new JPanel(new GridBagLayout());
actions.add(submitURL);
content.add(actions);
add(content);
}
}
While it might sound stupid, spend more time focusing on the actual workflow of the user, this will lead you to the best layout decisions, rather then focus on the physical layout itself
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);
}
}
}