7

I want the table with the same width as the frame and also when I resize the frame the table need to be resized too. I think setSize() of JTable doesn't work correctly. Can you help me?

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public Main() {
        setSize(400, 600);
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", 2},
            {"Jhon", "ewrewr", 4},
            {"Max", "zxczxc", 6}
        };

        JTable table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);

        int A = this.getWidth();
        int B = this.getHeight();

        table.setSize(A, B);
        JPanel tablePanel = new JPanel();
        tablePanel.add(tableSP);
        tablePanel.setBackground(Color.red);

        add(tablePanel);
        setTitle("Marks");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setVisible(true);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1761818
  • 365
  • 1
  • 7
  • 14
  • First of all: There is no need to add JScrollPane on your JPanel. Second: Don't call setSize(); at all, call pack();. – Branislav Lazic Nov 03 '12 at 21:28
  • I thank you might like to have read through [Using Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html) – MadProgrammer Nov 03 '12 at 21:42

4 Answers4

19

have look at

table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setFillsViewportHeight(true);

for the code you posted here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
8

Get rid of all the setSize calls. Then get rid of the tablePanel and just at the tableSP directly to the content pane of the JFrame.

The content pane of a JFrame has by default a BorderLayout, and adding a component to the BorderLayout#CENTER (which is the default when you simply use add without constraints) will make sure the component takes the size of the parent container.

Quickly adjusted your code

public class Main extends JFrame {

    public Main() {
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", 2},
            {"Jhon", "ewrewr", 4},
            {"Max", "zxczxc", 6}
        };

        JTable table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);
        tableSP.setPreferredSize( 400, 600 );

        add(tableSP);
        setTitle("Marks");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setVisible(true);
            }
        });
    }
}

Small side-note: there is no reason to extend JFrame. Just use one

Robin
  • 36,233
  • 5
  • 47
  • 99
  • never to setPreferredSize for JTable (e.g.) in JScrollpane :-) – mKorbel Nov 03 '12 at 22:57
  • @mKorbel I set the preferred size of the `JScrollPane`. I thought this was one of the few valid use-cases where you could call `setXXXSize`. How else would it determine its size and not just start showing scrollbars ? – Robin Nov 04 '12 at 07:36
1

As an alternative, use GridLayout for your tablePanel and pack() the enclosing Window.

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public Main() {
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", 2},
            {"Jhon", "ewrewr", 4},
            {"Max", "zxczxc", 6}
        };

        JTable table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);

        int A = this.getWidth();
        int B = this.getHeight();

        table.setSize(A, B);
        JPanel tablePanel = new JPanel(new GridLayout());
        tablePanel.add(tableSP);
        tablePanel.setBackground(Color.red);

        add(tablePanel);
        setTitle("Marks");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setVisible(true);
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Here is an answer:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Main extends JFrame {
    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(new Object[][]{},new String[]{"A","B","C"});
    public Main() {
        super("Mark");
        table.setModel(model);
        add(new JScrollPane(table));

   }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Main ex = new Main();
                ex.setDefaultCloseOperation(EXIT_ON_CLOSE);
                ex.setLocationByPlatform(true);
                ex.pack();
                ex.setVisible(true);
            }
        });
    }
}

Quite simplified, but same. :)

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85