Java Swing: focus issues

I'm making a level editor for my game I have a property panel where I can modify the properties of the selected object I also have a save button to write level XML

When the editor component loses focus or presses enter, the field edit (*) is committed This works, but the only problem is, when I have this series of actions:

>Edit a field > press Save button

Because, what happens:

>I edit the field > I press the Save button > level saved > the field has lost focus > Edit submitted

As you can see, this is the wrong order Of course, I want this field to lose focus, which will result in a commit and then save level

Is there a trick, hacker or solution to make the field lose focus first, and then execute the action listener of the Save button?

Thank you in advance

(* SUBMIT = field is also edited in the object attribute)

Edit: for the field of focusadapter where I am using focusadost:

FocusAdapter focusAdapter = new FocusAdapter()
{

    @Override
    public void focusLost(FocusEvent e)
    {
        compProperties.setProperty(i,getColor());
        record(); // For undo-redo mechanism
    }
};

For buttons, a simple actionlistener and actionperformed '

btnSave.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
         // Save the level
     }
});

Solution

Mmm... Not reproducible: in the following clip, missing content is always notified before actionperfomed, independent of whether I click a button or use a mnemonic:

final JTextField field = new JTextField("some text to change");
    FocusAdapter focus = new FocusAdapter() {

        @Override
        public void focusLost(FocusEvent e) {
            LOG.info("lost: " + field.getText());
        }

    };
    field.addFocusListener(focus);

    Action save = new AbstractAction("save") {

        @Override
        public void actionPerformed(ActionEvent e) {
            LOG.info("save: " + field.getText());
        }
    };
    save.putValue(Action.MNEMONIC_KEY,KeyEvent.VK_S);
    JButton button = new JButton(save);
    JComponent @R_125_2419@ = @R_125_2419@.createHorizontal@R_125_2419@();
    @R_125_2419@.add(field);
    @R_125_2419@.add(button);

On the other hand, the focus is on the tricky properties of dependency, and sorting may be system dependent (my vision is victory) Check how code snippets behave on your code

>If you see the same sequence as me, the problem lies elsewhere > if you save before you lose it, try wrapping the save operation in invoker (put it at the end of eventqueue to execute after all pending events)

Action save = new AbstractAction("save") {

    @Override
    public void actionPerformed(ActionEvent e) {
        SwingUtilities.invokelater(new Runnable() {
            public void run() {
                LOG.info("save: " + field.getText());
            }
        });
    }
};
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>