2011-12-15 116 views
6

我有主要的应用程序,其中有值的表。然后,我点击“添加”按钮,新的自定义(我自己创建)JDialog类型弹出窗口。在那里我可以输入价值,做一些滴答,然后点击“确认”。所以我需要从对话框读取该输入,所以我可以将此值添加到主应用程序中的表中。 当按下“确认”按钮时,我该如何收听,这样我才可以读取该值?JDialog的动作监听器点击按钮

addISDialog = new AddISDialog(); 
addISDialog.setVisible(true); 
addISDialog.setLocationRelativeTo(null); 
//somekind of listener... 
//after "Confirm" button in dialog was pressed, get value 
value = addISDialog.ISName; 
+0

我实现监听器里的JDialog,我可以听那个对话框内按钮,但我需要听那个按钮之外对话 - 在主应用程序,在那里我称之为该对话框 – 2011-12-15 16:45:50

+1

您可以编辑`JDialog`类?如果是这样,你可以将`ActionEvent`转发给另一个实现`ActionListener`接口的类,并且这个类可以做你想做的事。 – mre 2011-12-15 16:47:17

+0

我做AddISDialog自己(公共类AddISDialog扩展的JDialog实现的ActionListener)所以是的,我可以对其进行编辑。你是什​​么意思转发ActionEvent到另一个类?我该怎么做? – 2011-12-15 16:51:09

回答

12

如果对话框将在用户按下后消失确认:

  • ,你希望拥有的对话框表现为一个模式的JDialog,那么它很容易,因为你知道在哪里只要用户完成对话框,程序就会执行代码 - 在对话框中调用setVisible(true)后就会立即生效。因此,在对话框中调用setVisible(true)之后,您只需立即在代码行中查询对话框对象的状态。
  • 如果你需要处理一个非模态对话框,那么你需要添加一个WindowListener到对话框的窗口变得不可见时通知的对话框。

如果对话框是保持开放后,用户按下确认:

  • 那么你应该使用一个PropertyChangeListener如上面已经建议。或者给对话对象一个公共方法,允许外部类将ActionListener添加到确认按钮。

如需了解更多详情,敬请显示您的代码相关的部分或甚至更好的sscce

例如允许JDialog类的接受外界的听众,你可以给它一个JTextField和一个JButton:

