Java: meshing

This is my first question here, so please forgive me when I break any rules or don’t use the correct format

I am creating a simple form in java swing that consists of 1 JLabel, 1 JTextField and 1 Button

|---------------------------| | | | JLabel | | | |---------------------------| | JTextField | Button | |---------------------------| 

The button should be in the lower right corner, to the left of the JTextField, JLabel on top, covering both columns

I want the button to be fixed size, JTextField to fixed height, but using full width (except what the button uses), and JLabel uses all other space (c and height)

I'm not even sure if I should use GridBagLayout or another layout?

This is probably a very simple question, but I'm pretty puzzled for quite some time (too many options with GridBarLayout, I think)

+4
source share
4 answers

OK, here is a demo code that should catch you:

 import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class TestGridBagLayout { protected void initUI() { JFrame frame = new JFrame(TestGridBagLayout.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("A button"); JTextField textField = new JTextField(); JLabel label = new JLabel("A cool long nice label that will stretch."); label.setHorizontalAlignment(JLabel.CENTER); label.setBorder(BorderFactory.createLineBorder(Color.GREEN)); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH;// Fill the "cell" in both direction gbc.weightx = 1.0;// Allocate extra-width to the label gbc.weighty = 1.0;// Allocate extra-height to the label gbc.gridwidth = GridBagConstraints.REMAINDER;// The label takes all the available width of the "row" panel.add(label, gbc); gbc.weighty = 0; // Don't stretch TF vertically gbc.fill = GridBagConstraints.HORIZONTAL; // Fill horizontally gbc.gridwidth = GridBagConstraints.RELATIVE; panel.add(textField, gbc); gbc.weightx = 0; // No extra horizontal space is given to the button gbc.fill = GridBagConstraints.NONE; // No fill for the button panel.add(button, gbc); frame.add(panel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestGridBagLayout().initUI(); } }); } } 
+1
source

First set the panel layout to GridBagLayout .

Then create a GridBagConstraints and set the fill to GridBagConstraints.BOTH .

In JLabel set the following properties of the constraint object: gridx = 0, gridy = 0, gridwidth = 2, gridheight = 2, weightx = 1, weighty = 1 .

For JTextField set the following properties for the constraint object: gridx = 0, gridy = 1, gridwidth = 1, gridheight = 1, weightx = 1, weighty = 0 .

For JButton set the following properties for the constraint object: gridx = 1, gridy = 1, gridwidth = 1, gridheight = 1, weightx = 0, weighty = 0 .

+2
source

The BorderLayout class is easy to use, less powerful than the GridBagLayout.

But when it’s simple, the solution should be the same.

 panel.add( label, BorderLayout.CENTER ); JPanel south = new JPanel(); south.add( textfield ); south.add( button ); button.setPreferredSize( x, y ); panel.add( south, BorderLayout.SOUTH ); 
+2
source

I don’t know this is a normal thing, but below is the code that works (mostly the code from Dan and Guillaume)

  //show stuff setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); //show label c.fill = GridBagConstraints.BOTH; // Fill the "cell" in both direction c.weightx = 1.0; // Allocate extra-width to the label c.weighty = 1.0; // Allocate extra-height to the label c.gridwidth = GridBagConstraints.REMAINDER; // The label takes all the available width of the "row" add(mlblShow,c); //show cmd txt c.weighty = 0; // Don't stretch TF vertically c.fill = GridBagConstraints.BOTH; // Fill horizontally and vertically c.gridwidth = GridBagConstraints.RELATIVE; add(mtxtCmd,c); //show send button c.weightx = 0; // No extra horizontal space is given to the button c.fill = GridBagConstraints.NONE; // No fill for the button add(cmdSend,c); 
+1
source

Source: https://habr.com/ru/post/1444375/


All Articles