简体   繁体   中英

Data retrieving from JTable

I am trying to get the data from a table in Java. I have been able to get the data but when it is being output, it is concatenated by itself. If the data is "admin" the output is "adminadmin". Below is the code. Can someone tell me what the problem is?

package sample;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.proteanit.sql.DbUtils;

public class Table extends JFrame{
    JTable table;
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    private int row;
    private String name, status;

    private void connect(){
        conn = myconnection.ConnectDb();
    }


    public void mouseClicked(MouseEvent e) {
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
        int sel = table.getSelectedRow();
        name = table.getModel().getValueAt(sel, 0).toString();
        System.out.print(name);
    }
});
    }

    private void UpdateJTable(){

        String sql = "select firstname, status from tblmember";

        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }


    public Table(){
        setLayout(new FlowLayout());

        String [] columnName={"Name", "Status"};
        Object [][] data={
            {null, null},
            {null, null}
        };

        table = new JTable(data, columnName);
        table.setPreferredScrollableViewportSize(new Dimension(400,50));
        table.setFillsViewportHeight(true);

        JScrollPane sp = new JScrollPane(table);
        add(sp);
    }

    public static void main(String[] args){
        Table gui = new Table();

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(700, 500);
        gui.setVisible(true);
        gui.setTitle("AAAAAAAA");
        gui.connect();
        gui.UpdateJTable();
        gui.mouseClicked(null);

    }
}

It's concatenated because the selection event is triggered twice, and you use System.out.print() to display the selection.

You should probably ignore the event if its getValueIsAdjusting() method returns true.

Also, be careful that the selected row index returned is not necessarily the same index as the table model index: if your table is sorted, both indexes will differ. Use JTable's convertRowIndexToModel() before getting the data from the model.

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