请教JComboBox和数据库的刷新问题,thanks!

yidengjiang 2004-05-02 09:00:50
我在进行jcombobox和数据库操作的时候,通过点击“操作”按纽进行籍贯的添加、删除和修改,在数据库里都可以很成功的实现,但是jcombobox里没有实现刷新,请问怎么改才能是对数据库刷新后jcombobox里能够自动的刷新,谢谢




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.sql.*;
import java.util.*;
import java.util.Vector.*;



public class combobox1 extends JFrame implements ActionListener
{
public static void main(String args[])
{
new combobox1();
}
private Connection con1;
private Statement state1;
private ResultSet rst1;
private String str5="";
private JComboBox cp6;
private Vector rec_vector;
private Container c;
private JButton b1;



public combobox1()
{
super("输入国防生基本资料");
Container c=getContentPane();
c.setLayout(null);
connect_db();
b1=new JButton("操作");
b1.addActionListener(this);
b1.setBounds(200,50,80,30);
c.add(b1);


rec_vector = new Vector();
try
{
String query="SELECT nativeplaceName FROM nativeplace";
rst1= state1.executeQuery(query);
while(rst1.next())
{
str5= rst1.getString(1);
rec_vector.addElement(str5);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
cp6=new JComboBox(rec_vector);
cp6.setBounds(10,10,150,25);
c.add(cp6);


setBounds(150,100,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);


addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);

close_db();
setVisible(false);
}
}
);
}

public void connect_db()
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}

try
{
con1=DriverManager.getConnection("jdbc:odbc:空军国防生");
state1=con1.createStatement();
}

catch(SQLException e2)
{
e2.printStackTrace();
}
}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
new addNativeplace();
}
}

public void close_db()
{
try
{
con1.close();
state1.close();
}
catch(SQLException e5)
{
e5.printStackTrace();
}

}

}


...全文
339 17 点赞 打赏 收藏 举报
写回复
17 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
marvy 2004-05-10
你的这个页面只管显示,不要牵扯到具体如何操作和获取数据。
需要的数据从一个单独的数据库操作类取得。
  • 打赏
  • 举报
回复
yidengjiang 2004-05-10
请问什么是业务逻辑啊?
我不懂
谢谢
  • 打赏
  • 举报
回复
yidengjiang 2004-05-10
if(e.getSource()==disciplineCombo)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
String schoolName=(String)schoolCombo.getSelectedItem();
String collegeName=(String)collegeCombo.getSelectedItem();
//String disciplineName=(String)disciplineCombo.getSelectedItem();
//String className=(String)classCombo.getSelectedItem();
System.out.println(schoolName);
System.out.println(collegeName);
//System.out.println(disciplineName);
//System.out.println(className);
try
{

String query13="SELECT schoolNo FROM school WHERE schoolName='"+schoolName+"'";
rst13=state13.executeQuery(query13);
while(rst13.next())
{
rst13long=rst13.getLong(1);
}

String query14="SELECT collegeNo FROM college WHERE collegeName='"+collegeName+"' AND schoolNo="+rst13long+"";
rst14=state14.executeQuery(query14);
while(rst14.next())
{
rst14long=rst14.getLong(1);
}
String query15="SELECT disciplineName FROM discipline WHERE collegeNo="+rst14long+"";
rst15=state15.executeQuery(query15);
changeint2+=1;
if(changeint2<=1)
{
while(rst15.next())
{
String results=rst15.getString(1);
disciplineCombo.addItem(results);
}
}
String disciplineName=(String)disciplineCombo.getSelectedItem();
String query16="SELECT disciplineNo FROM discipline WHERE disciplineName='"+disciplineName+"' AND collegeNo="+rst14long+"";
rst16=state16.executeQuery(query16);
while(rst16.next())
{
rst16long=rst16.getLong(1);
}

String query17="SELECT className FROM class WHERE disciplineNo="+rst16long+"";
rst17=state17.executeQuery(query17);
classCombo.removeAllItems();
while(rst17.next())
{
String results=rst17.getString(1);
classCombo.addItem(results);
}
}

catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}
}
//一开始读数据库数据的函数定义
public void getCombobox(JComboBox combobox1,JComboBox combobox2,JComboBox combobox3,JComboBox combobox4)
{
combobox1.removeAllItems();
combobox2.removeAllItems();
combobox3.removeAllItems();
combobox4.removeAllItems();
String results="",query="";

int schoolIndex=combobox1.getItemCount();

try
{

query="SELECT schoolName FROM school";
rst1=state1.executeQuery(query);
while(rst1.next())
{
results=rst1.getString(1);
combobox1.addItem(results);
}
getstring1=(String)combobox1.getItemAt(0);

query="SELECT schoolNo FROM school WHERE schoolName='"+getstring1+"'";
rst1=state1.executeQuery(query);
while(rst1.next())
{
rst1long=rst1.getLong(1);
}

query="SELECT collegeName FROM college WHERE schoolNo="+rst1long+"";
rst1=state1.executeQuery(query);
while(rst1.next())
{
results=rst1.getString("collegeName");
combobox2.addItem(results);

}
getstring2=(String)combobox2.getItemAt(0);

query="SELECT collegeNo FROM college WHERE collegeName='"+getstring2+"' AND schoolNo="+rst1long+"";
rst1=state1.executeQuery(query);
while(rst1.next())
{
rst1long=rst1.getLong(1);
}

query="SELECT disciplineName FROM discipline WHERE collegeNo="+rst1long+"";
rst1=state1.executeQuery(query);
while(rst1.next())
{
results=rst1.getString(1);
combobox3.addItem(results);
}
getstring3=(String)combobox3.getItemAt(0);

query="SELECT disciplineNo FROM discipline WHERE disciplineName='"+getstring3+"' AND collegeNo="+rst1long+" ";
rst1=state1.executeQuery(query);
while(rst1.next())
{
rst1long=rst1.getLong(1);
}
query="SELECT className FROM class WHERE disciplineNo="+rst1long+"";
rst1=state1.executeQuery(query);
while(rst1.next())
{
results=rst1.getString(1);
combobox4.addItem(results);
}


}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}

}
}


  • 打赏
  • 举报
回复
yidengjiang 2004-05-10
//下拉框选项事件
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==schoolCombo)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
String schoolName=(String)e.getItem();
//System.out.println(schoolName);
try
{
String query1="SELECT schoolNo FROM school WHERE schoolName='"+schoolName+"'";
rst1=state1.executeQuery(query1);
while(rst1.next())
{
rst1long=rst1.getLong(1);
}

String query2="SELECT collegeName FROM college WHERE schoolNo="+rst1long+"";
collegeCombo.removeAllItems();
rst2=state2.executeQuery(query2);
while(rst2.next())
{
String results=rst2.getString("collegeName");
collegeCombo.addItem(results);
}

String collegeName=(String)collegeCombo.getSelectedItem();
String query3="SELECT collegeNo FROM college WHERE collegeName='"+collegeName+"' AND schoolNo="+rst1long+"";
rst3=state3.executeQuery(query3);
while(rst3.next())
{
rst3long=rst3.getLong(1);
}
String query4="SELECT disciplineName FROM discipline WHERE collegeNo="+rst3long+"";
rst4=state4.executeQuery(query4);
disciplineCombo.removeAllItems();
while(rst4.next())
{
String results=rst4.getString(1);
disciplineCombo.addItem(results);
}
String disciplineName=(String)disciplineCombo.getSelectedItem();
String query5="SELECT disciplineNo FROM discipline WHERE disciplineName='"+disciplineName+"' AND collegeNo="+rst3long+"";
rst5=state5.executeQuery(query5);
while(rst5.next())
{
rst5long=rst5.getLong(1);
}

String query6="SELECT className FROM class WHERE disciplineNo="+rst5long+"";
rst6=state6.executeQuery(query6);
classCombo.removeAllItems();
while(rst6.next())
{
String results=rst6.getString(1);
classCombo.addItem(results);
}
//className=(String)classCombo.getSelectedItem();
}

catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}

if(e.getSource()==collegeCombo)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
String schoolName=(String)schoolCombo.getSelectedItem();
//String collegeName=(String)collegeCombo.getSelectedItem();
//String disciplineName=(String)disciplineCombo.getSelectedItem();
//String className=(String)classCombo.getSelectedItem();
//System.out.println(schoolName);
//System.out.println(collegeName);
//System.out.println(disciplineName);
//System.out.println(className);
try
{

String query7="SELECT schoolNo FROM school WHERE schoolName='"+schoolName+"'";
rst7=state7.executeQuery(query7);
while(rst7.next())
{
rst7long=rst7.getLong(1);
}

String query8="SELECT collegeName FROM college WHERE schoolNo="+rst7long+"";
//collegeCombo.removeAllItems();
rst8=state8.executeQuery(query8);
changeint1+=1;
if(changeint1<=0)
{
while(rst8.next())
{
String results=rst8.getString("collegeName");
collegeCombo.addItem(results);
}
}

String collegeName=(String)collegeCombo.getSelectedItem();
String query9="SELECT collegeNo FROM college WHERE collegeName='"+collegeName+"' AND schoolNo="+rst7long+"";
rst9=state9.executeQuery(query9);
while(rst9.next())
{
rst9long=rst9.getLong(1);
}
String query10="SELECT disciplineName FROM discipline WHERE collegeNo="+rst9long+"";
rst10=state10.executeQuery(query10);
disciplineCombo.removeAllItems();
while(rst10.next())
{
String results=rst10.getString(1);
disciplineCombo.addItem(results);
}
String disciplineName=(String)disciplineCombo.getSelectedItem();
String query11="SELECT disciplineNo FROM discipline WHERE disciplineName='"+disciplineName+"' AND collegeNo="+rst9long+"";
rst11=state11.executeQuery(query11);
while(rst11.next())
{
rst11long=rst11.getLong(1);
}

String query12="SELECT className FROM class WHERE disciplineNo="+rst11long+"";
rst12=state12.executeQuery(query12);
classCombo.removeAllItems();
while(rst12.next())
{
String results=rst12.getString(1);
classCombo.addItem(results);
}
}

catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}
  • 打赏
  • 举报
回复
yidengjiang 2004-05-10
问题已经解决
请指正

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.sql.*;
//import java.util.*;
//import java.util.Vector.*;


