JTable getSelectedRow does not return the selected row index

前端 未结 2 2052
孤城傲影
孤城傲影 2020-11-27 08:43

I try to get data in the row that is selected but getSelectedRow() doesn\'t work. Actually, I used that method in the other class, but it works in there. When I

相关标签:
2条回答
  • 2020-11-27 09:16

    Look to your code

    private void jbInit() throws Exception {
        ...
        jScrollPane1.getViewport().add(jTable1, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jScrollPane1, null);
        jTable1 = new JTable (model);
        ...
    

    You add jTable1 to the JScrollPane at first and only then you create jTable1. It's incorrect way. jTable1 variable doen't linked now with table you place at the form. I think it's cause of your problem.

    Move creation of the jTable1 before adding in into JScrollPane.

    ...
    jTable1 = new JTable (model);
    jScrollPane1.getViewport().add(jTable1, null);
    this.getContentPane().add(jButton2, null);
    this.getContentPane().add(jButton1, null);
    this.getContentPane().add(jScrollPane1, null);
    ...
    
    0 讨论(0)
  • 2020-11-27 09:18

    As your other example shows, getSelectedRow() does work, so you're going to have to debug your code. Comparison with the sscce below may suggest way forward. Both the ActionListener and the ListSelectionListener show the selected row.

    image

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.DefaultTableModel;
    
    /**
     * @see http://stackoverflow.com/q/12301923/230513
     */
    public class TableSelection extends JPanel {
    
        private static final String SHOW = "Show";
        private DefaultTableModel model = new DefaultTableModel();
        private JTable table = new JTable(model);
        private JButton button = new JButton(new AbstractAction(SHOW) {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(table.getSelectedRow());
            }
        });
    
        public TableSelection() {
            model.addColumn("Column");
            for (int i = 0; i < 16; i++) {
                model.addRow(new Object[]{i});
            }
            table.setPreferredScrollableViewportSize(new Dimension(160, 100));
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
    
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        button.setText(SHOW + " " + table.getSelectedRow());
                    }
                }
            });
            this.add(new JScrollPane(table));
            table.setRowSelectionInterval(3, 3);
        }
    
        private void display() {
            JFrame f = new JFrame("TableSelection");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this, BorderLayout.CENTER);
            f.add(button, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TableSelection().display();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题