Right now I just use JList
, but it looks much better if the data is shown in a JTable
.
This is the GUI class:
public class Table extends JFrame {
private JPanel contentPane;
DefaultTableModel tableModel = new DefaultTableModel();
private JTable table;
private Controller controller = new Controller();
private static Vector <Vector<String>> data = new Vector <Vector<String>>();
/**
* Launch the application.
*/
public Table() throws SQLException {
Vector <String> columnNames = new Vector <String>();
ResultSet rs2 = controller.getPerson();
while (rs2.next()){
columnNames.add(rs2.getString(1));
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//add tableModel into table
table = new JTable(tableModel);
contentPane.add(table, BorderLayout.CENTER);
//Create JButton
JButton showBtn = new JButton("Show");
showBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
ResultSet rs = controller.getPerson();
while (rs.next()){
Vector <String> vstring = new Vector <String>();
vstring.add(rs.getString(1));
data.add(vstring);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
contentPane.add(showBtn, BorderLayout.SOUTH);
}}
When the button is clicked the data should be shown in the JTable
. I don't really know how to loop through the columns and rows in the database and then add this info to the JTable
.