public class InputMessage extends JFrame implements ItemListener
{
public static void main(String args[])
{
new InputMessage();
}
private Connection con1;
private Statement state1,state2,state3,state4,state5,state6,state7,state8,
state9,state10,state11,state12,state13,state14,state15,
state16,state17,state18,state19,state20;
private ResultSet rst1,rst2,rst3,rst4,rst5,rst6,rst7,rst8,rst9,rst10,
rst11,rst12,rst13,rst14,rst15,rst16,rst17,rst18,rst19,rst20;
private long rst1long,rst2long,rst3long,rst4long,rst5long,rst6long,rst7long,
rst8long,rst9long,rst10long,rst11long,rst12long,rst13long,rst14long,rst15long,
rst16long,rst17long,rst18long,rst19long,rst20long;
private JTextArea photo_Textarea;
private JComboBox schoolCombo,collegeCombo,disciplineCombo,classCombo;
private JLabel school_Label, department_Label,discipline_Label,class_Label;
static int changeint1=0,changeint2=0,changeint3=0,changeint4=0,changeint5=0,changeint6=0,
changeint7=0,changeint8=0,changeint9=0,changeint10=0,changeint11=0,changeint12=0;

private String str5="", getstring1,getstring2,getstring3,getstring4;
private String combostring;
private String schoolName,collegeName,disciplineName,className;
private String results,query;

public InputMessage()//主题函数
{
super("输入国防生基本资料");
Container c=getContentPane();
c.setLayout(null);
connect_db();

school_Label=new JLabel("学 校:");
school_Label.setBounds(10,10,100,25);
c.add(school_Label);

schoolCombo=new JComboBox();//定义学校的JComboBox()
schoolCombo.setBounds(110,10,150,25);
c.add(schoolCombo);

department_Label=new JLabel("院 系:");
department_Label.setBounds(10,40,100,25);
c.add(department_Label);

collegeCombo=new JComboBox();//定义院系的JComboBox()
collegeCombo.setBounds(110,40,150,25);
c.add(collegeCombo);


discipline_Label=new JLabel("专 业:");
discipline_Label.setBounds(10,70,100,25);
c.add(discipline_Label);

disciplineCombo=new JComboBox();//定义专业的JComboBox()

disciplineCombo.setBounds(110,70,150,25);
c.add(disciplineCombo);

class_Label=new JLabel("班 级:");
class_Label.setBounds(10,100,100,25);
c.add(class_Label);

classCombo=new JComboBox();//定义班级的JComboBox()
classCombo.addItemListener(this);//注册下拉框选项事件 没有必要了,因为它的改变没有触发事件
classCombo.setBounds(110,100,150,25);
c.add(classCombo);

//一开始把数据库里的数据读到4个JComboBox
getCombobox(schoolCombo,collegeCombo,disciplineCombo,classCombo);

//先完成初始化,再注册监听事件,因为初始化的时候有选项值的改变
schoolCombo.addItemListener(this);//注册下拉框选项事件
collegeCombo.addItemListener(this);//注册下拉框选项事件
disciplineCombo.addItemListener(this);//注册下拉框选项事件

setBounds(150,100,300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出窗体
setVisible(true);

}

//数据库连接
public void connect_db()
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}

try
{
con1=DriverManager.getConnection("jdbc:odbc:空军国防生");
state1=con1.createStatement();
state2=con1.createStatement();
state3=con1.createStatement();
state4=con1.createStatement();
state5=con1.createStatement();
state6=con1.createStatement();
state7=con1.createStatement();
state8=con1.createStatement();
state9=con1.createStatement();
state10=con1.createStatement();
state11=con1.createStatement();
state12=con1.createStatement();
state13=con1.createStatement();
state14=con1.createStatement();
state15=con1.createStatement();
state16=con1.createStatement();
state17=con1.createStatement();
state18=con1.createStatement();
state19=con1.createStatement();
state20=con1.createStatement();

}

catch(SQLException e2)
{
e2.printStackTrace();
}
}


  • 打赏
  • 举报
回复
marvy 2004-05-05
另外,提个建议:
不要把你的业务逻辑放在此处。
一旦业务逻辑更改,那么你就很麻烦了。
  • 打赏
  • 举报
回复
marvy 2004-05-05
粗略的写了一个测试代码,针对JComboBox的,希望能够合你的意。对于JTree的,思想是一样的,你自己看着写吧:~)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestComboBox extends JFrame {
JComboBox itemComboBox = new JComboBox();
JButton addButton = new JButton();
JButton modifyButton = new JButton();
JButton deleteButton = new JButton();
DefaultComboBoxModel itemComboBoxModel;
Object[] items = { "item one", "item two", "item three",
"item four", "item five", "item six",
"item seven", "item eight",
"item nine", "item ten" };

public TestComboBox(String title){
super(title);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
itemComboBoxModel = new DefaultComboBoxModel(items);
itemComboBox.setModel(itemComboBoxModel);
this.getContentPane().setLayout(null);
itemComboBox.setBounds(new Rectangle(68, 116, 148, 33));
this.setTitle("");
addButton.setBounds(new Rectangle(265, 53, 120, 34));
addButton.setText("add");
addButton.addActionListener(new TestComboBox_addButton_actionAdapter(this));
modifyButton.setText("modify");
modifyButton.addActionListener(new TestComboBox_modifyButton_actionAdapter(this));
modifyButton.setBounds(new Rectangle(264, 116, 120, 34));
deleteButton.setText("delete");
deleteButton.addActionListener(new TestComboBox_deleteButton_actionAdapter(this));
deleteButton.setBounds(new Rectangle(265, 176, 120, 34));
this.getContentPane().add(deleteButton, null);
this.getContentPane().add(modifyButton, null);
this.getContentPane().add(addButton, null);
this.getContentPane().add(itemComboBox, null);
this.setSize(500,300);
this.show();
}

public static void main(String[] args){
new TestComboBox("Test");
}

void addButton_actionPerformed(ActionEvent e) {
Object addItem = "item eleven";
boolean isAddSucceed = true;
// 在这你可以写一些操作数据库的代码。判断数据库操作是否成功。
if(isAddSucceed){
itemComboBoxModel.addElement(addItem);
}
}

void modifyButton_actionPerformed(ActionEvent e) {
Object modifyItem = itemComboBox.getSelectedItem();
int modifyItemIndex = itemComboBox.getSelectedIndex();
boolean isModifySucceed = true;
// 在这你可以写一些操作数据库的代码。判断数据库操作是否成功。
Object updatedItem = "update item";
if(isModifySucceed){
itemComboBoxModel.removeElement(modifyItem);
itemComboBoxModel.insertElementAt(updatedItem,modifyItemIndex);
itemComboBoxModel.setSelectedItem(updatedItem);
}
}

void deleteButton_actionPerformed(ActionEvent e) {
Object deleteItem = itemComboBox.getSelectedItem();
boolean isDeleteSucceed = true;
// 在这你可以写一些操作数据库的代码。判断数据库操作是否成功。
if(isDeleteSucceed){
itemComboBoxModel.removeElement(deleteItem);
}
}

}

class TestComboBox_addButton_actionAdapter implements java.awt.event.ActionListener {
TestComboBox adaptee;

TestComboBox_addButton_actionAdapter(TestComboBox adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.addButton_actionPerformed(e);
}
}

class TestComboBox_modifyButton_actionAdapter implements java.awt.event.ActionListener {
TestComboBox adaptee;

TestComboBox_modifyButton_actionAdapter(TestComboBox adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.modifyButton_actionPerformed(e);
}
}

class TestComboBox_deleteButton_actionAdapter implements java.awt.event.ActionListener {
TestComboBox adaptee;

TestComboBox_deleteButton_actionAdapter(TestComboBox adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.deleteButton_actionPerformed(e);
}
}
  • 打赏
  • 举报
回复
yidengjiang 2004-05-04

public void valueChanged(TreeSelectionEvent e)
{
TreePath path = e.getNewLeadSelectionPath();
if(path==null)
return;

int nodenumber=path.getPathCount();
if(path!=null)
{
if(nodenumber==2)
{
updateButton.setEnabled(true);
deleteButton.setEnabled(true);
addButton.setEnabled(false);

}
else
{
updateButton.setEnabled(false);
deleteButton.setEnabled(false);
addButton.setEnabled(true);
}

}



}



public void treeNodesChanged(TreeModelEvent e)
{
TreePath treePath=e.getTreePath();
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
try
{
int[] index=e.getChildIndices();
node=(DefaultMutableTreeNode)node.getChildAt(index[0]);
}
catch(NullPointerException exc)
{
exc.printStackTrace();
}

}
public void treeNodesInserted(TreeModelEvent e)
{

}
public void treeNodesRemoved(TreeModelEvent e)
{

}
public void treeStructureChanged(TreeModelEvent e)
{

}


class MouseHandle extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{

JTree tree=(JTree)e.getSource();
int rowLocation=tree.getRowForLocation(e.getX(),e.getY());
TreePath treePath=tree.getSelectionPath();

if(treePath!=null)
{
TreeNode treenode=(TreeNode)treePath.getLastPathComponent();
}

}
}
}
  • 打赏
  • 举报
回复
yidengjiang 2004-05-04
接下来还有
public void connect_db()
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}

try
{
con1=DriverManager.getConnection("jdbc:odbc:空军国防生");
state1=con1.createStatement();
state2=con1.createStatement();

}

catch(SQLException e2)
{
e2.printStackTrace();
}
}



public void actionPerformed(ActionEvent e)
{



if(e.getSource()==addButton)
{

String s=JOptionPane.showInputDialog(addNativeplace.this,
"请输入要添加的籍贯名称","添加籍贯",
JOptionPane.QUESTION_MESSAGE);
if(s!=null)
{
if(s.equals(""))
{
JOptionPane.showMessageDialog(addNativeplace.this,
"输入的籍贯不能为空字符,请重新输入!","籍贯输入错误",
JOptionPane.ERROR_MESSAGE);
}
else
{
DefaultMutableTreeNode newNode= new DefaultMutableTreeNode(s);

int flag=1;
try
{
String query2="SELECT nativeplaceName FROM nativeplace";
rst2=state2.executeQuery(query2);
while(rst2.next())
{
String results=rst2.getString(1);
if(results.equals(s))
{
flag=0;
}
}

if(flag==0)
{
JOptionPane.showMessageDialog(addNativeplace.this,
"对不起,你输入的籍贯名称已经存在,请重新输入",
"籍贯输入错误",JOptionPane.ERROR_MESSAGE);
}

if(flag==1)
{
treeModel.insertNodeInto(newNode,root,root.getChildCount());
String query="INSERT INTO nativeplace ("+
"nativeplaceName"+
") VALUES('"+s+"')";
state1.executeUpdate(query);
JOptionPane.showMessageDialog(addNativeplace.this,
"成功输入籍贯,请继续操作!","籍贯添加成功",
JOptionPane.INFORMATION_MESSAGE);
}

}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
}
}



if(e.getSource()==updateButton)
{
TreePath treepath=tree.getSelectionPath();
DefaultMutableTreeNode parentNode=(DefaultMutableTreeNode)(treepath.getLastPathComponent());
String s=JOptionPane.showInputDialog(addNativeplace.this,
"请输入要更改的籍贯名称","更改籍贯",
JOptionPane.QUESTION_MESSAGE);
if(s!=null)
{
if(s.equals(""))
{
JOptionPane.showMessageDialog(addNativeplace.this,
"输入的的更改籍贯不能为空字符,请重新输入!","更改籍贯输入错误",
JOptionPane.ERROR_MESSAGE);
}
else
{
DefaultMutableTreeNode newNode= new DefaultMutableTreeNode(s);
int flag=1;
try
{
String query2="SELECT nativeplaceName FROM nativeplace";
rst2=state2.executeQuery(query2);
while(rst2.next())
{
String results=rst2.getString(1);
if(results.equals(s))
{
flag=0;
}
}

if(flag==0)
{
JOptionPane.showMessageDialog(addNativeplace.this,
"对不起,你输入的更改籍贯名称已经存在,请重新输入",
"籍贯输入错误",JOptionPane.ERROR_MESSAGE);
}

if(flag==1)
{
String query="UPDATE nativeplace SET nativeplaceName='"+s+"' WHERE nativeplaceName='"+parentNode+"'";
state1.executeUpdate(query);

JOptionPane.showMessageDialog(addNativeplace.this,
"成功修改籍贯名称,请继续操作!","籍贯修改成功",
JOptionPane.INFORMATION_MESSAGE);
parentNode.setUserObject(s);
}


}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
}
}

if(e.getSource()==deleteButton)
{
TreePath treepath=tree.getSelectionPath();
DefaultMutableTreeNode parentNode=(DefaultMutableTreeNode)(treepath.getLastPathComponent());
int result= JOptionPane.showConfirmDialog(addNativeplace.this,
"删除籍贯后该籍贯将不存在,是否确定要删除所选籍贯?",
" 是否删除籍贯",JOptionPane.YES_NO_OPTION);
TreeNode parent1Node=(TreeNode)parentNode.getParent();

if(treepath!=null)
{
if(parent1Node!=null)
{
if(result==JOptionPane.YES_OPTION)
{
try
{
String query="DELETE FROM nativeplace WHERE nativeplaceName='"+parentNode+"'";
state1.executeUpdate(query);
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
treeModel.removeNodeFromParent(parentNode);
}
}
}

}
tree.updateUI();

if(e.getSource()==exitButton)
{

dispose();
}

}

  • 打赏
  • 举报
回复
yidengjiang 2004-05-04
marvy(marvy) 我没有实现过这种东西
你能帮我弄一下吗?
谢谢

需要用到的程序是

import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.tree.*;

public class addNativeplace extends JFrame implements ActionListener,
TreeModelListener,TreeSelectionListener
{
public static void main(String args[])
{
new addNativeplace();
}


private JButton addButton,updateButton,deleteButton,exitButton;
private Connection con1;
private Statement state1,state2;
private ResultSet rst1,rst2;
private JTree tree;
private DefaultTreeModel treeModel=null;
private DefaultMutableTreeNode root;
private JScrollPane scroller1;

public addNativeplace()
{
super("添加籍贯");
Container c=getContentPane();
c.setLayout(null);
connect_db();

addButton=new JButton("添加籍贯");
addButton.addActionListener(this);

addButton.setBounds(220,20,100,30);
c.add(addButton);

updateButton=new JButton("修改籍贯");
updateButton.addActionListener(this);
updateButton.setEnabled(false);
updateButton.setBounds(220,70,100,30);
c.add(updateButton);

deleteButton=new JButton("删除籍贯");
deleteButton.setEnabled(false);
deleteButton.addActionListener(this);
deleteButton.setBounds(220,120,100,30);
c.add(deleteButton);

exitButton=new JButton("退出");
exitButton.addActionListener(this);
exitButton.setBounds(245,170,60,30);
c.add(exitButton);



root=new DefaultMutableTreeNode("籍贯");
tree=new JTree(root);

tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
tree.addTreeSelectionListener(this);
treeModel=(DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);
scroller1=new JScrollPane();
scroller1.setViewportView(tree);
scroller1.setBounds(10,10,150,200);
c.add(scroller1);

try
{
String query1="SELECT nativeplaceName FROM nativeplace";
rst1=state1.executeQuery(query1);


while(rst1.next())
{

String rst1string=rst1.getString(1);
DefaultMutableTreeNode newnode1=new DefaultMutableTreeNode(rst1string);
root.add(newnode1);

treeModel.reload();
}
}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//System.exit(0);

// close_db();
setVisible(false);
}
}
);

//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200,200,350,250);
setVisible(true);
setResizable(false);

}


  • 打赏
  • 举报
回复
fzghua 2004-05-04
高手
  • 打赏
  • 举报
回复
yidengjiang 2004-05-04
高手啊
救救我吧
  • 打赏
  • 举报
回复
battyht 2004-05-04

好东西,帮你顶一下,大大们来帮帮忙!
  • 打赏
  • 举报
回复
an7coding 2004-05-04
学习!
  • 打赏
  • 举报
回复
yidengjiang 2004-05-03
怎么连看都不看我的啊
  • 打赏
  • 举报
回复
marvy 2004-05-03
有两种方案可供选取:

1、每次对数据库进行增、删、改操作成功后,重新取得数据库中所有你自己要显示在JComboBox中的列值,构造新的DefaultComboBoxModel,然后重绘JComboBox;

2、每次对数据库进行增、删、改操作成功后,将数据库受影响的JComboBox对应的值通过DefaultComboBoxModel同样进行增、删、改,然后重绘JComboBox。

个人推荐方案[2],这样的性能应该更好。
  • 打赏
  • 举报
回复
yidengjiang 2004-05-02
大家给点意见啊!
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2004-05-02 09:00
社区公告
暂无公告