java - 从 JTable 的第一列获取一行名称

标签 java string jtable

我有一个 JTable ,其列名称为 "Names ""Quantity ""Unit " 。 我正在编写一个程序,您可以在其中获取成分名称。 所以我需要获取一列的整行并将其全部串在一起, 因为我需要将它存储在 mySql 中,并且我已经将其全部设置为 String。 知道我该怎么做吗? 我的代码如下:

JTable 代码:

DefaultTableModel model = (DefaultTableModel)table.getModel();  
if(!txtQty.getText().trim().equals("")){  

     model.addRow(new Object[]{ingCB.getSelectedItem().toString(),txtQty.getText(),unitCB.getSelectedItem().toString()});  
}else{  

     JOptionPane.showMessageDialog(null,"*Quantity field left blank");  
}  

获取值并存储:

for(int i = 1; i<= i ; i++){  
     ingredients = table.getName(); 
}  

这是 for 循环是错误的,它不起作用,因为我有一个构造函数来接收成分,但因为它位于循环内部,所以它无法接收它。 请问有什么建议吗?谢谢。

构造函数:

Food e2 = new Food(Name, Description, priceDbl, Image, Category, Ingredients, promotion );

e2.createFood();

最佳答案

I'm coding a program where you get the ingredients names. So i need to get the whole row of one column and String it all up together, because i need to store it in mySql and i have already set it all as String.

想要这样做,试试这个。在这里,我将结果放入 ArrayListString 中,正如我所注释的 ArrayList 你可以避免它。

public class TableValuePrint extends JFrame implements ActionListener{
    private final JButton print;
    private final JTable table;
    private String str="";

    public TableValuePrint() {
        setSize(600, 300);
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", "Pass"},
            {"Jhon", "ewrewr", "Fail"},
            {"Max", "zxczxc", "Pass"}
        };

        table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);
        JPanel tablePanel = new JPanel();
        tablePanel.add(tableSP);
        tablePanel.setBackground(Color.red);
        add(tablePanel);
        setTitle("Result");

        setSize(1000,700);

        print=new JButton("Print");  
        JPanel jpi1 = new JPanel();  
        jpi1.add(print);  
        tablePanel.add(jpi1,BorderLayout.SOUTH);      

        print.addActionListener(this);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                TableValuePrint ex = new TableValuePrint();
                ex.setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
       if(ae.getSource()==print){
//         ArrayList list = new ArrayList();
for(int i = 0;i<table.getModel().getRowCount();i++)
{
//list.add(table.getModel().getValueAt(i, 0)); //get the all row values at column index 1
str=str+table.getModel().getValueAt(i,0).toString();
}
//System.out.println("List="+list);
System.out.println("String="+str);
        }
    }
}
<小时/>

输出 output

String=MoniJhonMax

关于java - 从 JTable 的第一列获取一行名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661055/

相关文章:

list 中的 Java 类路径未启动库

java - DefaultTtableModel 上的单选按钮

java - 无法显示选项卡式 JTable 的表头

java - 编辑 JTable 单元格时更改其字体

java - 通用 lambda 不适用于参数

java - 在j2me中哪里可以看到derby数据库中存储的数据

java - 执行捕获异常中的前面步骤

java - 替换字符串中包含数字或特殊符号的单词

json - 在redis中将结构存储为字符串

java - 正则表达式不拆分 [ 如果字符串位于 ""之下