[英]Getting a row of names from the first column of JTable
I have a JTable
with the column names " Names "
, " Quantity "
and " Unit "
. 我有一个JTable
,其列名为" Names "
, " Quantity "
和" Unit "
。 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. 所以我需要获得一列的整个行并将其全部字符串在一起,因为我需要将其存储在mySql中并且我已经将其全部设置为String。 Any idea how i can do this ? 知道我该怎么做吗? My code are as follows : 我的代码如下:
JTable Code: 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");
}
Getting the values and for storing : 获取值并存储:
for(int i = 1; i<= i ; i++){
ingredients = table.getName();
}
This is for loop is wrong and it does not work because i have a constructor to take in Ingredients but because it is inside the loop, it cannot take it in. Any suggestions please ? 这是for循环是错误的,它不起作用,因为我有一个构造函数可以接收成分,但是因为它在循环内部,所以不能接收它。有任何建议吗? Thank you. 谢谢。
Constructor : 构造函数:
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. 所以我需要获得一列的整个行并将其全部字符串在一起,因为我需要将其存储在mySql中并且我已经将其全部设置为String。
Want to do so, try this. 想要这样做,尝试这个。 Here I am getting result into ArrayList
and String
, as I am commented ArrayList
you can avoid it. 在这里,我得到的结果是ArrayList
和String
,因为我评论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 字符串= MoniJhonMax
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.