Using an empty column as a divider in a JTable

后端 未结 4 1108
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 13:17

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

4条回答
  •  情书的邮戳
    2020-11-29 14:04

    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());
    }
    

提交回复
热议问题