简体   繁体   English

JTable根据ScrollPane的大小调整大小

[英]JTable resizes according to ScrollPane's size

I am trying to make the JTable adjust its size automatically as I resize the window containing the JTable. 当我调整包含JTable的窗口的大小时,我试图使JTable自动调整其大小。

As the Example below: http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/components/SimpleTableDemo.java 如以下示例所示: 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. JTable将调整其宽度,如果高度太小而无法显示全部内容,则JTable还将在右侧具有滚动条。

This one is the JFrame, I use separate class extends JPanel: 这是JFrame,我使用单独的类扩展了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: 这是包含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 JTable的宽度不会调整大小,而且我似乎也无法在右侧获得滚动条

在此处输入图片说明

Consider using a BorderLayout instead... 考虑改用BorderLayout ...

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 或者,如果您想要更多控制权,甚至可以使用GridBagLayout

Updated 更新

JPanel by default uses a FlowLayout , try changing the TablePanel to use a BorderLayout as well... JPanel默认使用FlowLayout ,尝试将TablePanel更改为也使用BorderLayout

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));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM