0

以下のようにJTableのフリーズされた列を実装しました。垂直スクロールバーを使用して大きなデータセットをスクロールすると、フリーズされたテーブルの列がメインテーブルと整列しません。

ここに画像の説明を入力してください

JTable      table,fixedTable
JScrollPane     _scrl_table;

table = new JTable();
table.setName("MAIN_TABLE");
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setVisibleRowCount(8);


fixedTable = new JTable();
fixedTable.setName("FIXED_TABLE");
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

 _scrl_table = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

fixedTable.setPreferredScrollableViewportSize(fixedTable.getPreferredSize());
    _scrl_table.setRowHeaderView( fixedTable );
    _scrl_table.setCorner(JScrollPane.UPPER_LEFT_CORNER,         fixedTable.getTableHeader());  
4

1 に答える 1

2
  • おそらく2つを使用する理由はありませんJTables

  • camickrまたはRowHeaderで適切な RowNumberTable を使用する

  • AdjustmentListenerの有無にかかわらず共有できますBoundedRangeModel

水平方向のスクロールの例、垂直方向に変更する必要があります

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


public class FixedRowExample extends JFrame {

    private static final long serialVersionUID = 1L;
    private Object[][] data;
    private Object[] column;
    private JTable fixedTable, table;
    private int FIXED_NUM = 2;

    public FixedRowExample() {
        super("Fixed Row Example");
        data = new Object[][]{
                    {"a", "", "", "", "", ""},
                    {"", "b", "", "", "", ""},
                    {"", "", "c", "", "", ""},
                    {"", "", "", "d", "", ""},
                    {"", "", "", "", "e", ""},
                    {"", "", "", "", "", "f"},
                    {"fixed1", "", "", "", "", "", "", ""},
                    {"fixed2", "", "", "", "", "", "", ""}};
        column = new Object[]{"A", "B", "C", "D", "E", "F"};
        AbstractTableModel model = new AbstractTableModel() {

            private static final long serialVersionUID = 1L;

            @Override
            public int getColumnCount() {
                return column.length;
            }

            @Override
            public int getRowCount() {
                return data.length - FIXED_NUM;
            }

            @Override
            public String getColumnName(int col) {
                return (String) column[col];
            }

            @Override
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }

            @Override
            public void setValueAt(Object obj, int row, int col) {
                data[row][col] = obj;
            }

            public boolean CellEditable(int row, int col) {
                return true;
            }
        };

        AbstractTableModel fixedModel = new AbstractTableModel() {

            private static final long serialVersionUID = 1L;

            @Override
            public int getColumnCount() {
                return column.length;
            }

            @Override
            public int getRowCount() {
                return FIXED_NUM;
            }

            @Override
            public Object getValueAt(int row, int col) {
                return data[row + (data.length - FIXED_NUM)][col];
            }
        };
        table = new JTable(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        fixedTable = new JTable(fixedModel);
        fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JScrollPane fixedScroll = new JScrollPane(fixedTable) {

            private static final long serialVersionUID = 1L;

            @Override
            public void setColumnHeaderView(Component view) {
            } // work around
        }; // fixedScroll.setColumnHeader(null);  
        fixedScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        JScrollBar bar = fixedScroll.getVerticalScrollBar();
        JScrollBar dummyBar = new JScrollBar() {

            private static final long serialVersionUID = 1L;

            @Override
            public void paint(Graphics g) {
            }
        };
        dummyBar.setPreferredSize(bar.getPreferredSize());
        fixedScroll.setVerticalScrollBar(dummyBar);

        final JScrollBar bar1 = scroll.getHorizontalScrollBar();
        JScrollBar bar2 = fixedScroll.getHorizontalScrollBar();
        bar2.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                bar1.setValue(e.getValue());
            }
        });
        scroll.setPreferredSize(new Dimension(400, 100));
        fixedScroll.setPreferredSize(new Dimension(400, 52)); 
        getContentPane().add(scroll, BorderLayout.CENTER);
        getContentPane().add(fixedScroll, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        FixedRowExample frame = new FixedRowExample();
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}
于 2012-08-02T16:12:21.953 に答える