2011-12-15 120 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){} 

這也適用。