Creating a custom TableModel with multiple column headers and row headers

前端 未结 2 1358
醉酒成梦
醉酒成梦 2020-12-11 23:57

I\'m attempting to create a JTable that looks like the mockup below:

\"custom

The green corner

2条回答
  •  情歌与酒
    2020-12-12 00:39

    Input is limited to 20x20 so including the headers that's 22x22.

    Also consider a JScrollPane containing a JPanel having GridLayout and containing 22x22 instances JLabel, or a suitable subclass. This scales easily to several thousand cells.

    Addendum: If the need arises, CellRendererPane makes a good flyweight renderer, as suggested here.

    If you go with JTable for rendering scalability,

    1. This is no abuse; it is exactly how TableModel is intended to be used. TableModel models a rectangular matrix of whatever you decide. JTable is just an (efficiently rendered) view of that model.

    2. I prefer AbstractTableModel, shown here, because Vector is rarely the desired data structure. Use whatever container makes your indexing most convenient. DefaultTableModel is handy and serves as a guide to extending AbstractTableModel. In particular, you'll need a setValueAt().

      @Override
      public void setValueAt(Object aValue, int row, int col) {
          ... // update your data structure
          this.fireTableCellUpdated(row, col); // notify the view
      }
      

提交回复
热议问题