Reputation: 3
I have two tables and I need to add the value from specific cell in table_3 to table_4 .. but its not working. Any help please?
Code :
JTable table_3 = new JTable(TableModel2);
final JTable table_4 = new JTable(TableModel3);
JButton btnAdd = new JButton("add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table_3.getSelectedRow();
int column = table_3.getSelectedColumn();
Object selectedPlanet = TableModel2.getValueAt(row, column);
int columnContainingPlanets = 0;
for (int index = columnContainingPlanets; index < TableModel3.getRowCount();index ++){
Object value = TableModel3.getValueAt(index, columnContainingPlanets) ;
if (value == selectedPlanet) {
JOptionPane.showMessageDialog(panel_10,"This item was selected ,Please select another item");
return; }}
TableModel3.addRow(new Object[] { selectedPlanet });
}
});
Upvotes: 0
Views: 276
Reputation: 324088
Still waiting for your Minimal, Complete, and Verifiable Example(MCVE)
. What makes you think the problem is in the code you posted? Until a problem is solved every question should have a simple example demonstrating the problem which proves that you tried to simplify problem to better debug it.
What is the point of posting the error message without indicating the line of code causing the problem? Again another reason to post the MCVE
. You don't know until the question is answered what information is needed to solve the problem.
Without all the information all we can do is guess.
As a newbie to the forum you get one guess. Future questions will require an MCVE
My guess is that the "tableModel3" doesn't have any columns, so you can't add a row to the model containing a single column of data.
So the model should be created with code something like:
String[] columnNames = { "Planets" };
DefaultTableModel tableModel3 = new DefaultTableMNodel(columnNames, 0);
Upvotes: 1