Re: JTables and dynamically adding data



michael.miceli88@xxxxxxxxx wrote:
On Apr 26, 1:46 pm, RedGrittyBrick <RedGrittyBr...@xxxxxxxxxxxxx>
wrote:
michael.micel...@xxxxxxxxx wrote:
Hey,
I have a JTable that I want to add a new column to every time someone
clicks a button. I have been reading about extending the
AbstractTableModel, but I don't how this allows you to add a new
column. The examples online show what happens when a user edits a
table, but I don't want that. I want it add when the user presses a
button outside of the table. Some advice would be appreciated.
Have you tried incrementing the value returned by your getColumnCount(),
making sure getValueAt() now returns the appropriate values then calling
fireTableStructureChanged()?

--
RGB

No, but I will give this a try, and hopefully get back to you by the
end of the day.
Thanks


I couldn't resist trying it.

-------------------------------- 8< ------------------------------
public class TableColumnAdder implements ActionListener {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableColumnAdder();
}
});
}

private MultiplyModel model = new MultiplyModel();

TableColumnAdder() {
JButton button = new JButton("Add Column");
button.addActionListener(this);

JTable table = new JTable(model);

Box box = new Box(BoxLayout.PAGE_AXIS);
box.add(button);
box.add(new JScrollPane(table));

JFrame f = new JFrame("TableColumnAdder");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(box);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
model.addColumn();
}

class MultiplyModel extends AbstractTableModel {

private int columns = 1;

private static final int ROWS = 12;

@Override
public int getColumnCount() {
return columns;
}

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

@Override
public Object getValueAt(int row, int column) {
return row * column;
}

public void addColumn() {
columns++;
fireTableStructureChanged();
}

}
}
-------------------------------- 8< ------------------------------



--
RGB
.



Relevant Pages

  • Re: Sortable Java Tree Table
    ... I have never actually done this, but if I wanted to sort a JTree, ... private Listener listener = new Listener; ... public Object getChild(Object parent, int index) { ... public void valueForPathChanged{ ...
    (comp.lang.java.gui)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • Re: NotSerializable object issue...
    ... private static final NoopOutputStream DUMMY_OUTPUT_STREAM = new ... public SerializableCheckerthrows IOException { ... public void writeObjectthrows IOException { ... private int counter; ...
    (comp.lang.java.programmer)
  • Re: Im needing help with compile errors for GUI enviroment GameOfWar
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)
  • Re: Im needing help with compile errors GameOfWar new compile errors under 2judith
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)