user2465510 user2465510 - 3 months ago 24
Java Question

Inserting a row into a JTable

EDIT: Problem Solved. I had accidentally put a

return
inside of the
if
statement instead of
break
. The function was exiting before altering the
DefaultTableModel
.

StockNameArr
is an array of all stock symbols in the table (which are alphabetically arranged), arranged itself in alphabetical order.

This class represents a
JTable
with two clickable columns. The first column only prints to
stdout
right now, and that works fine. The second one deletes the row it is in and arranges the name of each stock in alphabetical order in a
JComboBox
. This also works correctly as far as I can tell.

When one of the items in the
JComboBox
is clicked, it should delete the item, create a
String[8]
(matching the other columns of 8 rows), insert the stock name from the item that was clicked, and insert the array as a row at the same place it was previously deleted from.

What actually happens, though, is that the item is deleted from the
JComboBox
, while the new row is never inserted into the table. I've tried calling:

model.insertItemAt(...);
thisTable.setModel(model);


But it didn't do anything. Thinking that maybe
model
or
thisTable
were't set up correctly, I referenced
thisTable
to this as a class variable, and tried to insert a row into the
TableModel
withouth even creating a new variable for it in the inner class. Still, no luck.

Can anyone help me?

import java.awt.event.Action/Event;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JMainTable extends JTable {
JMainTable thisTable = this; //Represents this class in inner classes
final String[] stockNames;
DefaultTableModel model;
JComboBox<String> dropdown;
ArrayList<String> dropdownStocks = new ArrayList<String> ();
int rowChangeOffset = 0;
public JMainTable(DefaultTableModel tableModel, String[] stockNameArr, JComboBox<String> combobox) {
super(tableModel);
// thisTable = this;
stockNames = stockNameArr;
addMouseListener(new mainTableListener());
model = tableModel;

dropdown = combobox;
dropdown.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
//^ prevents ActionListener from firing when scrolling through list with arrow keys

dropdown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!e.getActionCommand().equals(ActionEvent.KEY_EVENT_MASK)){
System.out.println("Clicked");
JComboBox<String> source = (JComboBox<String>) e.getSource();
String stockString = (String) source.getSelectedItem();
int index = dropdownStocks.indexOf(stockString);
try {
source.removeItemAt(index);
dropdownStocks.remove(index);
} catch(ArrayIndexOutOfBoundsException arrEx) {}
int indexInStockNames = 0; //for compiler
for(int i = 0; i<stockNames.length; i++) {
if(stockNames[i].equals(stockString)) {
indexInStockNames = i;
return;
}//end of If
}//End of For
String[] replacementRow = new String[8];
replacementRow[0] = stockString;
//model.insertRow(indexInStockNames, replaceRow);
((DefaultTableModel) thisTable.getModel()).insertRow(indexInStockNames, replacementRow);
if(index>=0) {//Prevents offset from being changed
//when user clicks on empty JComboBox
rowChangeOffset--;
}

}
}
});
}

class mainTableListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent mouseevent) {
int column;
if((column = thisTable.getSelectedColumn()) == 6) {
int row = thisTable.getSelectedRow();
System.out.println("Button for " + stockNames[row + rowChangeOffset] + " working.");//TESTING
//NOIMPLEMEN
} else if((column = thisTable.getSelectedColumn()) == 7) {
int row = thisTable.getSelectedRow();
model.removeRow(row);
try{
dropdownStocks.add(stockNames[row + rowChangeOffset]);
} catch(Exception adsf) {adsf.printStackTrace(); System.out.println(stockNames[row+rowChangeOffset]);}
int index;
try {
Collections.sort(dropdownStocks);
index = dropdownStocks.indexOf(stockNames[row+rowChangeOffset]);
} catch(Exception ex) {/*dropdownStocks may be empty*/
index=0;
}

dropdown.insertItemAt(stockNames[row+rowChangeOffset], index);
rowChangeOffset = rowChangeOffset + 1;
}
}
}
}

Answer

try this, it will help to add a row to your model that is linked to your table

 model.addRow(new Object[]{objec1,object2}); //param to count of columns to table
 jTable1.setModel(modelo);
/*You are not required if in the beginning did something like this
modelo = (DefaultTableModel)jTable1.getModel();*/