JComboBox删除清空的问题(删除最后一项会抛异常) 我向清空JComboBox的Item,再重新从加新的Item我用removeAllItem()能够解决问题,但是会抛出一个 null 异常从DrfauleComboBoxModel 清空也会产生 null 异常没办法了,我一句一句删除,但是到了删除最后一项的时候,又抛了 null 异常,而且删除最后一项不成功我的JComboBox加不加Model都有异常,该怎么解决呢?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你怎么处理的啊?import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JComboBoxDemo extends JFrame{ String[] s={"西瓜","苹果","草莓","香蕉","葡萄"}; JComboBox combo=new JComboBox(s); public JComboBoxDemo(){ super("JComboBoxDemo"); this.setSize(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = this.getContentPane(); c.setLayout(new FlowLayout(FlowLayout.LEADING)); this.getContentPane().add(combo); JButton add = new JButton("add"); JButton remove = new JButton("remove"); add.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ combo.addItem("test"); } }); remove.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ combo.removeAllItems(); } }); this.getContentPane().add(add); this.getContentPane().add(remove); this.setVisible(true); } public static void main(String[] args){ new JComboBoxDemo(); } }也没有出现异常啊 我是这样写的: public void ReSetcbClass()throws SQLException { try{ cbClass.removeAllItems();//cbClass是在Panel new 出来的JComboBox,异常就是在这里抛出的 }catch(Exception e) { System.out.println("dd:"+e.getMessage()); } // setcbClass(); }你的程序很正常啊,为什么我的会抛出null呢? 新的问题出现了,我把 cbClass.removeAllItems();做成方法 写在构造函数里没有异常但是在别的地方引用就会抛出null异常 你的Combobox是不是添加事件了,如ActionListeter,你每移除一个Item就是触发一个ActionListener 我的JComboBox没有添加事件呀,奇怪 import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.table.*;import java.util.*;import java.sql.*;class TablePanel extends JPanel{ javax.swing.JPopupMenu pm=new JPopupMenu(); JMenuItem edit =new JMenuItem("Edit"); JMenuItem exit =new JMenuItem("Exit"); DefaultTableModel model =new DefaultTableModel(); javax.swing.JTable table =new JTable(model); javax.swing.JScrollPane sp=new JScrollPane(table); TreePanel tp=new TreePanel(this); DB db=null; JLabel labStuNo =new JLabel("StuNo"); JLabel labStuName =new JLabel("StuName"); JLabel labBirthday =new JLabel("Birthday"); JLabel labStuClassName =new JLabel("ClassName"); JTextField txtStuNo=new JTextField(10); JTextField txtStuName=new JTextField(10); JTextField txtBirthday=new JTextField(10); //JTextField txtStuClassName=new JTextField(10); //DefaultComboBoxModel bm=new DefaultComboBoxModel(); JComboBox cbClass=new JComboBox(); JButton but_ins =new JButton("Insert"); JButton but_up =new JButton("Update"); JButton but_del =new JButton("Delete"); JButton but_ref =new JButton("Refresh"); ArrayList ary2=null; public TablePanel() { pm.add(edit); pm.add(exit); this.setLayout(new BorderLayout()); this.add(sp,BorderLayout.CENTER); this.add(tp,BorderLayout.WEST); model.addColumn("StuNo"); model.addColumn("Name"); model.addColumn("Birthday"); model.addColumn("ClassName"); JPanel p=new JPanel(); p.setLayout(new java.awt.GridBagLayout()); p.add(labStuNo,new java.awt.GridBagConstraints(0,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); p.add(txtStuNo,new java.awt.GridBagConstraints(1,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(labStuName,new java.awt.GridBagConstraints(2,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); p.add(txtStuName,new java.awt.GridBagConstraints(3,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(labBirthday,new java.awt.GridBagConstraints(4,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); p.add(txtBirthday,new java.awt.GridBagConstraints(5,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(labStuClassName,new java.awt.GridBagConstraints(6,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); /*p.add(txtStuClassName,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));*/ p.add(cbClass,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(but_ins,new java.awt.GridBagConstraints(0,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); p.add(but_up,new java.awt.GridBagConstraints(2,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); p.add(but_del,new java.awt.GridBagConstraints(4,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); p.add(but_ref,new java.awt.GridBagConstraints(6,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); but_ins.addActionListener(new li_i()); but_up.addActionListener(new li_u()); but_del.addActionListener(new li_d()); but_ref.addActionListener(new li_r()); exit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(JFrame.EXIT_ON_CLOSE); } } ); edit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { int index=table.getSelectedRow(); String StuNo=model.getValueAt(index,0).toString(); StuDialog sd=new StuDialog((JFrame)TablePanel.this.getTopLevelAncestor(),StuNo); sd.setVisible(true); try{ Refresh(); }catch(Exception e) { System.out.println(e.getMessage()); } } } ); this.add(p,BorderLayout.SOUTH); try { db=new DB(); ary2=db.getClassMsg(); if(ary2!=null) { for(int i=0;i<ary2.size();i++) { this.cbClass.addItem(ary2.get(i)); } } //cbClass.removeAllItems(); //this.setcbClass(); //this.ReSetcbClass(); Refresh(); this.table.addMouseListener(new li_m()); }catch(Exception e) { System.out.println(e.getMessage()); } } class li_m extends MouseAdapter { public void mouseClicked(MouseEvent me) { if(me.getButton()==me.BUTTON3) { pm.show(table,me.getX(),me.getY()); } try{ int index=table.getSelectedRow(); String stuNo=model.getValueAt(index,0).toString(); student stu=db.getStuMsgByStuNo(stuNo); TablePanel.this.txtStuNo.setText(stu.getStuNo()); TablePanel.this.txtStuName.setText(stu.getStuName()); TablePanel.this.txtBirthday.setText(stu.getBirthday()); //TablePanel.this.txtStuClassName.setText(model.getValueAt(index,3).toString()); //System.out.println(tp.theClass); //下列处理合适? TablePanel.this.cbClass.setSelectedItem(tp.theClass); }catch(Exception e) { System.out.println(e.getMessage()); } } } class li_i implements ActionListener { public void actionPerformed(ActionEvent ae) { student _stu=new student(); try { _stu.setStuNo(txtStuNo.getText()); System.out.println(txtStuName.getText()); _stu.setStuName(txtStuName.getText()); _stu.setBirthday(txtBirthday.getText()); int a=(((ClassMsg)cbClass.getSelectedItem()).getClassId()); _stu.setClassId(a); //_stu.setStuClassName(txtStuClassName.getText()); db.insertStu(_stu);//写入数据库 Refresh(); JOptionPane.showMessageDialog(null,"录入成功数据库!"); txtStuNo.setText(""); txtStuName.setText(""); txtBirthday.setText("");// txtStuClassName.setText(""); } catch(MyException e) { JOptionPane.showMessageDialog(null,e.getMessage()); switch(e.check) { case 1: txtStuNo.setText(""); break; case 2: txtStuName.setText(""); break; case 3: txtBirthday.setText(""); break; case 4:// txtStuClassName.setText(""); break; default: break; } //解决清空 } catch(java.lang.NumberFormatException nb) { JOptionPane.showMessageDialog(null,"请输入整数!"); txtBirthday.setText(""); } catch(Exception e) { JOptionPane.showMessageDialog(null,e.getMessage()); } } } import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.table.*;import java.util.*;import java.sql.*;class TablePanel extends JPanel{ javax.swing.JPopupMenu pm=new JPopupMenu(); JMenuItem edit =new JMenuItem("Edit"); JMenuItem exit =new JMenuItem("Exit"); DefaultTableModel model =new DefaultTableModel(); javax.swing.JTable table =new JTable(model); javax.swing.JScrollPane sp=new JScrollPane(table); TreePanel tp=new TreePanel(this); DB db=null; JLabel labStuNo =new JLabel("StuNo"); JLabel labStuName =new JLabel("StuName"); JLabel labBirthday =new JLabel("Birthday"); JLabel labStuClassName =new JLabel("ClassName"); JTextField txtStuNo=new JTextField(10); JTextField txtStuName=new JTextField(10); JTextField txtBirthday=new JTextField(10); //JTextField txtStuClassName=new JTextField(10); //DefaultComboBoxModel bm=new DefaultComboBoxModel(); JComboBox cbClass=new JComboBox(); JButton but_ins =new JButton("Insert"); JButton but_up =new JButton("Update"); JButton but_del =new JButton("Delete"); JButton but_ref =new JButton("Refresh"); ArrayList ary2=null; public TablePanel() { pm.add(edit); pm.add(exit); this.setLayout(new BorderLayout()); this.add(sp,BorderLayout.CENTER); this.add(tp,BorderLayout.WEST); model.addColumn("StuNo"); model.addColumn("Name"); model.addColumn("Birthday"); model.addColumn("ClassName"); JPanel p=new JPanel(); p.setLayout(new java.awt.GridBagLayout()); p.add(labStuNo,new java.awt.GridBagConstraints(0,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); p.add(txtStuNo,new java.awt.GridBagConstraints(1,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(labStuName,new java.awt.GridBagConstraints(2,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); p.add(txtStuName,new java.awt.GridBagConstraints(3,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(labBirthday,new java.awt.GridBagConstraints(4,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); p.add(txtBirthday,new java.awt.GridBagConstraints(5,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(labStuClassName,new java.awt.GridBagConstraints(6,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,0),0,10)); /*p.add(txtStuClassName,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));*/ p.add(cbClass,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,0,10),20,10)); p.add(but_ins,new java.awt.GridBagConstraints(0,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); p.add(but_up,new java.awt.GridBagConstraints(2,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); p.add(but_del,new java.awt.GridBagConstraints(4,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); p.add(but_ref,new java.awt.GridBagConstraints(6,1,2,1,0.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,new Insets(10,0,10,0),20,10)); but_ins.addActionListener(new li_i()); but_up.addActionListener(new li_u()); but_del.addActionListener(new li_d()); but_ref.addActionListener(new li_r()); exit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(JFrame.EXIT_ON_CLOSE); } } ); edit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { int index=table.getSelectedRow(); String StuNo=model.getValueAt(index,0).toString(); StuDialog sd=new StuDialog((JFrame)TablePanel.this.getTopLevelAncestor(),StuNo); sd.setVisible(true); try{ Refresh(); }catch(Exception e) { System.out.println(e.getMessage()); } } } ); this.add(p,BorderLayout.SOUTH); try { db=new DB(); ary2=db.getClassMsg(); if(ary2!=null) { for(int i=0;i<ary2.size();i++) { this.cbClass.addItem(ary2.get(i)); } } //cbClass.removeAllItems(); //this.setcbClass(); //this.ReSetcbClass(); Refresh(); this.table.addMouseListener(new li_m()); }catch(Exception e) { System.out.println(e.getMessage()); } } class li_m extends MouseAdapter { public void mouseClicked(MouseEvent me) { if(me.getButton()==me.BUTTON3) { pm.show(table,me.getX(),me.getY()); } try{ int index=table.getSelectedRow(); String stuNo=model.getValueAt(index,0).toString(); student stu=db.getStuMsgByStuNo(stuNo); TablePanel.this.txtStuNo.setText(stu.getStuNo()); TablePanel.this.txtStuName.setText(stu.getStuName()); TablePanel.this.txtBirthday.setText(stu.getBirthday()); //TablePanel.this.txtStuClassName.setText(model.getValueAt(index,3).toString()); //System.out.println(tp.theClass); //下列处理合适? TablePanel.this.cbClass.setSelectedItem(tp.theClass); }catch(Exception e) { System.out.println(e.getMessage()); } } } class li_i implements ActionListener { public void actionPerformed(ActionEvent ae) { student _stu=new student(); try { _stu.setStuNo(txtStuNo.getText()); System.out.println(txtStuName.getText()); _stu.setStuName(txtStuName.getText()); _stu.setBirthday(txtBirthday.getText()); int a=(((ClassMsg)cbClass.getSelectedItem()).getClassId()); _stu.setClassId(a); //_stu.setStuClassName(txtStuClassName.getText()); db.insertStu(_stu);//写入数据库 Refresh(); JOptionPane.showMessageDialog(null,"录入成功数据库!"); txtStuNo.setText(""); txtStuName.setText(""); txtBirthday.setText("");// txtStuClassName.setText(""); } catch(MyException e) { JOptionPane.showMessageDialog(null,e.getMessage()); switch(e.check) { case 1: txtStuNo.setText(""); break; case 2: txtStuName.setText(""); break; case 3: txtBirthday.setText(""); break; case 4:// txtStuClassName.setText(""); break; default: break; } //解决清空 } catch(java.lang.NumberFormatException nb) { JOptionPane.showMessageDialog(null,"请输入整数!"); txtBirthday.setText(""); } catch(Exception e) { JOptionPane.showMessageDialog(null,e.getMessage()); } } } 求助:swing 相关 请问,怎样调整panel的大小? 今天的面试题,好难喔,我想没多少人会做吧!!!! 请问如何在TreeViewer里用add方法增加一个根节点啊? 如何找Jar文件 大家觉得张孝祥的java视频教程基础篇和高级篇怎么样,给点建议小弟准备买!谢谢! jTable的设计问题 在线等 表格问题? 关于System.in.read()的问题: 刚学java,请问。。。 jmf在linux下为什么会出现Cannot find a Player java JFrame 怎么实现象JDialong不点关闭就切换不到别的窗体,焦点始终在当前窗体。
import java.awt.event.*;import javax.swing.*;public class JComboBoxDemo extends JFrame{ String[] s={"西瓜","苹果","草莓","香蕉","葡萄"};
JComboBox combo=new JComboBox(s);
public JComboBoxDemo(){
super("JComboBoxDemo");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEADING));
this.getContentPane().add(combo);
JButton add = new JButton("add");
JButton remove = new JButton("remove");
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
combo.addItem("test");
}
});
remove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
combo.removeAllItems();
}
});
this.getContentPane().add(add);
this.getContentPane().add(remove);
this.setVisible(true);
}
public static void main(String[] args){
new JComboBoxDemo();
}
}也没有出现异常啊
{
try{
cbClass.removeAllItems();//cbClass是在Panel new 出来的JComboBox,异常就是在这里抛出的
}catch(Exception e)
{
System.out.println("dd:"+e.getMessage());
}
// setcbClass();
}你的程序很正常啊,为什么我的会抛出null呢?
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;class TablePanel extends JPanel
{
javax.swing.JPopupMenu pm=new JPopupMenu();
JMenuItem edit =new JMenuItem("Edit");
JMenuItem exit =new JMenuItem("Exit");
DefaultTableModel model =new DefaultTableModel();
javax.swing.JTable table =new JTable(model);
javax.swing.JScrollPane sp=new JScrollPane(table);
TreePanel tp=new TreePanel(this);
DB db=null;
JLabel labStuNo =new JLabel("StuNo");
JLabel labStuName =new JLabel("StuName");
JLabel labBirthday =new JLabel("Birthday");
JLabel labStuClassName =new JLabel("ClassName");
JTextField txtStuNo=new JTextField(10);
JTextField txtStuName=new JTextField(10);
JTextField txtBirthday=new JTextField(10);
//JTextField txtStuClassName=new JTextField(10);
//DefaultComboBoxModel bm=new DefaultComboBoxModel();
JComboBox cbClass=new JComboBox();
JButton but_ins =new JButton("Insert");
JButton but_up =new JButton("Update");
JButton but_del =new JButton("Delete");
JButton but_ref =new JButton("Refresh");
ArrayList ary2=null;
public TablePanel()
{
pm.add(edit);
pm.add(exit);
this.setLayout(new BorderLayout());
this.add(sp,BorderLayout.CENTER);
this.add(tp,BorderLayout.WEST);
model.addColumn("StuNo");
model.addColumn("Name");
model.addColumn("Birthday");
model.addColumn("ClassName");
JPanel p=new JPanel();
p.setLayout(new java.awt.GridBagLayout());
p.add(labStuNo,new java.awt.GridBagConstraints(0,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
p.add(txtStuNo,new java.awt.GridBagConstraints(1,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(labStuName,new java.awt.GridBagConstraints(2,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
p.add(txtStuName,new java.awt.GridBagConstraints(3,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(labBirthday,new java.awt.GridBagConstraints(4,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
p.add(txtBirthday,new java.awt.GridBagConstraints(5,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(labStuClassName,new java.awt.GridBagConstraints(6,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
/*p.add(txtStuClassName,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));*/
p.add(cbClass,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(but_ins,new java.awt.GridBagConstraints(0,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
p.add(but_up,new java.awt.GridBagConstraints(2,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
p.add(but_del,new java.awt.GridBagConstraints(4,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
p.add(but_ref,new java.awt.GridBagConstraints(6,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
but_ins.addActionListener(new li_i());
but_up.addActionListener(new li_u());
but_del.addActionListener(new li_d());
but_ref.addActionListener(new li_r());
exit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(JFrame.EXIT_ON_CLOSE);
}
}
);
edit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int index=table.getSelectedRow();
String StuNo=model.getValueAt(index,0).toString();
StuDialog sd=new StuDialog((JFrame)TablePanel.this.getTopLevelAncestor(),StuNo);
sd.setVisible(true);
try{
Refresh();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
);
this.add(p,BorderLayout.SOUTH);
try
{
db=new DB();
ary2=db.getClassMsg();
if(ary2!=null)
{
for(int i=0;i<ary2.size();i++)
{
this.cbClass.addItem(ary2.get(i));
}
}
//cbClass.removeAllItems();
//this.setcbClass();
//this.ReSetcbClass();
Refresh();
this.table.addMouseListener(new li_m());
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
class li_m extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
if(me.getButton()==me.BUTTON3)
{
pm.show(table,me.getX(),me.getY());
}
try{
int index=table.getSelectedRow();
String stuNo=model.getValueAt(index,0).toString();
student stu=db.getStuMsgByStuNo(stuNo);
TablePanel.this.txtStuNo.setText(stu.getStuNo());
TablePanel.this.txtStuName.setText(stu.getStuName());
TablePanel.this.txtBirthday.setText(stu.getBirthday());
//TablePanel.this.txtStuClassName.setText(model.getValueAt(index,3).toString());
//System.out.println(tp.theClass);
//下列处理合适?
TablePanel.this.cbClass.setSelectedItem(tp.theClass);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
class li_i implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
student _stu=new student();
try
{
_stu.setStuNo(txtStuNo.getText());
System.out.println(txtStuName.getText());
_stu.setStuName(txtStuName.getText());
_stu.setBirthday(txtBirthday.getText());
int a=(((ClassMsg)cbClass.getSelectedItem()).getClassId());
_stu.setClassId(a);
//_stu.setStuClassName(txtStuClassName.getText()); db.insertStu(_stu);//写入数据库
Refresh();
JOptionPane.showMessageDialog(null,"录入成功数据库!");
txtStuNo.setText("");
txtStuName.setText("");
txtBirthday.setText("");
// txtStuClassName.setText("");
}
catch(MyException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
switch(e.check)
{
case 1:
txtStuNo.setText("");
break;
case 2:
txtStuName.setText("");
break;
case 3:
txtBirthday.setText("");
break;
case 4:
// txtStuClassName.setText("");
break;
default:
break;
}
//解决清空
}
catch(java.lang.NumberFormatException nb)
{
JOptionPane.showMessageDialog(null,"请输入整数!");
txtBirthday.setText("");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;class TablePanel extends JPanel
{
javax.swing.JPopupMenu pm=new JPopupMenu();
JMenuItem edit =new JMenuItem("Edit");
JMenuItem exit =new JMenuItem("Exit");
DefaultTableModel model =new DefaultTableModel();
javax.swing.JTable table =new JTable(model);
javax.swing.JScrollPane sp=new JScrollPane(table);
TreePanel tp=new TreePanel(this);
DB db=null;
JLabel labStuNo =new JLabel("StuNo");
JLabel labStuName =new JLabel("StuName");
JLabel labBirthday =new JLabel("Birthday");
JLabel labStuClassName =new JLabel("ClassName");
JTextField txtStuNo=new JTextField(10);
JTextField txtStuName=new JTextField(10);
JTextField txtBirthday=new JTextField(10);
//JTextField txtStuClassName=new JTextField(10);
//DefaultComboBoxModel bm=new DefaultComboBoxModel();
JComboBox cbClass=new JComboBox();
JButton but_ins =new JButton("Insert");
JButton but_up =new JButton("Update");
JButton but_del =new JButton("Delete");
JButton but_ref =new JButton("Refresh");
ArrayList ary2=null;
public TablePanel()
{
pm.add(edit);
pm.add(exit);
this.setLayout(new BorderLayout());
this.add(sp,BorderLayout.CENTER);
this.add(tp,BorderLayout.WEST);
model.addColumn("StuNo");
model.addColumn("Name");
model.addColumn("Birthday");
model.addColumn("ClassName");
JPanel p=new JPanel();
p.setLayout(new java.awt.GridBagLayout());
p.add(labStuNo,new java.awt.GridBagConstraints(0,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
p.add(txtStuNo,new java.awt.GridBagConstraints(1,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(labStuName,new java.awt.GridBagConstraints(2,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
p.add(txtStuName,new java.awt.GridBagConstraints(3,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(labBirthday,new java.awt.GridBagConstraints(4,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
p.add(txtBirthday,new java.awt.GridBagConstraints(5,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(labStuClassName,new java.awt.GridBagConstraints(6,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,0),0,10));
/*p.add(txtStuClassName,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));*/
p.add(cbClass,new java.awt.GridBagConstraints(7,0,1,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,0,10),20,10));
p.add(but_ins,new java.awt.GridBagConstraints(0,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
p.add(but_up,new java.awt.GridBagConstraints(2,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
p.add(but_del,new java.awt.GridBagConstraints(4,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
p.add(but_ref,new java.awt.GridBagConstraints(6,1,2,1,0.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,new Insets(10,0,10,0),20,10));
but_ins.addActionListener(new li_i());
but_up.addActionListener(new li_u());
but_del.addActionListener(new li_d());
but_ref.addActionListener(new li_r());
exit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(JFrame.EXIT_ON_CLOSE);
}
}
);
edit.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int index=table.getSelectedRow();
String StuNo=model.getValueAt(index,0).toString();
StuDialog sd=new StuDialog((JFrame)TablePanel.this.getTopLevelAncestor(),StuNo);
sd.setVisible(true);
try{
Refresh();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
);
this.add(p,BorderLayout.SOUTH);
try
{
db=new DB();
ary2=db.getClassMsg();
if(ary2!=null)
{
for(int i=0;i<ary2.size();i++)
{
this.cbClass.addItem(ary2.get(i));
}
}
//cbClass.removeAllItems();
//this.setcbClass();
//this.ReSetcbClass();
Refresh();
this.table.addMouseListener(new li_m());
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
class li_m extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
if(me.getButton()==me.BUTTON3)
{
pm.show(table,me.getX(),me.getY());
}
try{
int index=table.getSelectedRow();
String stuNo=model.getValueAt(index,0).toString();
student stu=db.getStuMsgByStuNo(stuNo);
TablePanel.this.txtStuNo.setText(stu.getStuNo());
TablePanel.this.txtStuName.setText(stu.getStuName());
TablePanel.this.txtBirthday.setText(stu.getBirthday());
//TablePanel.this.txtStuClassName.setText(model.getValueAt(index,3).toString());
//System.out.println(tp.theClass);
//下列处理合适?
TablePanel.this.cbClass.setSelectedItem(tp.theClass);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
class li_i implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
student _stu=new student();
try
{
_stu.setStuNo(txtStuNo.getText());
System.out.println(txtStuName.getText());
_stu.setStuName(txtStuName.getText());
_stu.setBirthday(txtBirthday.getText());
int a=(((ClassMsg)cbClass.getSelectedItem()).getClassId());
_stu.setClassId(a);
//_stu.setStuClassName(txtStuClassName.getText()); db.insertStu(_stu);//写入数据库
Refresh();
JOptionPane.showMessageDialog(null,"录入成功数据库!");
txtStuNo.setText("");
txtStuName.setText("");
txtBirthday.setText("");
// txtStuClassName.setText("");
}
catch(MyException e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
switch(e.check)
{
case 1:
txtStuNo.setText("");
break;
case 2:
txtStuName.setText("");
break;
case 3:
txtBirthday.setText("");
break;
case 4:
// txtStuClassName.setText("");
break;
default:
break;
}
//解决清空
}
catch(java.lang.NumberFormatException nb)
{
JOptionPane.showMessageDialog(null,"请输入整数!");
txtBirthday.setText("");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
}