How can I get the text in a JTable?

ⅰ亾dé卋堺 提交于 2020-06-23 17:41:08

问题


For example I have a Table that I want to get text that's in the first column and store it in an ArrayList.


回答1:


Java Tables often use the TableModel interface for storage.

You can get the a particular value via:

myJTable.getModel().getValueAt(rowIndex, columnIndex);

More on that: Sun's Swing Table Tutorial




回答2:


public static void main(String[] args)
{
    try
    {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cp = frame.getContentPane();
        cp.setLayout(new FlowLayout());

        final JTable tbl = new JTable(new String[][]{{"c1r1", "c2r1"}, {"c1r2", "c2r2"}}, new String[]{"col 1", "col 2"});

        cp.add(tbl);
        cp.add(new JButton(new AbstractAction("click")
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                List<String> colValues = new ArrayList<String>();

                for (int i = 0; i < tbl.getRowCount(); i++)
                    colValues.add((String) tbl.getValueAt(0, i));

                JOptionPane.showMessageDialog(frame, colValues.toString());
            }
        }));

        frame.pack();
        frame.setVisible(true);
    }
    catch (Throwable e)
    {
        e.printStackTrace();
    }
}



回答3:


You need to go through the JTable's TableModel, accessible through the getModel() method. That has all the information you need.




回答4:


There is everything you need here!



来源:https://stackoverflow.com/questions/1795936/how-can-i-get-the-text-in-a-jtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!