wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Guru Watch "Need Guru New topic
Author

Need Guru's help!!

xiao li
Greenhorn

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 ()
{
label1.setDisplayedMnemonic('A');
label2.setDisplayedMnemonic('B');
label3.setDisplayedMnemonic('C');
vector.addElement(label1);
vector.addElement(label2);
vector.addElement(label3);
list = new JList(vector);
scrollpane= new JScrollPane(list);
setSize(400, 100);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.CENTER, scrollpane);
setVisible(true);
}
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="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</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() );
Regards,
Manfred.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need Guru's help!!
 
Similar Threads
Would my code help?!
Icons and a Label
JScrollpane problem
help!!!a problem about java gui
How to show the label with mnemonic key in a scrollPane?