File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Need Guru's help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Guru Watch "Need Guru New topic

Need Guru's help!!

xiao li

Joined: Apr 09, 2002
Posts: 13
Hi Swing Gurus,
I need to show the labels with its mnemonic key in a scollPane. but it does not work(neither label nor mnemonic key is shown up).
here is my sample code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
class MnemonicTest extends JDialog
JLabel label1 = new JLabel("A");
JLabel label2 = new JLabel("B");
JLabel label3 = new JLabel("C");
Vector vector = new Vector();
JList list;
JScrollPane scrollpane;

public MnemonicTest ()
list = new JList(vector);
scrollpane= new JScrollPane(list);
setSize(400, 100);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.CENTER, scrollpane);
public static void main(String ag[])
MnemonicTest mt = new MnemonicTest ();
Thanks in advance!!
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
I'm not exactly sure on this but, my understanding of JScrollPane is that it was designed to scroll ONE Object, not many. If you want to Scroll something with 3 Components (in your case lables) then you should add those to a panel and have the JScrollPane scroll that.
Heres the fixed code:

The reason why you don't want add the Lables to a JList is because a JList is designed to have text. So when you add Objects it will simply display the String representation of it. So when you added those labels all it did was call its toString() and that was what you were seeing.
Hope this helps
[ April 16, 2002: Message edited by: Ibrahim Hashimi ]

SCJP 1.4<br /><a href="" target="_blank" rel="nofollow"></a>
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi xiao,
You need to create your own cell renderer to do that. You can then tailor the list created label to match the one you are sending in. The code below shows a cell renderer to do what you want.

To use it you just need to add a new line to your code, immediately after you create your JList:
list.setCellRenderer( new MyCellRenderer() );
I agree. Here's the link:
subject: Need Guru's help!!
jQuery in Action, 3rd edition