It appears to me that tree selection events should happen after focus events, but this doesn't seem to be the case. Assume you have a JTree and a JTextField, where the JTextField is populated by what is selected in the tree. When the user changes the text field, on focus lost, you update the tree from the text field. however, the tree selection is changed before the focus is lost on the text field. this is incorrect, right? Any ideas? Here is some sample code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Focus extends JFrame
{
public static void main(String[] args)
{
Focus f = new Focus();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public Focus()
{
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
final JTextArea ta = new JTextArea(5, 10);
cp.add(new JScrollPane(ta), BorderLayout.SOUTH);
JSplitPane sp = new JSplitPane();
cp.add(sp, BorderLayout.CENTER);
JTree t = new JTree();
t.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent tse)
{
ta.append("Tree Selection changed\n");
}
});
t.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent fe)
{
ta.append("Tree focus gained\n");
}
public void focusLost(FocusEvent fe)
{
ta.append("Tree focus lost\n");
}
});
sp.setLeftComponent(new JScrollPane(t));
JTextField f = new JTextField(10);
sp.setRightComponent(f);
pack();
f.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent fe)
{
ta.append("Text field focus gained\n");
}
public void focusLost(FocusEvent fe)
{
ta.append("Text field focus lost\n");
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}