我有一个JtextField和Jcombobox.Whenever值从textfield和combobox中选择并单击添加按钮,值应该出现在表中。第一行正确添加。当我尝试添加第二行时,第一行值将替换为新值,但不会添加第二行。 每次单击添加按钮时,应该将值添加到表中而不删除旧值,单击删除我应该能够删除行..我可以实现这一点。 这是我的代码
public static void main(String[] args)
{
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try
{
ModelSelection modelselection=new ModelSelection();
modelselection.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
public ModelSelection() throws TooManyListenersException
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setBounds(240, 30, 906, 658);
contentPane =new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
Image im=new ImageIcon(this.getClass().getResource("/sprinkle.jpg")).getImage();
setContentPane(new JLabel(new ImageIcon(im)));
contentPane.setLayout(null);
modelSelection=new JLabel("MODULE SELECTION");
modelSelection.setBounds(240, 20, 450,40);
modelSelection.setFont(new Font("arial",Font.BOLD,40));
Color modellabel = new Color(16,180,205);
modelSelection.setForeground(modellabel);
add(modelSelection);
lab=new JLabel("Choose S-NET device and S-NET ID");
lab.setBounds(30, 75, 500,40);
lab.setFont(new Font("arial",Font.BOLD,25));
Color label = new Color(176,224,230);
lab.setForeground(label);
add(lab);
choice=new JLabel("S-Net Device -");
choice.setBounds(30, 145, 200, 20);
choice.setFont(new Font("arial",Font.BOLD,25));
Color choicecolor = new Color(135,206,250);
choice.setForeground(choicecolor);
add(choice);
String devicesnames[]={"SW-LE-4","SD-LD-4","SR-RL-8","SD-FL-4","SD-FN-2"};
mod=new JComboBox(devicesnames);
mod.setBounds(260,140,230,35);
Color modcolor = new Color(173,216,230);
mod.setBackground(modcolor);
mod.setFont(new Font("arial",Font.BOLD,17));
Color border=new Color(30,144,255);
mod.setBorder(new LineBorder(border, 2));
mod.setSelectedIndex(-1);
add(mod);
snet=new JLabel("S-Net ID -");
snet.setBounds(30, 200, 200, 20);
snet.setFont(new Font("arial",Font.BOLD,25));
Color snetcolor = new Color(135,206,250);
snet.setForeground(snetcolor);
add(snet);
id=new JTextField("1");
id.setBounds(260,200,230,35);
id.setEditable(true);
Color idcolor = new Color(173,216,230);
id.setBackground(idcolor);
Color idborder=new Color(30,144,255);
id.setBorder(new LineBorder(idborder, 3));
id.setFont(new Font("arial",Font.BOLD,20));
add(id);
validityid=new JLabel("**S-Net ID(1-230) only");
validityid.setBounds(500, 210, 200, 20);
validityid.setFont(new Font("arial",Font.PLAIN,18));
Color validityidcolor = new Color(255,99,71);
validityid.setForeground(validityidcolor);
add(validityid);
add=new JButton("ADD");
add.setBounds(180,260, 80, 35);
Color addbackground=new Color(0,0,255);
add.setBackground(addbackground);
Color border1=new Color(30,144,255);
add.setBorder(new LineBorder(border1,3));
add.setFont(new Font("arial",Font.BOLD,18));
add.setForeground(Color.WHITE);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(mod.getSelectedItem()!=null && id.getText()!=null)
{
System.out.println("correct");
simpleset();
}
else
{
JOptionPane optionPane = new JOptionPane("Fields cannot be empty", JOptionPane.ERROR_MESSAGE);
JDialog dialog = optionPane.createDialog("FAILURE");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
}
});
add(add);
devices=new JLabel("Selected S-NET device and S-NET ID");
devices.setBounds(30, 310, 450,40);
devices.setFont(new Font("arial",Font.BOLD,25));
Color deviceslabel = new Color(176,224,230);
devices.setForeground(deviceslabel);
add(devices);
String columnNames[] = { "S-NET Device","S-NET ID"};
String dataValues[][] =
{
{ "12", "234", },
{ "-123", "43", },
{ "93", "89.2", },
{ "12", "234", },
{ "-123", "43", },
};
t=new JTable();
JScrollPane s=new JScrollPane(t);
s.setBounds(30, 360, 400, 200);
Color sbcolor = new Color(128,200,242);
t.setBackground(sbcolor);
Color headercolor = new Color(152,252,252);
t.getTableHeader().setBackground(headercolor);
Color headerborder=new Color(0,0,255);
t.getTableHeader().setBorder(new LineBorder(headerborder,2));
t.getTableHeader().setFont(new Font("arial", Font.BOLD, 18));
t.setRowHeight(30);
t.setFillsViewportHeight(true);
t.setShowVerticalLines(true);
t.setShowGrid(true);
Color gridcolor=new Color(0,0,0);
t.setGridColor(gridcolor);
t.setFont(new Font("arial", Font.BOLD, 17));
Color scrollborder=new Color(15,132,210);
s.setBorder(new LineBorder(scrollborder, 4));
s.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
add(s);
configure=new JButton("CONFIGURE");
configure.setBounds(80,575, 120, 35);
Color configureborder=new Color(30,144,255);
configure.setBorder(new LineBorder(configureborder, 3));
Color configurebackground=new Color(50,205,50);
configure.setBackground(configurebackground);
configure.setFont(new Font("arial",Font.BOLD,15));
configure.setForeground(Color.WHITE);
add(configure);
delete=new JButton("DELETE");
delete.setBounds(230,575, 120, 35);
Color deleteborder=new Color(30,144,255);
delete.setBorder(new LineBorder(deleteborder, 3));
Color deletebackground=new Color(220,20,60);
delete.setBackground(deletebackground);
delete.setFont(new Font("arial",Font.BOLD,15));
delete.setForeground(Color.WHITE);
add(delete);
}
protected void simpleset()
{
// TODO Auto-generated method stub
String data=mod.getSelectedItem().toString();
System.out.println(data);
String num=id.getText();
String columnNames[] = { "S-NET Device","S-NET ID"};
Object[][] row={{data,num}};
for(int i=0;i<row.length;i++)
{
System.out.println(row[i]);
}
DefaultTableModel dtm = new DefaultTableModel(0, 0);
dtm.setColumnIdentifiers(columnNames);
List<String> list = new ArrayList<String>();
list.add(num);
list.add(data);
dtm.addRow(list.toArray());
t.setModel(dtm);
DefaultTableCellRenderer rightRenderer=new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(SwingConstants.CENTER);
t.getColumnModel().getColumn(0).setCellRenderer(rightRenderer);
t.getColumnModel().getColumn(1).setCellRenderer(rightRenderer);
}
答案 0 :(得分:2)
您的simpleset()会在每次调用时重新创建TableModel。
DefaultTableModel dtm = new DefaultTableModel(0, 0);
...
t.setModel(dtm);
只创建一次,存储为字段(或从JTable实例获取)并使用addRow()或insertRow()方法插入新数据。
答案 1 :(得分:1)
问题是每次单击添加按钮时都要重新创建整个表模型。表格模型(以及表格单元格渲染器,btw)必须在初始化时创建一次,并且只需在单击按钮时调用addRow。