Using an empty column as a divider in a JTable

不想你离开。 提交于 2019-11-26 12:33:25

问题


I\'m trying to use an empty column as a divider between pairs of columns in a JTable. Here\'s a picture and code for what I have so far. I know I can change the look using a custom TableCellRenderer. Before I go down that road, is there a better way to do this? Any ideas appreciated.

import javax.swing.*;
import javax.swing.table.*;

public class TablePanel extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame(\"TablePanel\");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.add(new TablePanel());
                f.pack();
                f.setVisible(true);
            }
        });
    }

    public TablePanel() {
        TableModel dataModel = new MyModel();
        JTable table = new JTable(dataModel);
        table.getColumnModel().getColumn(MyModel.DIVIDER).setMaxWidth(0);
        JScrollPane jsp = new JScrollPane(table);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.add(jsp);
    }

    private static class MyModel extends AbstractTableModel {

        private static final int DIVIDER = 2;
        private final String[] names = { \"A1\", \"A2\", \"\", \"B1\", \"B2\" };

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

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

        @Override
        public String getColumnName(int col) {
            if (col == DIVIDER) return \"\";
            return names[col];
        }

        @Override
        public Object getValueAt(int row, int col) {
            if (col == DIVIDER) return \"\";
            return (row + 1) / 10.0;
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == DIVIDER) return String.class;
            return Number.class;
        }
    }
}

回答1:


On problem with this approach it that the user will need to "tab over" the divider column. You could use the Table Tabbing suggestion to make it more user friendly.

Or if tabbing between the two tables isn't important, then maybe you can use use two tables and put whatever divider you want betweeen the two. The selection model can shared if required.

Edit:

As I suggested above sharing models is easier than writing custom listeners. To keep the scrolling in sync the code would be:

jspa.getVerticalScrollBar().setModel( jspb.getVerticalScrollBar().getModel() );

You can also do the same with the selection model so that highlighting of rows is in sync.




回答2:


I kind of combined both answers: I used two tables that share one's scrollbar. This works with sorting, and it actually makes the model simpler. Tabbing doesn't matter, but comparing "A" and "B" does. I think I was trying to solve a "view" problem in the "model". I made this a separate answer, because I'd appreciate any critical comments.

public TablePanel() {
    this.setLayout(new GridLayout(1, 0, 8, 0));

    JTable tableA = new JTable(new MyModel("A"));
    tableA.setPreferredScrollableViewportSize(new Dimension(200, 400));
    final JScrollPane jspA = new JScrollPane(tableA);
    jspA.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    this.add(jspA);

    JTable tableB = new JTable(new MyModel("B"));
    tableB.setPreferredScrollableViewportSize(new Dimension(200, 400));
    final JScrollPane jspB = new JScrollPane(tableB);
    jspB.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    this.add(jspB);

    tableA.setSelectionModel(tableB.getSelectionModel());
    jspA.getVerticalScrollBar().setModel(jspB.getVerticalScrollBar().getModel());
}



回答3:


Without knowing what do you want to show in this table it's hard to tell whether you've selected good solution or not.

Regarding this solution. This column does not seem like a divider. Paint it with gray/another color, or paint divider header cell in white.

But anyway I'd prefer JScrollPane + two tables inside it instead of this solution.




回答4:


Please have a look at the answer of this question, some nice new suggestion was given there: Column dividers in JTable or JXTable



来源:https://stackoverflow.com/questions/2614457/using-an-empty-column-as-a-divider-in-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!