Another alternative is to use an event that monitors selections made in the table:
suaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if(!lsm.isSelectionEmpty()){
//aqui você insere a ação que quer fazer quando
//uma linha for selecionada ou uma seleção for
//alterada na tabela
}
}
}
And don’t forget to convert the table’s Dice to the model’s, to avoid indexofboundException
if you insert filters into the table.
int rowSel = suaTable.getSelectedRow();//pega o indice da linha na tabela
int indexRowModel = suaTable.getRowSorter().convertRowIndexToModel(rowSel);//converte pro indice do model
If you don’t have a method that returns an object from your model, you can use the method getValueAt(indexRow, indexColumn)
to take the values of each column of the row and populate its object. The method would look a little less like this:
suaTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if(!lsm.isSelectionEmpty()){
int rowSel = suaTable.getSelectedRow();//pega o indice da linha na tabela
int indexRowModel = suaTable.getRowSorter().convertRowIndexToModel(rowSel);//converte pro indice do model
//aqui você adapta conforme sua tabela e seu objeto,
//alterando o indice da coluna
seuTable.getModel().getValueAt(indexRowModel, indiceDaColuna);
}
}
});
Another tip would be to use JDialog
for secondary screens of your application, and use JFrame
only in the main, this way you can control the visibility and easily rescue information between these windows, even after you have given dispose
in the daughter windows. At this link you get information about the use of JDialogs
, its use is quite similar to the JFrames
, with the addition of making it a modal window.
In this question has an example similar to his.
Other references:
Update a Jtable that is in a Jframe from a Jdialog
How to call a Jframe from another Jframe with different classes
Say the code of what you’ve done so far.
– user28595
You have created an abstractTableModel or is using Default?
– LocalHost