0

我有这个代码:

    JPanel textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(0,1));
    //JPanel text = new JPanel();
    //text.setBorder(new TitledBorder(new EtchedBorder(), "Disploay Area"));
    MyModel model = new MyModel();
    //sorter = new TableRowSorter<MyModel>(model);
    final JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    table.setAutoCreateRowSorter(true);
    //table.setRowSorter(sorter);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent event) {
                    int viewRow = table.getSelectedRow();
                    if (viewRow < 0) {
                        //Selection got filtered away.
                        txtstatus.setText("");
                    } else {
                        int modelRow = 
                            table.convertRowIndexToModel(viewRow);
                        txtstatus.setText(
                            String.format("Selected Row in view: %d. " +
                                "Selected Row in model: %d.", 
                                viewRow, modelRow));
                    }
                }
            }
    );
    JScrollPane scroll = new JScrollPane(table);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    textPanel.add(scroll, BorderLayout.CENTER);
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.ipadx = 800;
    c.gridy = 1;
    c.ipady = 460;
    c.anchor = GridBagConstraints.CENTER;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(0,0,0,0);
    con.add(textPanel, c);

我真的对我所做的事情感到困惑,因为我设计的程序可以正常工作,但是如果我想调整一行,那么我可以看到所有信息会挤压其余的列。无论如何,如果我想调整不会影响其余列的列的大小?

GUI已经连接到数据库并检索信息并显示在JTable. 任何帮助将非常感激。

4

2 回答 2

3

您可以使用:

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

现在将根据需要出现一个水平滚动条。

于 2013-04-27T19:05:46.030 回答
3

看看JTable#setAutoResizeMode

它定义了当您更改列的大小时应如何调整后续列的大小。

默认情况下,JTable使用AUTO_RESIZE_SUBSEQUENT_COLUMNS

于 2013-04-27T19:06:00.700 回答