目前,我有一个在构建时没有行值的表,在单击add
按钮后添加行。当我单击add
按钮时,它会向表中添加一行,并且cells
表示用户可以键入值。然后当我单击save
按钮时,它会将每个col值存储到一个对象中。我只是想知道是否有更有效的方法来代替我的if else
。
package UI;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
public class TableManage extends JFrame {
/**
*
*/
private static final long serialVersionUID = 4353951253754938210L;
private JPanel contentPane;
private JTable tablemanage;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableManage frame = new TableManage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TableManage() {
@SuppressWarnings("unused")
DefaultTableModel manageModel;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
DefaultTableModel model=new DefaultTableModel(
new Object[][] {
},
new String[] {
"NAME", "AGE", "GENDER"
}
);
tablemanage = new JTable(model);
JScrollPane managementpane=new JScrollPane(tablemanage);
managementpane.setBounds(40, 11, 355, 118);
contentPane.add(managementpane);
JButton btnAdd = new JButton("ADD");
btnAdd.setBounds(40, 164, 89, 23);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.addRow(new Object [][] {{null, null,null}});
model.setValueAt(null,tablemanage.getRowCount()-1,0);
}
});
contentPane.add(btnAdd);
JButton btnRemove = new JButton("REMOVE");
btnRemove.setBounds(172, 164, 89, 23);
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(tablemanage.getSelectedRow()<0)
{
JOptionPane.showMessageDialog(null,"Select value To Remove");
}
else
{
model.removeRow(tablemanage.getSelectedRow());
}
}
});
contentPane.add(btnRemove);
JButton btnSave = new JButton("SAVE");
btnSave.setBounds(306, 164, 89, 23);
contentPane.add(btnSave);
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nu=tablemanage.getRowCount();
int co=tablemanage.getColumnCount();
tablemanage.getSelectionModel().clearSelection();
int i;
int j;
ArrayList<TestingObject> testList=new ArrayList<TestingObject>();
TestingObject test=new TestingObject();
for( i=0;i<nu;i++)
{
test=new TestingObject();
for( j=0;j<co;j++)
{
System.out.println(j);
if(j==0){
test.setName((String) tablemanage.getModel().getValueAt(i,j));
}
else if(j==1){
test.setAge((String) tablemanage.getModel().getValueAt(i,j));
}
else{
test.setGender((String) tablemanage.getModel().getValueAt(i,j));
}
}
testList.add(test);
System.out.println ();
}
for(int k=0;k<testList.size();k++){
testList.get(k).print();
}
}
});
}
}
答案 0 :(得分:1)
只需删除for-loop
中的内部btnSave action lister
,然后将此代码添加到
因为column count
总是3
test=new TestingObject();
test.setName((String) tablemanage.getModel().getValueAt(i,0));
test.setAge((String) tablemanage.getModel().getValueAt(i,1));
test.setGender((String) tablemanage.getModel().getValueAt(i,2));
testList.add(test);
并使用此标准代码它可以与jdk 1.8 一起使用来打印数组列表值
testList.stream().forEach((var) -> {
System.out.println(var);
});