How do I popular a Jtable?

Asked

Viewed 7,351 times

6

I have a very simple java application, which connects to a database, in the console I type the query I want that is passed by parameter to the Query. Now I need to move all this to a graphical interface. Is there any way I can create just one window, and pass the data as it is arranged to the graphical interface?

I have nothing done yet, I have a Jtable just stated, because I did not find any good tutorial on how to do. In my program I have a Jtextarea that receives a query, so I will need to show this table dynamically as soon as I click the Run button. My question is how popular this table.

  • Yes, it is possible. If it is for desktop application you can use both swing and javafx. What is your question exactly?

  • I think I expressed myself badly, my doubt is how popular the table.

  • Do you already have a table? Would it be a Jtable? Could you show some of your code in the part where you have to popular that table?

  • @Math I haven’t done anything yet, yes, I have a Jtable just stated as I haven’t found any good tutorial on how to do it. In my program I have a Jtextarea that receives a query, so I will need to show this table dynamically as soon as I click the Run button.

  • You are using Resultset to search the BD?

  • Yes, I’m using Resultset

Show 1 more comment

1 answer

7


To create and popular a Jtable, you must actually add the data into a Defaulttablemodel and then set it as the table model.

I made an example with comments below:

JTable table = new JTable();
String[] nomesColunas = {"nome", "endereco", "telefone"};
//essa lista terá as linhas da sua JTable, preenchi abaixo apenas como exemplo
List<String[]> lista = new ArrayList<>();
//aqui você fará um while percorrendo seu result set e adicionando na lista
//while(resultset.next()) {
lista.add(new String[]{"Joao", "rua um", "1234-5678"});
lista.add(new String[]{"Henrique", "rua 42", "1122-3344"});
lista.add(new String[]{"Manuel", "av 7 de setembro", "8765-4321"});
//} //fim while
//crie um defaultablemodel com as informações acima
DefaultTableModel model = new DefaultTableModel(
        lista.toArray(new String[lista.size()][]), nomesColunas);
//define o model da sua tabela
table.setModel(model);
//adiciona no contentpane, coloque dentro de um JScrollPane pois caso 
//contrário não aparecerão os nomes das colunas
contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
  • Math, it’s working, but a doubt, I add in the list a string object right? But the resultset returns me an Object, how do I do?

  • @Joãoneto Exempo: lista.add(new String[]{String.valueOf(resultSet.getInt("id")), resultSet.getString("nome"), String.valueOf(resultSet.getBoolean("cadastrado"))}; That is, you take the value according to its type, and specify correctly in the method name, getString takes String, getInt takes Int. And everything that is not String you convert with String.valueOf(). The argument within the getTipo() method must be the column name in your BD.

  • Thanks! It worked out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.