2010-04-13 80 views
1

(問題出現在Ubuntu只在Windows工作正常。我不Linux等其他環境都知道)問題集中的JTextField

我已經使用了的ComponentListener的方法在對話中呼籲焦點JTextField中,但對於這種情況只是不工作,我不知道爲什麼。它顯示文本字段中的焦點並快速切換到按鈕。運行並看:

import java.awt.Component; 
import java.awt.GridLayout; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 

import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class User { 

    private String username = ""; 
    private String password = ""; 

    public User() { 
    // default constructor 
    } 

    public User(String username, String password) { 
    this.username = username; 
    this.password = password; 
    } 

    /** Create a panel containing the componet and tha label. */ 
    public JPanel createLabeledComponent(JLabel label, Component comp) { 
    GridLayout layout = new GridLayout(2, 1); 
    JPanel panel = new JPanel(layout); 
    panel.add(label); 
    panel.add(comp); 
    label.setLabelFor(comp); 
    return panel; 
    } 

    public void showEditDialog() { 

    JLabel usernameLbl = new JLabel(username); 
    final JTextField usernameField = new JTextField(); 
    usernameField.setText(username); 
    JPanel usernamePnl = createLabeledComponent(usernameLbl, usernameField); 

    JLabel passwordLbl = new JLabel(password); 
    JPasswordField passwordField = new JPasswordField(password); 
    JPanel passwordPnl = createLabeledComponent(passwordLbl, passwordField); 

    Object[] fields = { "User:", usernamePnl, "Password:", passwordPnl }; 

    JOptionPane optionPane = new JOptionPane(fields, JOptionPane.PLAIN_MESSAGE, 
     JOptionPane.OK_CANCEL_OPTION, null, null); 
    JDialog dialog = optionPane.createDialog("User Data"); 

    dialog.addComponentListener(new ComponentAdapter() { 
     @Override 
     public void componentShown(ComponentEvent e) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
      usernameField.requestFocusInWindow(); 
      } 
     }); 
     } 
    }); 

    dialog.setVisible(true); 
    } 

    public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
     new User().showEditDialog(); 
     } 
    }); 
    } 

} 

任何想法如何解決這個問題?

--update

現在一切都在美國東部時間運行。可悲的是,同樣的行爲。

順便說一句,使用JOptionPane構造函數的最後一個參數(Object initialValue)不起作用。

+0

感謝發佈代碼+1, createDialog(字符串)方法沒有找到:-) – 2010-04-13 20:14:09

+0

@ringbearer,javadocs注意該方法是'自1.6',我猜你正在使用JRE比那更老? :) – 2010-04-13 20:16:25

+0

對。我在RHEL上使用JDK 1.5。 但是,我最好的選擇是JOPtionPane有一個內部的FocusManager,導致Tom的設置被覆蓋。 JOptionPane可能不是用這種行爲啓動對話框的好方法。 – 2010-04-13 21:30:22

回答

1

我記得有類似的問題,我曾經在這個頁面的底部找到了解決辦法:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5018574

+0

看起來像計時器是唯一在這裏工作的解決方法。可悲的是不是一個一致的解決方案(可以在電腦上工作,但不在另一臺電腦上工作,並且可能取決於電腦的速度)無論如何,感謝您的鏈接。 – 2010-04-19 12:45:04

+0

我正在使用windowActivated方法中的Timer。不知道是否能在任何地方工作,但看起來像現在最好的答案。 – 2010-04-19 13:03:02

0

也許這個Dialog Focus解決方案將在Ubuntu上工作(我無法測試它)。

它顯示文本字段 中的焦點並快速切換到按鈕。

或者您可以嘗試在SwingUtilities.invokeLater()中調用requestFocusInWindow()方法調用以將您的請求放在EDT的末尾。

+0

問題已更新 – 2010-04-19 12:29:41

+0

所以你說這個解決方案在你的環境中不起作用?這將是很好的知道,所以我可以更新上述鏈接,如果它不適用於所有環境。如果我的解決方案不能像發佈那樣工作,那麼下一步就是將焦點代碼包裝到Swing utilities.invokeLater()中,以便將它添加到EDT的末尾,並且可能在將焦點重置爲第一場。 – camickr 2010-04-19 15:43:15

0

How to Use the Focus Subsytem

的焦點究竟如何窗口收益取決於開窗系統。在所有平臺上都沒有萬無一失的方法來確保窗口獲得關注。

此外,從Component.requestFocusInWindow

盡一切努力將作出該請求;但是,在某些情況下,可能無法這樣做。開發人員不得假定此組件是焦點所有者,直到此組件收到FOCUS_GAINED事件。

在致電requestFocusInWindow之前,您的組件可能不會實現。您是否曾嘗試在setVisible(true)之前放置dialog.pack();

+0

dialog.pack()沒有區別。 – 2010-04-19 12:30:24

+0

使用焦點偵聽器也不能解決.. – 2010-04-19 12:32:08