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]
# 1
Did you tryed the requestFocus method (from JComponent) to grab it ?
ProZa at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 2
yes I have tried, but has not worked....
Mich24a at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 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 > top of Java-index,Desktop,Core GUI APIs...
# 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)

Mich24a at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 5
You have to use an owner to create your JWindow if you want it focusable (look in API doc)Then key events will be pass throught the JWindow and you will be able to use UP and DOWN keys in the JList.
ProZa at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanx, your are right... I have just read in the official documentation.The problem now is how to obtain the owner frame... is there a "general method"? (suppose I have a Jcomponent and I want to find the owning frame... is that possible?)
Mich24a at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 7
getTopLevelAncestor() ?Else can you post a simple code ?
ProZa at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 8
I mean: I have to create the JWindow passing it the owner frame.But how can I get this frame? (I can access to the currently visible JComponent)With getTopLevelAncestor() I only obtain another container...
Mich24a at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 9
What is the type of your container ? Can't you cast it in a JFrame of Window type ?
ProZa at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 10
Try using a JDialog instead and use:dialog.setUndecorated(true);
Rodney_McKaya at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 11
great advice, thx very much!
Mich24a at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...
# 12
When you use a dialog you don't get an icon on the task bar so its easy to lose the dialog. If you need an icon then this posting shows a way to get around the problem: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=675445
camickra at 2007-7-8 1:40:42 > top of Java-index,Desktop,Core GUI APIs...