How to Mount a Jtable with Resultset?

Asked

Viewed 1,510 times

-1

I have an example:

Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },{ "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
Object columnNames[] = { "Posição", "Nome", "Pontuação" };
JTable table = new JTable(rowData, columnNames);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(255, 400);
frame.setVisible(true);

I need to do the rowData receive a ResultSet, I don’t master java, and so far while I couldn’t do the rowData receive my ResultSet

if(rs != null) {
    while(rs.next()) {
        // rowData = ?? 
    }
}
  • want to get the data that are in jtabel?

  • no, I want rowData to receive my rs @jsantos1991

  • 1

    try this: DefaultTableModel tm = (DefaultTableModel) table.getModel(); then use the tm to insert type: tm.addRow(new Object[]{new String("aaaaa"), new String("bbbb"), new String("ccccc"), new String("ddddd")});

1 answer

-1


I got it that way:

String[] colunasTabela = new String[]{ "Posição", "Nome", "Pontuação" };  
DefaultTableModel modeloTabela = new DefaultTableModel(null,colunasTabela);

if(rs != null) {
    while(rs.next()) {
        modeloTabela.addRow(new String[] {  
                rs.getInt("ranking"),  
                rs.getString("nome"),  
                rs.getInt("score")
            }); 
    }
}

JTable table = new JTable();
table.setModel(modeloTabela);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(255, 400);
frame.setVisible(true);
  • Just a hint look for tableModel, a good example is http://devsv.wordpress.com/2012/07/08/como-implementar-um-tablemodel/

Browser other questions tagged

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