JTable自动滚动到底部

时间:2011-05-10 21:20:04

标签: java swing scroll jtable

我有一个包含3个JScrollPanes的JPanel(每个包含一个Jtable)添加了一个boxlayout,所以我在页面中看到3个表并且我动态加载数据,代码逻辑对于3个表几乎相同,只有列名和一些单元格渲染是不同的,对于每个表格,我想在新表格添加到表格时自动滚动到表格的底部,前两个表格工作完美,滚动条到表格底部,但最后一个表格的滚动条做了奇怪的事情!我对3个表使用完全相同的滚动方法,但前两个工作不起作用!

有什么想法吗?

我删除了一些列添加代码以便清晰,但这是理念;

private JScrollPane fillThirdTable(ArrayList<DisplayVariable> displayList) {
    DefaultTableModel model = new DefaultTableModel();

    ToolTipTable answer = new ToolTipTable(model);

            answer.setRowHeight(60);
    answer.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    answer.setSize(1300, 400);  
    DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
    dtcr.setHorizontalAlignment(SwingConstants.CENTER);
    answer.getColumn("Display Variable ID").setCellRenderer(dtcr);

    JScrollPane scrollPane = null;
    for (DisplayVariable var : displayList) {

        model.addRow(new Object[] { id, shown, name, value });
        answer.setFillsViewportHeight(true);    
    }


    TableColumn c= answer.getColumnModel().getColumn(3);
    c.setCellRenderer(new MultiLineCellRenderer());

    TableColumn c2= answer.getColumnModel().getColumn(2);
    c2.setCellRenderer(new MultiLineCellRenderer());


    scrollPane = new JScrollPane(answer);
    scrollPane.setSize(1300, 400);

//here I call the method
    scrollToVisible(answer, (displayList.size()-1), 1);

    return scrollPane;

}

这是自动滚动的方法;

public void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;
    }
    JViewport viewport = (JViewport)table.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);

    // The location of the viewport relative to the table
    Point pt = viewport.getViewPosition();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0)
    rect.setLocation(rect.x-pt.x, rect.y-pt.y);

    // Scroll the area into view
    viewport.scrollRectToVisible(rect);
}

3 个答案:

答案 0 :(得分:6)

据我所知,您不需要在父母身上工作。只需使用:

public void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
    table.scrollRectToVisible(table.getCellRect(rowIndex, vColIndex, true));
}

我确实遇到过使用SwingUtilities.invokeLater()排序的滚动问题,所以你可能也想尝试一下:

public void scrollToVisible(final JTable table, final int rowIndex, final int vColIndex) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            table.scrollRectToVisible(table.getCellRect(rowIndex, vColIndex, false));
        }
    });
}

答案 1 :(得分:1)

我看不到你在说什么,这只是http://www.exampledepot.com/egs/javax.swing.table/pkg.html

的例子的完整副本 例如

(这里有三个JTable,但另一个故事)

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;

public class TableSelection implements ListSelectionListener {

    private JTable[] tables;
    private boolean ignore = false;

    public TableSelection() {
        Object[][] data1 = new Object[100][5];
        Object[][] data2 = new Object[50][5];
        Object[][] data3 = new Object[50][5];
        for (int i = 0; i < data1.length; i++) {
            data1[i][0] = "Company # " + (i + 1);
            for (int j = 1; j < data1[i].length; j++) {
                data1[i][j] = "" + (i + 1) + ", " + j;
            }
        }
        for (int i = 0; i < data2.length; i++) {
            data2[i][0] = "Company # " + ((i * 2) + 1);
            for (int j = 1; j < data2[i].length; j++) {
                data2[i][j] = "" + ((i * 2) + 1) + ", " + j;
            }
        }
        for (int i = 0; i < data3.length; i++) {
            data3[i][0] = "Company # " + (i * 2);
            for (int j = 1; j < data3[i].length; j++) {
                data3[i][j] = "" + (i * 2) + ", " + j;
            }
        }
        String[] headers = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
        DefaultTableModel model1 = new DefaultTableModel(data1, headers);
        DefaultTableModel model2 = new DefaultTableModel(data2, headers);
        DefaultTableModel model3 = new DefaultTableModel(data3, headers);
        final JTable jTable1 = new JTable(model1);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        final JScrollPane sp1 = new JScrollPane();
        sp1.setPreferredSize(new Dimension(600, 200));
        sp1.setViewportView(jTable1);
        final JTable jTable2 = new JTable(model2);
        jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        final JScrollPane sp2 = new JScrollPane();
        sp2.setPreferredSize(new Dimension(600, 200));
        sp2.setViewportView(jTable2);
        final JTable jTable3 = new JTable(model3);
        jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        final JScrollPane sp3 = new JScrollPane();
        sp3.setPreferredSize(new Dimension(600, 200));
        sp3.setViewportView(jTable3);
        //TableSelection tableSelection = new TableSelection(jTable1, jTable2, jTable3);
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(3, 0, 10, 10));
        panel1.add(sp1);
        panel1.add(sp2);
        panel1.add(sp3);
        JFrame frame = new JFrame("tableSelection");
        frame.add(panel1);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public TableSelection(JTable... tables) {
        for (JTable table : tables) {
            table.getSelectionModel().addListSelectionListener(this);
        }
        this.tables = tables;
    }

    private JTable getTable(Object model) {
        for (JTable table : tables) {
            if (table.getSelectionModel() == model) {
                return table;
            }
        }
        return null;
    }

    private void changeSelection(JTable table, String rowKey) {
        int col = table.convertColumnIndexToView(0);
        for (int row = table.getRowCount(); --row >= 0;) {
            if (rowKey.equals(table.getValueAt(row, col))) {
                table.changeSelection(row, col, false, false);
                return;
            }
        }
        table.clearSelection();
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() || ignore) {
            return;
        }
        ignore = true;
        try {
            JTable table = getTable(e.getSource());
            int row = table.getSelectedRow();
            String rowKey = table.getValueAt(row, table.convertColumnIndexToView(0)).toString();
            for (JTable t : tables) {
                if (t == table) {
                    continue;
                }
                changeSelection(t, rowKey);
                JViewport viewport = (JViewport) t.getParent();
                Rectangle rect = t.getCellRect(t.getSelectedRow(), 0, true);
                Rectangle r2 = viewport.getVisibleRect();
                t.scrollRectToVisible(new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
                System.out.println(new Rectangle(viewport.getExtentSize()).contains(rect));
            }
        } finally {
            ignore = false;
        }
    }

    public static void main(String[] args) {
        TableSelection tableSelection = new TableSelection();
    }
}

答案 2 :(得分:0)

我刚删除了“scrollPane.setSize(1300,400)”并且它有效