Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Guru's help!!

 
xiao li
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic