-4

I am making a Swing GUI. I am not getting how to max and min the JTable which is inside a JPanel.

  • Please update you question and show others what you have tried so far. – MWiesner Jul 22 '19 at 09:09
  • What do you mean *`how to max and min`*? – George Z. Jul 22 '19 at 09:39
  • In my Swing GUI I have a panel which is having a table. I want to maximize that table to full screen. – anish sethiya Jul 22 '19 at 12:46
  • Hello, a good way to get better help sooner is to show us what you have tried and exactly where you are stuck. Have a look at [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), often known as a MCVE or [SSCCE](http://sscce.org/). – Dan Jul 22 '19 at 14:47

1 Answers1

3

Edit From comments

You comment said I want to make the table "full screen". If by full screen you mean, the size of the container, you can simply do.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;

public class JTableExample {
    public static void main(String[] args) {
        JPanel borderLayoutPanel = new JPanel(new BorderLayout());
        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);
        borderLayoutPanel.add(scrollableTable, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(borderLayoutPanel);
        frame.setSize(800, 800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

If you actually want a full screen window, look at this.


There are a few ways to do it.

I recommend you read this question as it will help explain why null layouts are bad.

The TLDR is basically use a layout manager. My preferred layout manager is MigLayout as it is simple to use and understand but full of features. If you can't use MigLayout as it is a 3rd party library, then use something like GridBagLayout

MigLayout Website and MigLayout Download Page.

My Preferred Layout Manager

If you are willing to use a third part layout manager, then I think MigLayout is the way to go. It provides more control and I think is easier to use than most layout managers.

private JPanel createMigPanel() {
    String layoutConstraints = "fillx, filly";
    String columnAndRowConstraints = "fill, grow, center";

    JPanel migPanel = new JPanel(new MigLayout(layoutConstraints, columnAndRowConstraints, columnAndRowConstraints));

    String[] headers = {"Header 1", "Header 2", "etc..."};
    String[][] data = {{"Some data", "some more data", "etc..."},
            {"Some data 1", "some more data 3", "etc..."},
            {"Some data 2", "some more data 4", "etc..."}};

    JTable table = new JTable(data, headers);
    JScrollPane scrollableTable = new JScrollPane(table);

    migPanel.add(new JScrollPane(table), "width 400:500:800, height 400:500:800");

    return migPanel;
}

The Bad Way

If you must use a null layout in a JPanel, which is not recommended at all, then you can use two buttons to set the table to its min / max size.

// Not recommended
private JPanel createNullPanel() {
    JPanel nullPanel = new JPanel(null);

    String[] headers = {"Header 1", "Header 2", "etc..."};
    String[][] data = {{"Some data", "some more data", "etc..."},
            {"Some data 1", "some more data 3", "etc..."},
            {"Some data 2", "some more data 4", "etc..."}};

    JTable table = new JTable(data, headers);
    JScrollPane scrollableTable = new JScrollPane(table);

    scrollableTable.setMinimumSize(new Dimension(100, 100));
    scrollableTable.setMaximumSize(new Dimension(500, 500));

    JButton minButton = new JButton("Min");
    minButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            scrollableTable.setSize(scrollableTable.getMinimumSize());
        }
    });
    minButton.setBounds(10, 10, 50, 25);
    nullPanel.add(minButton);


    JButton maxButton = new JButton("Min");
    maxButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            scrollableTable.setSize(scrollableTable.getMaximumSize());
        }
    });
    maxButton.setBounds(70, 10, 50, 25);
    nullPanel.add(maxButton);

    nullPanel.add(scrollableTable);
    scrollableTable.setBounds(10, 45, 300, 300);

    return nullPanel;
}

Inbuilt Layout Manager

But, you can use a layout manager. One that is a bit complicated but provides some more control than most is the GridBagLayout.

private JPanel createGridBagPanel() {
    JPanel gridBagPanel = new JPanel(new GridBagLayout());

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(10, 10, 10, 10);

    String[] headers = {"Header 1", "Header 2", "etc..."};
    String[][] data = {{"Some data", "some more data", "etc..."},
            {"Some data 1", "some more data 3", "etc..."},
            {"Some data 2", "some more data 4", "etc..."}};

    JTable table = new JTable(data, headers);
    JScrollPane scrollableTable = new JScrollPane(table);
    scrollableTable.setMinimumSize(new Dimension(400, 400));
    scrollableTable.setPreferredSize(new Dimension(500, 500));
    scrollableTable.setMaximumSize(new Dimension(800, 800));

    // Nasty work around to support min and max size
    // https://stackoverflow.com/questions/15161332/setting-up-a-maximum-component-size-when-using-gridbaglayout-in-java
    JPanel wrappingPanel = new JPanel(null);
    wrappingPanel.setLayout(new BoxLayout(wrappingPanel, BoxLayout.LINE_AXIS));
    wrappingPanel.add(scrollableTable);

    gridBagPanel.add(wrappingPanel, constraints);

    return gridBagPanel;
}

Complete code sample

import net.miginfocom.swing.MigLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTableExample {

    private void run() {
        setUpWindow();
    }

    private void setUpWindow() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // frame.getContentPane().add(createNullPanel());
        // frame.getContentPane().add(createGridBagPanel());
        frame.getContentPane().add(createMigPanel());

        // If using null layout manager
        // frame.setSize(800, 800);

        // If using a none null layout manager
        frame.pack();

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    // Not recommended
    private JPanel createNullPanel() {
        JPanel nullPanel = new JPanel(null);

        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);

        scrollableTable.setMinimumSize(new Dimension(100, 100));
        scrollableTable.setMaximumSize(new Dimension(500, 500));

        JButton minButton = new JButton("Min");
        minButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scrollableTable.setSize(scrollableTable.getMinimumSize());
            }
        });
        minButton.setBounds(10, 10, 50, 25);
        nullPanel.add(minButton);


        JButton maxButton = new JButton("Min");
        maxButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scrollableTable.setSize(scrollableTable.getMaximumSize());
            }
        });
        maxButton.setBounds(70, 10, 50, 25);
        nullPanel.add(maxButton);

        nullPanel.add(scrollableTable);
        scrollableTable.setBounds(10, 45, 300, 300);

        return nullPanel;
    }

    private JPanel createGridBagPanel() {
        JPanel gridBagPanel = new JPanel(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = new Insets(10, 10, 10, 10);

        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);
        scrollableTable.setMinimumSize(new Dimension(400, 400));
        scrollableTable.setPreferredSize(new Dimension(500, 500));
        scrollableTable.setMaximumSize(new Dimension(800, 800));

        // Nasty work around to support min and max size
        // https://stackoverflow.com/questions/15161332/setting-up-a-maximum-component-size-when-using-gridbaglayout-in-java
        JPanel wrappingPanel = new JPanel(null);
        wrappingPanel.setLayout(new BoxLayout(wrappingPanel, BoxLayout.LINE_AXIS));
        wrappingPanel.add(scrollableTable);

        gridBagPanel.add(wrappingPanel, constraints);

        return gridBagPanel;
    }

    private JPanel createMigPanel() {
        String layoutConstraints = "fillx, filly";
        String columnAndRowConstraints = "fill, grow, center";

        JPanel migPanel = new JPanel(new MigLayout(layoutConstraints, columnAndRowConstraints, columnAndRowConstraints));

        String[] headers = {"Header 1", "Header 2", "etc..."};
        String[][] data = {{"Some data", "some more data", "etc..."},
                {"Some data 1", "some more data 3", "etc..."},
                {"Some data 2", "some more data 4", "etc..."}};

        JTable table = new JTable(data, headers);
        JScrollPane scrollableTable = new JScrollPane(table);

        migPanel.add(new JScrollPane(table), "width 400:500:800, height 400:500:800");

        return migPanel;
    }

    public static void main(String[] args) {
        JTableExample example = new JTableExample();
        example.run();
    }
}
Dan
  • 7,286
  • 6
  • 49
  • 114