problem with focus for JList
Hi all.
I have created a Jlist and added it to a JWindow to see it on the screen.
Now I want that when I press "up" or "down" the selected item of the list changes. This should be automatic I think, but it isn't.
I think the problem is due to the fact that the JWindow/JList doesn't have the focus.
How can I solve the problem?
Thank you in advance!
[389 byte] By [
Mich24a] at [2007-11-26 13:59:45]

# 3
I tried this and as soon as the window appears, the JList has the focus and up and down keys move the selection :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TestJList extends JPanel {
public TestJList() {
}
public static JFrame createFrame() {
JFrame frame = new JFrame();
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
return frame;
}
public static void main(String s[]) {
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
JPanel jtest = new JPanel();
JFrame frame = createFrame();
dataList.setSelectedIndex(2);
jtest.add(new JTextField(10));
jtest.add(dataList);
jtest.add(new JTextField(10));
frame.setTitle("TestJList");
frame.getContentPane().add(jtest, BorderLayout.CENTER);
frame.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(0,0);
frame.setResizable(true);
frame.setVisible(true);
dataList.requestFocus();
}
}
ProZa at 2007-7-8 1:40:42 >

# 4
Yes, but you have used a JFrame, i have tried with a JWindow.
I also have tried with a JFrame and it has worked, but I don't want to use JFrame for two reasons:
- the JFrame's icon appears in the Start bar
- I don't want to have the three button in the upper right corner (minimize, fullscreen, close)