-1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Eskipo
  • 51
  • 4
  • 10
  • _I don't really know how to loop through the columns and rows in the database_ and you did **what** to find out? There are tons of similar questions (with nice answers) at this site alone ... – kleopatra Jan 12 '14 at 09:19

2 Answers2

1

JDBCAdapter, which extends AbstractTableModel, is a good example for study. It may be found in samples/demo/jfc/TableExample, found among the Java SE Development Kit 7u45 Demos and Samples Downloads.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

//Global Declaration

private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header

//Display info to JTable

    data = get();

    //create header for the table
    header = new Vector<String>();
    header.add("Column1"); 
    header.add("Column2");
    ...
    model=new DefaultTableModel(data,header);
    table = new JTable(model);

This will help you to get data from database

get(){
Vector<Vector<String>> doublevector = new Vector<Vector<String>>();

Connection conn = dbConnection();//Your Database connection code
PreparedStatement pre1 = conn.prepareStatement("select * from Table");

ResultSet rs1 = pre1.executeQuery();
while(rs1.next())
{
Vector<String> singlevector = new Vector<String>();
singlevector.add(rs1.getString(1)); 
singlevector.add(rs1.getString(2)); 
....
doublevector.add(singlevector);

}

return doublevector;
}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59