简体   繁体   中英

Can't focus on JTextField in JDialog

I'm trying to focus on a JTextField in a JDialog; I've tried all the suggestions in this link after poking around for similar questions, but none of the suggestions worked.

The component I'm trying to focus on is created in a class called "InputTaskWindow", and I'm trying to create a dialog from a class called "MainWindow". Here's where the issue is (This is within "MainWindow"):

public  class NewTask extends AbstractAction {

JList mTaskList;
JTextArea mDetailsTextArea;

public NewTask (JList tl, JTextArea dta){
    mTaskList = tl;
    mDetailsTextArea = dta;
}

public void actionPerformed(ActionEvent e) {
    int option = 0;
    InputTaskWindow inputTaskWindow = new InputTaskWindow();
    JOptionPane optionPane = new JOptionPane(inputTaskWindow.createComponents(), JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);

    JDialog dialog = optionPane.createDialog("New Task");

    dialog.setSize(200, 200);

    dialog.pack();

    ((JTextField) inputTaskWindow.getComponent(inputTaskWindow.NAMETF)).requestFocus();

    dialog.setVisible(true);

    if (optionPane.getValue() != null){
        option = (Integer)optionPane.getValue();
    }

    if (option == JOptionPane.OK_OPTION){
        Task t = inputTaskWindow.getTask();
        activeDay.addTask(t);
    }

    populateLists(mTaskList);
}

I put the JTextField I'm trying to access in an array of JComponents in the GUI class; I've accessed other components using the getComponents() method, so I know that's pointing to the correct thing.

I've tried requestFocus(), requestFocusInWindow(), adding an ancestorListener in the NewTask class, and adding an ancestorListener upon the creation of the JTextField in InputTaskWindow (in both cases I'm adding it to the JTextField and using the RequestFocusListener class described in the link). Any help would be much appreciated.

I haven't determined what the problem is, but I've decided to abandon this code. I strongly suspect the problem has to do with a refresh function, not a misunderstanding of focus methods (my first assumption). The code is too tangled to warrant sorting out, so I'm considering the question closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM