GridLayout Help in Java

I have a JPanel with the following code:

JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2, 10, 30));
final JTextField fileName = new JTextField();
pane.add(fileName);
JButton creater = new JButton("Create File");
pane.add(creater);
JButton deleter = new JButton("Delete File");
pane.add(deleter);

I was wondering, how can I make JTextField take up two spaces in GridLayout, and two buttons divide the line, taking up one space in one line?

+3
source share
4 answers

This is hard to do with GridLyout. You create wider cells (for example, new GridLayout(2, 2, 10, 30)and then add a TextField to the fist cell. Then you need to create another panel with GridLayout (2, 1), put it in the cell in the second row and add your button in the 1st row of the cell of this nested grid scheme.

Soon you will need a GridLayout in another GridLayout.

. GridBagLayout. , , :). , MigLayout. JDK, , .

+2

, GBL, , " , ", ( ).

SSCCE .

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class SimpleLayoutTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JPanel ui = new JPanel( new BorderLayout(20,20) );
                // I would go for an EmptyBorder here, but the filled
                // border is just to demonstrate where the border starts/ends
                ui.setBorder( new LineBorder(Color.RED,15) );

                // this should be a button that pops a JFileChooser, or perhaps
                // a JTree of the existing file system structure with a JButton
                // to prompt for the name of a new File.
                final JTextField fileName = new JTextField();
                ui.add(fileName, BorderLayout.NORTH);

                JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 30));
                ui.add(buttonPanel, BorderLayout.CENTER);

                JButton creater = new JButton("Create File");
                buttonPanel.add(creater);
                JButton deleter = new JButton("Delete File");
                buttonPanel.add(deleter);

                JOptionPane.showMessageDialog(null, ui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
+1

GridBagLayout.

:

    JPanel pane = new JPanel();
    GridBagLayout gridbag  = new GridBagLayout();
    pane.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();

    final JTextField fileName = new JTextField();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(fileName, c);

    JButton creater = new JButton("Create File");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 1;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(creater, c);

    JButton deleter = new JButton("Delete File");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 1;
    pane.add(deleter, c);
0

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


All Articles