简体   繁体   中英

Read Data from JTable

I need to get the BookID from the BookInfo table. When I press on the ADD Button, Reservation window should pop out and have the BookID, Issued Date and Return Date written in the Table. So I need to pass BookID as an Object to Reservation.

I tried creating a new instance of book info, or pass a string to reservation...

//for BookInfo:
public void addRow(Object[] objToAdd)
    {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(objToAdd);
    }
...

        JButton btnAdd = new JButton("Add");
        btnAdd.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, new Color(0, 0, 0), new Color(0, 0, 0)));
        btnAdd.setOpaque(true);
        btnAdd.setFont(new Font("American Typewriter", Font.PLAIN, 18));
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Reservation.reserve(/*Object bookID*/);
            }
        });
String[] columnNames = {" Book ID", " Title ", " Author", " Genre", " Date Published ", " Availability"}; // table layout

//for Reservation:
public void reserve(Object[] bookID)
    {
        String issuedDate = new SimpleDateFormat("yyyy.MM.dd").format(new Date());

        Calendar c = Calendar.getInstance();
        c.setTime(new Date()); 
        c.add(Calendar.DATE, 30); 
        String returnDate = new SimpleDateFormat("yyyy.MM.dd").format(c);


        String[]resInfo = {"Book ID" , issuedDate, returnDate};
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(resInfo);
    }

I'm not sure I understand what your user interface looks like, what behavior you want or what you have implemented currently. Please try to be more clear in your descriptions and provide all relevant code when asking a question; it cannot be derived from the code when and where the JButtons are created and what information you have in that scope, which is some of the most crucial information needed to answer your question.

Assuming that you render the table by iterating over the table model and create a button for every row then you already have the BookID in the actionListener's scope as the value of one of the columns. You simply need to add it to your listener code.

A similar alternative approach involves using the button's actionCommand to associate String data with a button. You can then retrieve this information from the ActionEvent that the actionPerformed method is called with. Here's one example . But I don't see any reason to use this approach here. It just adds complexity, the solution above is more simple and natural.

If I am wrong in my assumption on how you create the table you could attach a mouseListener instead of an actionListener, like here , so you can get the row from the event and access the relevant column.

You can use bellow code to read all jtable data

     DefaultTableModel model =  (DefaultTableModel) jTable1.getModel();
     Vector data = model.getDataVector();

     for (Object data1 : data) {
         System.out.println(data1);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM