对话框自己写的,有一个列表和两个按钮(一个确定按钮,一个取消按钮),点击按钮后除了要返回数据外还要将生成的该对话框关闭或者隐藏掉,返回的数据拿到了,可是对话框隐藏或关闭不掉,一开始用了单例,在确定按钮里返回数据后,关闭掉该对话框,说空指针错误。后来去掉了单例模式,还是关不掉,请各位大虾看看,代码:public class NpcTypeDropsDialog extends JDialog {
// private static NpcTypeDropsDialog npcTypeDropsDialog = null;
String strIndex = " "; //用于存储编辑后 返回的值
boolean checkedFlag = false;
private JPanel p; //用于哑实现,没有实际意义
JDialog dialog = new JDialog(); //弹出的对话框,在构造方法中实现
DefaultListModel model=new DefaultListModel();
JList list = null; //可以多选的列表,JList
JPanel panel = null; //用于放确定和取消按钮的容器
JButton bt = null; //按钮组件
int listLength = 0;
DropItemServicesHandler handler = new DropItemServices();
Vector<DropItem> dropItemVector =null;
/**
* 选择item的构造器
*/
public NpcTypeDropsDialog() {
System.out.println(" NpcTypeDropsDialog 构造函数 ");
dialog.setTitle("选择DropItem"); //设置对话框的头
dialog.setModal(true); //设为模态对话框,其他的窗口不可点击
dialog.setBounds(600, 400, 600, 300); //设置对话框的显示位置
listLength = handler.getAll().size();
dropItemVector = handler.getAll();
String strTemp = null;
String strItem[] = new String[listLength];
for(int i = 0 ; i<listLength; i++) {
strTemp = "DropItem[" + dropItemVector.get(i).getId() + "] = {taskID = "+dropItemVector.get(i).getTaskID();
strTemp += ", DropItem = {{itemID = " + dropItemVector.get(i).getItemID() + ", startRa = ";
strTemp += dropItemVector.get(i).getStartRa() + ", endRa = " + dropItemVector.get(i).getEndRa() + "}}}";
strItem[i] = strTemp;
}
list = new JList(strItem);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //设置为多选
panel = new JPanel();
bt = new JButton("确定");
bt.addActionListener(new ActionListener(){
//取得选择的项的索引
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//将选择的索引值保存到strIndex中去
int index[] = list.getSelectedIndices();
for(int i = 0 ; i< index.length; i++){
if(i == index.length-1 )
strIndex += dropItemVector.get(index[i]).getId() ;
else if(i == 0)
strIndex = dropItemVector.get(index[i]).getId() + ",";
else
strIndex += dropItemVector.get(index[i]).getId()+ ",";
}
checkedFlag = true;
return;
}
});
panel.add(bt);
bt = new JButton("取消");
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
checkedFlag = false;
}
});
panel.add(bt);
//当关闭对话框的时候,编辑也随之取消
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
checkedFlag = false;
}
});
dialog.add(new JScrollPane(list), BorderLayout.CENTER);
dialog.add(panel,BorderLayout.SOUTH);
dialog.setVisible(true);
}
/**
* 单例
* @return
*/
/*public static NpcTypeDropsDialog getInstance() {
if(npcTypeDropsDialog == null){
npcTypeDropsDialog = new NpcTypeDropsDialog();
// return npcTypeDropsDialog;
}else{
npcTypeDropsDialog=null;
npcTypeDropsDialog = new NpcTypeDropsDialog();
return npcTypeDropsDialog;
}
return npcTypeDropsDialog;
}*/
/**
* 获得选择的任务
* @return
*/
public String getSelectedItem(){
return strIndex;
}
public boolean getCheckedFlag(){
return checkedFlag ;
}
/* public void disposeDialog(){
if(npcTypeDropsDialog != null){
npcTypeDropsDialog.dispose();
}
}*/
}
// private static NpcTypeDropsDialog npcTypeDropsDialog = null;
String strIndex = " "; //用于存储编辑后 返回的值
boolean checkedFlag = false;
private JPanel p; //用于哑实现,没有实际意义
JDialog dialog = new JDialog(); //弹出的对话框,在构造方法中实现
DefaultListModel model=new DefaultListModel();
JList list = null; //可以多选的列表,JList
JPanel panel = null; //用于放确定和取消按钮的容器
JButton bt = null; //按钮组件
int listLength = 0;
DropItemServicesHandler handler = new DropItemServices();
Vector<DropItem> dropItemVector =null;
/**
* 选择item的构造器
*/
public NpcTypeDropsDialog() {
System.out.println(" NpcTypeDropsDialog 构造函数 ");
dialog.setTitle("选择DropItem"); //设置对话框的头
dialog.setModal(true); //设为模态对话框,其他的窗口不可点击
dialog.setBounds(600, 400, 600, 300); //设置对话框的显示位置
listLength = handler.getAll().size();
dropItemVector = handler.getAll();
String strTemp = null;
String strItem[] = new String[listLength];
for(int i = 0 ; i<listLength; i++) {
strTemp = "DropItem[" + dropItemVector.get(i).getId() + "] = {taskID = "+dropItemVector.get(i).getTaskID();
strTemp += ", DropItem = {{itemID = " + dropItemVector.get(i).getItemID() + ", startRa = ";
strTemp += dropItemVector.get(i).getStartRa() + ", endRa = " + dropItemVector.get(i).getEndRa() + "}}}";
strItem[i] = strTemp;
}
list = new JList(strItem);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //设置为多选
panel = new JPanel();
bt = new JButton("确定");
bt.addActionListener(new ActionListener(){
//取得选择的项的索引
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//将选择的索引值保存到strIndex中去
int index[] = list.getSelectedIndices();
for(int i = 0 ; i< index.length; i++){
if(i == index.length-1 )
strIndex += dropItemVector.get(index[i]).getId() ;
else if(i == 0)
strIndex = dropItemVector.get(index[i]).getId() + ",";
else
strIndex += dropItemVector.get(index[i]).getId()+ ",";
}
checkedFlag = true;
return;
}
});
panel.add(bt);
bt = new JButton("取消");
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
checkedFlag = false;
}
});
panel.add(bt);
//当关闭对话框的时候,编辑也随之取消
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
checkedFlag = false;
}
});
dialog.add(new JScrollPane(list), BorderLayout.CENTER);
dialog.add(panel,BorderLayout.SOUTH);
dialog.setVisible(true);
}
/**
* 单例
* @return
*/
/*public static NpcTypeDropsDialog getInstance() {
if(npcTypeDropsDialog == null){
npcTypeDropsDialog = new NpcTypeDropsDialog();
// return npcTypeDropsDialog;
}else{
npcTypeDropsDialog=null;
npcTypeDropsDialog = new NpcTypeDropsDialog();
return npcTypeDropsDialog;
}
return npcTypeDropsDialog;
}*/
/**
* 获得选择的任务
* @return
*/
public String getSelectedItem(){
return strIndex;
}
public boolean getCheckedFlag(){
return checkedFlag ;
}
/* public void disposeDialog(){
if(npcTypeDropsDialog != null){
npcTypeDropsDialog.dispose();
}
}*/
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
String value = " ";
java.awt.Point point = e.getPoint();
int rowIndex = table.rowAtPoint(point);
int colIndex = table.columnAtPoint(point);
if(rowIndex != -1 && colIndex != -1){
colIndex = table.convertColumnIndexToModel(colIndex);
rowIndex = table.convertRowIndexToModel(rowIndex);
String title = tableModel.getColumnName(colIndex);
if(title.equals("drops") ){
System.out.println("you clicked drops");
NpcTypeDropsDialog d = new NpcTypeDropsDialog();
System.out.println("npctype managerframe clicked 2" + " " + d.getSelectedItem() + "rowIndesx: "+rowIndex + "colIndex: "+colIndex);
value = (String) tableModel.getValueAt(rowIndex, colIndex);
if(d.getCheckedFlag()){
tableModel.setValueAt(d.getSelectedItem(), rowIndex, colIndex);
d.dispose();
}else{
tableModel.setValueAt(value, rowIndex, colIndex);
d.dispose();
}
table.repaint();
}
System.out.println("npctype managerframe clicked 222222");
}
}
});
这个是你关闭对话框的代码?!dialog.setVisible(true);
应该是false吧????
//当关闭对话框的时候,编辑也随之取消
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
checkedFlag = false;
}
});1. 返回的数据拿到了,可是对话框隐藏或关闭不掉,一开始用了单例。使用单例时,只需要隐藏对话框,不需要把其删除。 public void windowClosing(WindowEvent e){
checkedFlag = false;
NpcTypeDropsDialog.this.setVisible(false);
}2. 隐藏并翻译对话框的资源,使用如下 public void windowClosing(WindowEvent e){
checkedFlag = false;
NpcTypeDropsDialog.this.setVisible(false);
NpcTypeDropsDialog.this.dispose();
}3. 提示空指针,看一下是哪一行发生了空指针调用,然后继续找到引发问题的代码所在行。