java 获取选中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6082433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:15:08  来源:igfitidea点击:

get the selected row

javaswingjtable

提问by Εφη Κολοκυθ?

Hello i want to select a row and after i click the button i want to display this line!

您好,我想选择一行,单击按钮后,我想显示这一行!

public class TableDemo extends JPanel {

    static List<String[]> rosterList = new ArrayList<String[]>();
    int[] rowIndices;

    public TableDemo() {
        super(new BorderLayout(3, 3));

        final JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        final JButton button = new JButton("Buy it");

        JPanel buttonCenter = new JPanel(new FlowLayout(FlowLayout.CENTER));

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        if (table.getColumnSelectionAllowed()
                            && !table.getRowSelectionAllowed()) {
                            // Column selection is enabled
                            // Get the indices of the selected columns
                            int[] vColIndices = table.getSelectedColumns();
                        } else if (!table.getColumnSelectionAllowed()
                            && table.getRowSelectionAllowed()) {
                            // Row selection is enabled
                            // Get the indices of the selected rows
                            rowIndices = table.getSelectedRows();
                        }
                        if (rowIndices.length > 0) {
                            for (int i = 0; i <= rowIndices.length; i++) {
                                System.out.println(rowIndices[i]);
                            }
                        }
                    }
                });
            }
        });

        buttonCenter.add(button);
        add(buttonCenter, BorderLayout.SOUTH);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane, BorderLayout.CENTER);
        //create a button

        // add a nice border
        setBorder(new EmptyBorder(5, 5, 5, 5));
    }

    class MyTableModel extends AbstractTableModel {

        private String[] columnNames = {
            "Κωδικ??", "Ποσ?τητα", "Τιμ?", "Περιγραφ?", "Μ?γεθο?", "Ρ?τσα"
        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return rosterList.get(row)[col];
        }
    }

    private static void createAndShowGUI() {
        //Create and set up the window.

        JFrame frame = new JFrame("TableDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                creatArr();
                createAndShowGUI();
            }
        });
    }

    private static void creatArr() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("Dogss.txt"));
            String line = br.readLine();

            while (line != null) {
                String[] rowfields = line.split("#");
                rosterList.add(rowfields);
                line = br.readLine();
            }

        } catch (FileNotFoundException e) {
            // can be thrown when creating the FileReader/BufferedReader
            // deal with the exception
            e.printStackTrace();
        } catch (IOException e) {
            // can be thrown by br.readLine()
            // deal with the exception
            e.printStackTrace();
        }
    }
}

回答by Howard

You can get the row number (starting from zero) with

您可以获得行号(从零开始)

table.getSelectedRow()

or with multiple selected rows via

或通过多个选定的行

table.getSelectedRows()