class MyDialog extends JDialog { 
    private JTextField textfield = new JTextField(10); 
    private JButton confirmBtn = new JButton("Confirm"); 

并允许外部类一个ActionListener添加到按钮的方法:

public void addConfirmListener(ActionListener listener) { 
    confirmBtn.addActionListener(listener); 
} 

然后,外部类可以简单地调用addConfirmListener(...)方法将其ActionListener添加到confirmBtn。

例如:

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class OutsideListener extends JFrame { 
    private JTextField textField = new JTextField(10); 
    private JButton showDialogBtn = new JButton("Show Dialog"); 
    private MyDialog myDialog = new MyDialog(this, "My Dialog"); 

    public OutsideListener(String title) { 
     super(title); 
     textField.setEditable(false); 

     showDialogBtn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      if (!myDialog.isVisible()) { 
       myDialog.setVisible(true); 
      } 
     } 
     }); 

     // !! add a listener to the dialog's button 
     myDialog.addConfirmListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String text = myDialog.getTextFieldText(); 
      textField.setText(text); 
     } 
     }); 

     JPanel panel = new JPanel(); 
     panel.add(textField); 
     panel.add(showDialogBtn); 

     add(panel); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(400, 300); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new OutsideListener("OutsideListener"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

class MyDialog extends JDialog { 
    private JTextField textfield = new JTextField(10); 
    private JButton confirmBtn = new JButton("Confirm"); 

    public MyDialog(JFrame frame, String title) { 
     super(frame, title, false); 
     JPanel panel = new JPanel(); 
     panel.add(textfield); 
     panel.add(confirmBtn); 

     add(panel); 
     pack(); 
     setLocationRelativeTo(frame); 
    } 

    public String getTextFieldText() { 
     return textfield.getText(); 
    } 

    public void addConfirmListener(ActionListener listener) { 
     confirmBtn.addActionListener(listener); 
    } 
} 

注意事项,虽然我不建议继承的JFrame或JDialog的,除非绝对必要的。这只是为了简洁起见。我也自己喜欢使用模态对话框来解决这个问题,并在需要时重新打开对话框。

编辑2
使用模态对话框的一个例子:

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class OutsideListener2 extends JFrame { 
    private JTextField textField = new JTextField(10); 
    private JButton showDialogBtn = new JButton("Show Dialog"); 
    private MyDialog2 myDialog = new MyDialog2(this, "My Dialog"); 

    public OutsideListener2(String title) { 
     super(title); 
     textField.setEditable(false); 

     showDialogBtn.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      if (!myDialog.isVisible()) { 
       myDialog.setVisible(true); 

       textField.setText(myDialog.getTextFieldText()); 
      } 
     } 
     }); 

     JPanel panel = new JPanel(); 
     panel.add(textField); 
     panel.add(showDialogBtn); 

     add(panel); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(400, 300); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new OutsideListener2("OutsideListener"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

class MyDialog2 extends JDialog { 
    private JTextField textfield = new JTextField(10); 
    private JButton confirmBtn = new JButton("Confirm"); 

    public MyDialog2(JFrame frame, String title) { 
     super(frame, title, true); // !!!!! made into a modal dialog 
     JPanel panel = new JPanel(); 
     panel.add(new JLabel("Please enter a number between 1 and 100:")); 
     panel.add(textfield); 
     panel.add(confirmBtn); 

     add(panel); 
     pack(); 
     setLocationRelativeTo(frame); 

     ActionListener confirmListener = new ConfirmListener(); 
     confirmBtn.addActionListener(confirmListener); // add listener 
     textfield.addActionListener(confirmListener); 
    } 

    public String getTextFieldText() { 
     return textfield.getText(); 
    } 

    private class ConfirmListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
     String text = textfield.getText(); 
     if (isTextValid(text)) { 
      MyDialog2.this.setVisible(false); 
     } else { 
      // show warning 
      String warning = "Data entered, \"" + text + 
       "\", is invalid. Please enter a number between 1 and 100"; 
      JOptionPane.showMessageDialog(confirmBtn, 
        warning, 
        "Invalid Input", JOptionPane.ERROR_MESSAGE); 
      textfield.setText(""); 
      textfield.requestFocusInWindow(); 
     } 
     } 
    } 

    // true if data is a number between 1 and 100 
    public boolean isTextValid(String text) { 
     try { 
     int number = Integer.parseInt(text); 
     if (number > 0 && number <= 100) { 
      return true; 
     } 
     } catch (NumberFormatException e) { 
     // one of the few times it's OK to ignore an exception 
     } 
     return false; 
    } 

} 
-1
import javax.swing.JOptionPane; 

,或者如果你已经摆

import javax.swing.*; 

将你覆盖。

条件触发后的JOptionPane送你的警告或任何模式消息:

JOptionPane.showMessageDialog(
      null, 
      "Your warning String: I can't do that John", 
      "Window Title", 
      JOptionPane.ERROR_MESSAGE); 

检查JOptionPane的选项*确定消息类型。

0
//Why is this working so well even without the ActionListener interface, and all I'm 
//getting is minor format errors at the end brackets? Know how to fix it? 



final JButton newButton = new JButton("Send"); 
      newButton.setActionCommand("Send"); 
      textPane.add(newButton); 
      newButton.setEnabled(true); 
      newButton.addActionListener(new ActionListener(); 


        public void actionPerformed(ActionEvent e){ 

        // display/center the jdialog when the button is pressed 
        JDialog digFree = new JDialog(digFree, "Hello", true); 
        digFree.setLocationRelativeTo(newButton); 
        digFree.setFocusable(true); 
        digFree.setVisible(true); 
        digFree.setBounds(20, 20, 100, 120); 
        } 
0

为什么你不检查你的jDialog是否可见?

yourJD.setVisible(true); 
while(yourJD.isVisible())try{Thread.sleep(50);}catch(InterruptedException e){} 

这也适用。