0
I need to call a method that shows the user any message.
This method should be called at the exact moment the user finishes editing a Jtable column of type Integer and the user has not entered an integer number.
That is, the method should be called when the column turns 'red'.
Below I’ll put an example image that the article used in one of its posts, just to illustrate when the event should be called.
Below is the code I’m developing.
public class Dados {
/**
* @return the coluna0
*/
public String getColuna0() {
return coluna0;
}
/**
* @param coluna0 the coluna0 to set
*/
public void setColuna0(String coluna0) {
this.coluna0 = coluna0;
}
/**
* @return the colunaInteiro
*/
public Integer getColunaInteiro() {
return colunaInteiro;
}
/**
* @param colunaInteiro the colunaInteiro to set
*/
public void setColunaInteiro(Integer colunaInteiro) {
this.colunaInteiro = colunaInteiro;
}
private String coluna0;
private Integer colunaInteiro;
}
.
public class TesteTableModel extends AbstractTableModel {
private String colunas[] = {"Coluna0", "colunaInteiro"};
private List<Dados> dados;
private final int COLUNA_0 = 0;
private final int COLUNA_INTEIRO = 1;
public TesteTableModel(List<Dados> dados) {
this.dados = dados;
}
//retorna se a célula é editável ou não
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
//retorna o total de itens(que virarão linhas) da nossa lista
@Override
public int getRowCount() {
return dados.size();
}
//retorna o total de colunas da tabela
@Override
public int getColumnCount() {
return colunas.length;
}
//retorna o nome da coluna de acordo com seu indice
@Override
public String getColumnName(int indice) {
return colunas[indice];
}
//determina o tipo de dado da coluna conforme seu indice
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case COLUNA_0:
return String.class;
case COLUNA_INTEIRO:
return Integer.class;
default:
return String.class;
}
}
//preenche cada célula da tabela
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Dados dados = this.dados.get(rowIndex);
switch (columnIndex) {
case COLUNA_0:
return dados.getColuna0();
case COLUNA_INTEIRO:
return dados.getColunaInteiro();
default:
return null;
}
}
}
.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class GuiPrincipal extends JFrame {
private JTable table;
List<Dados> dados = new ArrayList<Dados>();
public GuiPrincipal(JTable table) throws HeadlessException {
this.table = table;
}
public void addDadosInDados() {
Dados dado = new Dados();
dado.setColuna0("Dado qualquer");
dado.setColunaInteiro(1);
dados.add(dado);
}
public GuiPrincipal() {
setLayout(new FlowLayout());
setSize(new Dimension(700, 300));
setLocationRelativeTo(null);
setTitle("Exemplo JTable");
addDadosInDados();// add dados em dados
table = new JTable(new TesteTableModel(dados));
table.setShowVerticalLines(false);
table.setIntercellSpacing(new Dimension(0, 0));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
// este é o método onde é executado nosso programa
public static void main(String[] args) {
GuiPrincipal gui = new GuiPrincipal();
gui.setVisible(true);
}
}
How do I fire a method at this very moment?
Vinicius, this code is not compileable. Test it and check the error, so it is possible to test.
– user28595
In fact the code works just tested, only it was not separated the classes in the question and without the "package with the location of the file in each one", I changed and I will test the response code.
– Vinicius Silva
It only works in your IDE. I pasted all classes separately and the code does not compile, as there are missing methods in the Data class. That’s why you should create a separate project when creating one [mcve].
– user28595
Vinicius, Oce changed the doubt to something completely different, so I reversed the question. It is therefore important to provide executable code, so that the answers are according to the question.
– user28595
I took the same code and put it into a whole new application, tested it in the Netbeans IDE and the Intelij IDE. In both the code ran. What methods didn’t work when you tested them? How did you run the code, if you tell me try to run it the same way and thus fix these errors.
– Vinicius Silva
Note the use of the Data class within the Main Guideway. You are calling the add method, which does not exist in this class. Anyway, the possible solutions are in the answer, there are two ways, if one does not serve, surely the other will serve, because there are no other ways.
– user28595
The jFrame Add? Anyway, that wasn’t the question.
– Vinicius Silva
Vinicius, the ways to do what you want are in the answer, I ask you to read again, because I suggested 2 different ways, not just the one you used first.
– user28595