Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

Can the content of JComboBox be RightAlignment

Thread view: 
cl - 27 Feb 2006 09:21 GMT
Hi,everybody

As the title, is there any methods?
I just tried, but no finding.

regards,
poorrichard
Roland de Ruiter - 27 Feb 2006 11:31 GMT
> Hi,everybody
>
[quoted text clipped - 3 lines]
> regards,
> poorrichard

A ListCellRenderer takes care of rendering the combobox items. Class
BasicComboBoxRenderer (the default implementation of the
ListCellRenderer interface) extends JLabel. For right alignment, you
could try the following code

<untested>

JComboBox yourComboBox = new JComboBox();

ListCellRenderer itemRenderer = yourComboBox.getRenderer();

if (itemRenderer instanceof JLabel) {
   JLabel itemRendererLabel = (JLabel)itemRenderer;
   itemRendererLabel.setHorizontalAlignment(SwingConstants.RIGHT);
}

</untested>

Regards,

Roland
ph@semm.tmfweb.nl - 27 Feb 2006 12:13 GMT
This is what I could think of.
    cb.setRenderer(new MyCellRenderer());
.....
    class MyCellRenderer extends JLabel implements ListCellRenderer {
        Color bg;
        public MyCellRenderer() {
           super("", JLabel.RIGHT );
            setOpaque(true);
            bg = this.getBackground();
        }
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus)
        {
            setText(value.toString());
            setBackground(isSelected ? Color.YELLOW : bg );
            return this;
        }
    }
Color handling could be improved.
Anyone know an easier/shorter way ?
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Andrey Kuznetsov - 27 Feb 2006 14:00 GMT
> Color handling could be improved.
> Anyone know an easier/shorter way ?
probably you may extend DefaultListCellRenderer instead.

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

poorichard@gmail.com - 28 Feb 2006 01:01 GMT
Dear people,

I tried your method, and it works.
Or to implements ListCellRenderer .

thinks all,
Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.