Using an empty column as a separator in JTable

I am trying to use an empty column as a separator between pairs of columns in JTable . Here is the picture and code for what I have so far. I know that I can change the look with a custom TableCellRenderer . Before I go this route, is there a better way to do this? Any ideas are welcome.

TablePanel.png

 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; } } } 
+4
source share
4 answers

On the issue with this approach, this is that the user will need to “move” the delimiter column. You can use the Table Tabbing clause to make it more convenient.

Or if tabulation between two tables is not important, then perhaps you can use two tables and place any separator you want between the two. A selection model can be shared if required.

Edit:

As I said above, sharing models are easier than writing custom listeners. To keep scrolling in sync, the code will look like this:

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

You can also do the same with the selection model so that the line highlight is in sync.

+8
source

I kind of combined the two answers: I used two tables with one scrollbar. This works with sorting and actually simplifies the model. The tab doesn't matter, but comparing "A" and "B" does matter. I think I was trying to solve the "look" problem in the "model". I made this a separate answer because I will be grateful for any criticisms.

Tablepanel

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

Not knowing what you want to show in this table, it is difficult to say whether you have chosen a good solution or not.

Regarding this decision. This column does not look like a separator. Draw it in gray / another color or a white color separator header cell.

But in any case, I would prefer JScrollPane + two tables inside it instead of this solution.

+2
source

Please take a look at the answer to this question, a nice new suggestion was suggested there: Column delimiters in JTable or JXTable

+1
source

Source: https://habr.com/ru/post/1306573/


All Articles