Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one way to use <a href="http://tips4java.wordpress.com/2009/07/12/table-button-column/" rel="noreferrer"><code>ButtonColumn</code></a>.</p> <pre><code>public class TableTest extends JFrame { public TableTest() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(new TestModel()); table.getColumnModel().getColumn(1).setPreferredWidth(3); table.getColumnModel().getColumn(2).setPreferredWidth(3); this.add(new JScrollPane(table)); Action increase = new AbstractAction("+") { @Override public void actionPerformed(ActionEvent e) { JTable table = (JTable) e.getSource(); int row = Integer.valueOf(e.getActionCommand()); TestModel model = (TestModel) table.getModel(); model.increment(row, 0); } }; ButtonColumn inc = new ButtonColumn(table, increase, 1); Action decrease = new AbstractAction("-") { @Override public void actionPerformed(ActionEvent e) { JTable table = (JTable) e.getSource(); int row = Integer.valueOf(e.getActionCommand()); TestModel model = (TestModel) table.getModel(); model.decrement(row, 0); } }; ButtonColumn dec = new ButtonColumn(table, decrease, 2); pack(); } public static void main(String[] args) { new TableTest().setVisible(true); } } class TestModel extends AbstractTableModel { List&lt;TestRecord&gt; records = new LinkedList&lt;TestRecord&gt;(); private static class TestRecord { private int val = 0; } public void increment(int row, int col) { records.get(row).val++; fireTableCellUpdated(row, 0); } public void decrement(int row, int col) { records.get(row).val--; fireTableCellUpdated(row, 0); } public TestModel() { records.add(new TestRecord()); records.add(new TestRecord()); } @Override public Class&lt;?&gt; getColumnClass(int col) { if (col == 0) { return Integer.class; } else { return ButtonColumn.class; } } @Override public boolean isCellEditable(int row, int col) { return true; } @Override public int getColumnCount() { return 3; } @Override public int getRowCount() { return records.size(); } @Override public Object getValueAt(int row, int col) { if (col == 0) { return records.get(row).val; } else if (col == 1) { return "+"; } else { return "-"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload