获取选定的行

发布于 2024-11-08 12:37:57 字数 4424 浏览 0 评论 0原文

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

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();
        }
    }
}

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();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2024-11-15 12:37:57

行号(从零开始)

table.getSelectedRow()

您可以通过以下方式获取具有或多个选定行的

table.getSelectedRows()

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

table.getSelectedRow()

or with multiple selected rows via

table.getSelectedRows()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文