简体   繁体   English

Java-JScrollPane与JTable不对齐

[英]Java - JScrollPane Doesn't align with JTable

I am trying to make a JTable that has a width of 500. The problem is that the JScrollPane associated with the table doesn't appear next to the table. 我试图制作一个宽度为500的JTable。问题是与表关联的JScrollPane不在表旁边。

Here is the relevant code: 以下是相关代码:

    // Create authorsPanel
    JPanel authorsPanel = new JPanel(new BorderLayout(5,5));
    authorsPanel.setBorder( new TitledBorder("Author Selection") );

    // Configure author table
    DefaultTableModel authorstableModel = new DefaultTableModel(new String[80][1], new String[]{"Authors"}) {
        @Override
        public boolean isCellEditable(int row, int column) {
           //all cells false
           return false;
        }
    };
    JTable authorsTable = new JTable(authorstableModel);
    authorsTable.getColumnModel().getColumn(0).setPreferredWidth(500);
    authorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    try {
        authorsTable.setAutoCreateRowSorter(true);
    } catch(Exception continuewithNoSort) {
    }
    JScrollPane tableScroll = new JScrollPane(authorsTable);
    Dimension tablePreferred = tableScroll.getPreferredSize();
    tableScroll.setPreferredSize(new Dimension(500, tablePreferred.height/3));
    authorsPanel.add(tableScroll);

Here is a screenshot: 这是屏幕截图:

When I get rid of the line: 当我摆脱这条线:

authorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

Then the table returns to being the full width of the panel, so it seems like I need this line. 然后表格返回到面板的整个宽度,因此似乎我需要此行。

The java docs on BorderLayout states : 关于BorderLayout的Java文档指出:

The components are laid out according to their preferred sizes and the constraints of the container's size. 根据组件的首选大小和容器大小的限制来布局组件。 The NORTH and SOUTH components may be stretched horizontally; NORTH和SOUTH分量可以水平拉伸; the EAST and WEST components may be stretched vertically; EAST和WEST分量可以垂直拉伸; the CENTER component may stretch both horizontally and vertically to fill any space left over. CENTER组件可以水平和垂直拉伸以填充剩余的任何空间。

You have used authorsPanel.add(tableScroll) to add the JScrollPane to the JPanel . 您已经使用authorsPanel.add(tableScroll)JScrollPane添加到JPanel So you are basically adding it to the center . 因此,您基本上是将其添加到中心 So this is going to occupy the whole space that is lying vacant. 因此,这将占据整个空置的空间。

The solution to your problem lies in choosing a different layout. 解决问题的办法在于选择不同的布局。 I could suggest MigLayout which is very versatile and you can get all kinds of effects using it. 我建议使用MigLayout,它用途广泛,可以使用它获得各种效果。

You add your scroll pane to a panel with BorderLayout. 您将滚动窗格添加到带有BorderLayout的面板中。 BorderLayout does not care about preferred size. BorderLayout不在乎首选大小。 Use eg GridBagLayout with proper constraints or you can BoxLayout (with horizontal flow) placing the table first and an empty panel second as place holder. 例如,使用具有适当约束的GridBagLayout,或者可以将BoxLayout(水平流动)放在第一位,然后将空白面板放在占位符上。

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

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