jDialog 不处理
jDialog do not dispose
我有一个 jFrame,当我按 F2 键时调用 jDialog,在这个 jDialog 中放置一些信息,然后当按下或单击 jButton 或 Enter 时,我获取信息并处理 jDialog。
但是,jDialog 并没有关闭,而是获取了我请求的所有信息。
代码如下:
/**
* Creates new form DialogCPF
*/
public DialogCPF(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
jFormattedTextField1.addKeyListener(new Tecla() {
@Override
public void keyReleased(KeyEvent e) {
}
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
jButtonSalvar.setEnabled(true);
jButtonExcluir.setEnabled(false);
jButtonINSERIR.setEnabled(false);
jButtonALTERAR.setEnabled(false);
jButtonPROCURAR.setEnabled(false);
jTextFieldAPELIDO.setEnabled(true);
jTextFieldNOME.setEnabled(true);
jFormattedTextFieldCPF.setEnabled(true);
jFormattedTextFieldDATA.setEnabled(true);
jTextFieldID.setEnabled(false);
/*DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), false);
dialog.dispose();*/
DialogCPF A = (DialogCPF)evt.getSource();
A.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
// Nothing
}
@Override
public void keyPressed(KeyEvent e) {
// Nothing
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
jButtonSalvar.setEnabled(true);
jButtonExcluir.setEnabled(false);
jButtonINSERIR.setEnabled(false);
jButtonALTERAR.setEnabled(false);
jButtonPROCURAR.setEnabled(false);
jTextFieldAPELIDO.setEnabled(true);
jTextFieldNOME.setEnabled(true);
jFormattedTextFieldCPF.setEnabled(true);
jFormattedTextFieldDATA.setEnabled(true);
jTextFieldID.setEnabled(false);
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
//dialog.setEnabled(false);
dialog.dispose();
}
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
public static javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
我已经尝试了 This, this and this 解决方案,但是,我无法提供解决方案
这个...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
//...
DialogCPF A = (DialogCPF) evt.getSource();
A.dispose();
}
不太可能工作,因为 ActionEvent
的来源不太可能是对话。我还担心 ConsumirWS2
的实例的创建,这不是对话框的责任,对话框应该允许调用者提取它需要的信息,可能通过观察者模式。
这个...
dialog.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// Nothing
}
@Override
public void keyPressed(KeyEvent e) {
// Nothing
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//...
}
}
});
是个坏主意,出于多种原因,除了对用户来说不明显之外,任何将焦点从对话框(如文本字段)上移开的组件都会阻止它的执行。相反,请参阅 How to set the Java default button to react on ENTER key _released_? 以获得更好的解决方案
但是当我们在这里时...
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//...
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
//dialog.setEnabled(false);
dialog.dispose();
}
}
行不通。您创建了一个新的对话框实例并立即将其处理掉……这将如何影响屏幕上的对话框实例并触发此事件?!
一般来说,您需要在对话框的当前实例上调用 dispose
并且您最好查看 How to Make Dialogs and How to Make Frames 并且您最好避免使用表单编辑
作为概念示例...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(50, 50, 50, 50));
JButton btn = new JButton("Collect data");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormData formData = DataEntryPane.showDialog(TestPane.this, null);
if (formData != null) {
JOptionPane.showMessageDialog(TestPane.this, "I have new data");
} else {
JOptionPane.showMessageDialog(TestPane.this, "No new data for me");
}
}
});
add(btn);
}
}
public static class FormData {
private String importantStuff;
public FormData(String importantStuff) {
this.importantStuff = importantStuff;
}
public String getImportantStuff() {
return importantStuff;
}
}
public static class DataEntryPane extends JPanel {
public interface Observer {
public void formValidationRequired(DataEntryPane source);
}
private Observer observer;
private JTextField textField;
public DataEntryPane(Observer observer, FormData formData) {
this.observer = observer;
setLayout(new GridBagLayout());
textField = new JTextField(10);
add(new JLabel("Some important stuff: "));
add(textField);
if (formData != null) {
textField.setText(formData.getImportantStuff());
}
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (observer != null) {
observer.formValidationRequired(DataEntryPane.this);
}
}
};
textField.addActionListener(actionListener);
}
public String getImportantStuff() {
return textField.getText();
}
public static FormData showDialog(Component parent, FormData data) {
enum State {
OK, CANCEL
}
// You could use a JOptionPane instead, but where's the fun in that
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(parent));
dialog.setTitle("All you data is belong to us");
dialog.setModal(true);
JPanel content = new JPanel(new BorderLayout());
content.setBorder(new EmptyBorder(32, 32, 32, 32));
dialog.setContentPane(content);
DataEntryPane entryPane = new DataEntryPane(new Observer() {
@Override
public void formValidationRequired(DataEntryPane source) {
// Do what ever validation you need
boolean isValid = true;
if (isValid) {
source.putClientProperty("state", State.CANCEL);
dialog.dispose();
}
}
}, data);
entryPane.setBorder(new EmptyBorder(32, 32, 32, 32));
dialog.add(entryPane);
JButton okayButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
okayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entryPane.putClientProperty("state", State.OK);
dialog.dispose();
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entryPane.putClientProperty("state", State.CANCEL);
dialog.dispose();
}
});
JPanel actionPane = new JPanel();
actionPane.setBorder(new EmptyBorder(8, 8, 8, 8));
actionPane.add(okayButton);
actionPane.add(cancelButton);
dialog.add(actionPane, BorderLayout.SOUTH);
dialog.getRootPane().setDefaultButton(okayButton);
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
State state = (State) entryPane.getClientProperty("state");
// Only process the information if the user selected "okay"
if (state != null && State.OK == state) {
System.out.println("Validate user input");
// Do what ever validation you need
boolean isValid = true;
if (isValid) {
String importantStuff = entryPane.getImportantStuff();
return new FormData(importantStuff);
}
} else {
System.out.println("User cancelled operation");
}
return null;
}
}
}
我有一个 jFrame,当我按 F2 键时调用 jDialog,在这个 jDialog 中放置一些信息,然后当按下或单击 jButton 或 Enter 时,我获取信息并处理 jDialog。
但是,jDialog 并没有关闭,而是获取了我请求的所有信息。
代码如下:
/**
* Creates new form DialogCPF
*/
public DialogCPF(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
jFormattedTextField1.addKeyListener(new Tecla() {
@Override
public void keyReleased(KeyEvent e) {
}
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
jButtonSalvar.setEnabled(true);
jButtonExcluir.setEnabled(false);
jButtonINSERIR.setEnabled(false);
jButtonALTERAR.setEnabled(false);
jButtonPROCURAR.setEnabled(false);
jTextFieldAPELIDO.setEnabled(true);
jTextFieldNOME.setEnabled(true);
jFormattedTextFieldCPF.setEnabled(true);
jFormattedTextFieldDATA.setEnabled(true);
jTextFieldID.setEnabled(false);
/*DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), false);
dialog.dispose();*/
DialogCPF A = (DialogCPF)evt.getSource();
A.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent e) {
// Nothing
}
@Override
public void keyPressed(KeyEvent e) {
// Nothing
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
jButtonSalvar.setEnabled(true);
jButtonExcluir.setEnabled(false);
jButtonINSERIR.setEnabled(false);
jButtonALTERAR.setEnabled(false);
jButtonPROCURAR.setEnabled(false);
jTextFieldAPELIDO.setEnabled(true);
jTextFieldNOME.setEnabled(true);
jFormattedTextFieldCPF.setEnabled(true);
jFormattedTextFieldDATA.setEnabled(true);
jTextFieldID.setEnabled(false);
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
//dialog.setEnabled(false);
dialog.dispose();
}
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
public static javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
我已经尝试了 This, this and this 解决方案,但是,我无法提供解决方案
这个...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
//...
DialogCPF A = (DialogCPF) evt.getSource();
A.dispose();
}
不太可能工作,因为 ActionEvent
的来源不太可能是对话。我还担心 ConsumirWS2
的实例的创建,这不是对话框的责任,对话框应该允许调用者提取它需要的信息,可能通过观察者模式。
这个...
dialog.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// Nothing
}
@Override
public void keyPressed(KeyEvent e) {
// Nothing
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//...
}
}
});
是个坏主意,出于多种原因,除了对用户来说不明显之外,任何将焦点从对话框(如文本字段)上移开的组件都会阻止它的执行。相反,请参阅 How to set the Java default button to react on ENTER key _released_? 以获得更好的解决方案
但是当我们在这里时...
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//...
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
//dialog.setEnabled(false);
dialog.dispose();
}
}
行不通。您创建了一个新的对话框实例并立即将其处理掉……这将如何影响屏幕上的对话框实例并触发此事件?!
一般来说,您需要在对话框的当前实例上调用 dispose
并且您最好查看 How to Make Dialogs and How to Make Frames 并且您最好避免使用表单编辑
作为概念示例...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(50, 50, 50, 50));
JButton btn = new JButton("Collect data");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormData formData = DataEntryPane.showDialog(TestPane.this, null);
if (formData != null) {
JOptionPane.showMessageDialog(TestPane.this, "I have new data");
} else {
JOptionPane.showMessageDialog(TestPane.this, "No new data for me");
}
}
});
add(btn);
}
}
public static class FormData {
private String importantStuff;
public FormData(String importantStuff) {
this.importantStuff = importantStuff;
}
public String getImportantStuff() {
return importantStuff;
}
}
public static class DataEntryPane extends JPanel {
public interface Observer {
public void formValidationRequired(DataEntryPane source);
}
private Observer observer;
private JTextField textField;
public DataEntryPane(Observer observer, FormData formData) {
this.observer = observer;
setLayout(new GridBagLayout());
textField = new JTextField(10);
add(new JLabel("Some important stuff: "));
add(textField);
if (formData != null) {
textField.setText(formData.getImportantStuff());
}
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (observer != null) {
observer.formValidationRequired(DataEntryPane.this);
}
}
};
textField.addActionListener(actionListener);
}
public String getImportantStuff() {
return textField.getText();
}
public static FormData showDialog(Component parent, FormData data) {
enum State {
OK, CANCEL
}
// You could use a JOptionPane instead, but where's the fun in that
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(parent));
dialog.setTitle("All you data is belong to us");
dialog.setModal(true);
JPanel content = new JPanel(new BorderLayout());
content.setBorder(new EmptyBorder(32, 32, 32, 32));
dialog.setContentPane(content);
DataEntryPane entryPane = new DataEntryPane(new Observer() {
@Override
public void formValidationRequired(DataEntryPane source) {
// Do what ever validation you need
boolean isValid = true;
if (isValid) {
source.putClientProperty("state", State.CANCEL);
dialog.dispose();
}
}
}, data);
entryPane.setBorder(new EmptyBorder(32, 32, 32, 32));
dialog.add(entryPane);
JButton okayButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
okayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entryPane.putClientProperty("state", State.OK);
dialog.dispose();
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entryPane.putClientProperty("state", State.CANCEL);
dialog.dispose();
}
});
JPanel actionPane = new JPanel();
actionPane.setBorder(new EmptyBorder(8, 8, 8, 8));
actionPane.add(okayButton);
actionPane.add(cancelButton);
dialog.add(actionPane, BorderLayout.SOUTH);
dialog.getRootPane().setDefaultButton(okayButton);
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
State state = (State) entryPane.getClientProperty("state");
// Only process the information if the user selected "okay"
if (state != null && State.OK == state) {
System.out.println("Validate user input");
// Do what ever validation you need
boolean isValid = true;
if (isValid) {
String importantStuff = entryPane.getImportantStuff();
return new FormData(importantStuff);
}
} else {
System.out.println("User cancelled operation");
}
return null;
}
}
}