Domanda

I am trying to make the JTable adjust its size automatically as I resize the window containing the JTable.

As the Example below: http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/components/SimpleTableDemo.java

The JTable will adjust its width, also it has a scroll bar on the right if the height is too small to display all.

This one is the JFrame, I use separate class extends JPanel:

public class GUIFrame 
{

public static void main(String[] args)
{

    //Setting up JFrame's basic properties
    JFrame mainFrame = new JFrame("Muney Manager");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Setting up panels:
    JPanel mPanel = new JPanel();
    mPanel.setLayout(new BoxLayout(mPanel,BoxLayout.PAGE_AXIS));

    TablePanel tPanel = new TablePanel();
    ToolBar buttonPanel = new ToolBar(tPanel);

    mPanel.add(buttonPanel);
    mPanel.add(tPanel);

    //Setting up Panels and Frame to be displayed :3
    mainFrame.getContentPane().add(mPanel);
    mainFrame.pack();
    mainFrame.setVisible(true);

}

This is the one containing the JTable:

public class TablePanel extends JPanel
{

//DefaultTableModel is needed for adding new rows
private JTable table;
private DefaultTableModel tmodel;

private JScrollPane scrollPane;

//Table's column name
private String[] columnNames = {"Date",
                                "Category",
                                "Details",
                                "Add/Subtract",
                                "Total"};


Object[][] data = {
        {20140925, "Grocery", "Supermarket", -5.23,600.00},
        {20141013,"Car Maintenance", "Changing Tires", -200.00, 400.00}
};




public TablePanel()
{

    //Initializing tmodel and putting it into table
    //It's needed for "adding rows" method
    tmodel = new DefaultTableModel(data, columnNames);
    table = new JTable();

    table.setModel(tmodel);


    //Setting up JScrollPane, table Headder
    //and make scrollPane visible 
    table.setFillsViewportHeight(true);
    add(new JScrollPane(table));

}

For some reason, I can't get it work, what's wrong with my code? The JTable width doesn't resize, also I can't seem to get a Scroll Bar on the right

enter image description here

È stato utile?

Soluzione

Consider using a BorderLayout instead...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame mainFrame = new JFrame("Muney Manager");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Setting up panels:
            JPanel mPanel = new JPanel();
            mPanel.setLayout(new BorderLayout());

            TablePanel tPanel = new TablePanel();
            ToolBar buttonPanel = new ToolBar(tPanel);

            mPanel.add(buttonPanel, BorderLayout.NORTH);
            mPanel.add(tPanel);

            //Setting up Panels and Frame to be displayed :3
            mainFrame.getContentPane().add(mPanel);
            mainFrame.pack();
            mainFrame.setVisible(true);
        }
    });
}

Or if you want more control, maybe even a GridBagLayout

Updated

JPanel by default uses a FlowLayout, try changing the TablePanel to use a BorderLayout as well...

public TablePanel()
{
    setLayout(new BorderLayout());

    //Initializing tmodel and putting it into table
    //It's needed for "adding rows" method
    tmodel = new DefaultTableModel(data, columnNames);
    table = new JTable();

    table.setModel(tmodel);


    //Setting up JScrollPane, table Headder
    //and make scrollPane visible 
    table.setFillsViewportHeight(true);
    add(new JScrollPane(table));

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top