سؤال

i want to add or insert column from different methods into one table.. cant explain it clearly but i show my codes to you to understand..for example.

(....)

DefaultTableModel dtm = new DefaultTableModel();
JTable table = new JTable();


Constructor(){

     table.setModel(dtm);

     (.....)

}


public void methodOne(){

        String id = num.getText();

        rs = stat.executeQuery("SELECT * FROM payments;");

        Vector<String> header = new Vector<String>();

        header.add("PAYMENT"); 
        header.add("AMOUNT");
        header.add("MODIFIER");
        header.add("DATE MODIFIED");

        Vector<Vector<Object>> data = new  Vector<Vector<Object>>();

        while(rs.next()) {

            Vector<Object> row = new Vector<Object>();

            row.add(rs.getString("description"));
            row.add(rs.getString("amount")); 
            row.add(rs.getString("remarks")); 
            row.add(rs.getString("date"));

            data.add(row);

        } // loop

        dtm.setDataVector(data , header);
        JScrollPane scrollPane = new JScrollPane(table);

        scrollPane.setBounds(0,0,490,250);
        panel.add(scrollPane);
        validate();

}

public void methodTwo(){

     (.....)

     rs = stat.executeQuery("SELECT * FROM record where idNum ='"+id+"';");

        while(rs.next()){

            Vector<Object> row = new Vector<Object>();

            row.add(rs.getString("description"));
            row.add(rs.getString("amount")); 
            row.add(rs.getString("remarks")); 
            row.add(rs.getString("date"));

            data.add(row);

        } // while



}

those value inside row are the value i want to add on my table, i dont have any idea on how to id.. i want it to be like this:

first when you run the java it will autoumatically create a table

http://i1023.photobucket.com/albums/af355/guiacustodio/javaaaaaaaaaaaaaaaaaaaaaaa_zpse9a22225.jpg

i have a button and textfield i enter number on the textfield i.e

[PAY BUTTON] TextField:[__100]

i clicked the button and this is what will happen:

http://i1023.photobucket.com/albums/af355/guiacustodio/javaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_zps43879eab.jpg

هل كانت مفيدة؟

المحلول

First of all, the data Vector you are using is defined inside methodOne so the same data is not accesible via methodTwo.

Secondly it is not because there is data being added to data in a tablemodel that the table will refresh, you have to call one of the methods that trigger an refresh event in the gui, normally one calls the method fireTableChanged on the tablemodel after you added data.

Thridly: there is an interesting library called GlazedLists that handles a lot of these things automatically !

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top