JDialog的动作监听器,用于单击按钮

时间:2011-12-15 16:36:40

标签: java swing jbutton actionlistener jdialog

我有主要应用程序,其中表是值。然后,我点击“添加”按钮,新的CUSTOM(我自己制作)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;

4 个答案:

答案 0 :(得分:13)

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

  • 并且您希望对话框的行为与 模态 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 :(得分: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);
                  }

答案 2 :(得分:0)

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

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

这也有效。

答案 3 :(得分:-2)

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。*选项以确定消息类型。