• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Need Guru's help!!

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic