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 / August 2008

Tip: Looking for answers? Try searching our database.

Simple JComboBox query

Thread view: 
Captain Haddock - 17 Mar 2005 11:09 GMT
hi,

how do I change the font of individual combo box items, say if I have 5
items in a combo box , how can I have each of them in a different font ? I
can do combo.setFont(Font f) but is there any way I can set the font of the
combo Items ? can anyone help me out please?

thanks
John McGrath - 17 Mar 2005 13:43 GMT
> how do I change the font of individual combo box items, say if I have 5
> items in a combo box , how can I have each of them in a different font ?
> I can do combo.setFont(Font f) but is there any way I can set the font
> of the combo Items ? can anyone help me out please?

Create a ListCellRenderer and install in using JComboBox.setRenderer().

Signature

Regards,

John McGrath

vaidhu - 21 Aug 2008 13:38 GMT
vaidhu wrote:
>hi,

how do I change the font of individual combo box items, say if I have 5
items in a combo box , how can I have each of them in a different font ? I
can do combo.setFont(Font f) but is there any way I can set the font of the
combo Items ?

Also,In my combobox  i want to display more than one item of every font type.
I want the source code or logic.

Note:

using list cell renderer it is giving only one item of every font type.But i
want more than one item of every font in one combobox.

This is my code which need changes

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class ComboBoxFonts extends JFrame implements ItemListener
{
// JTextArea textArea;
JComboBox comboBox;

public Font abiFont = new Font("Arial Black Italic",Font.PLAIN,20);
public Font bmoFont = new Font("Book Man Old Style Bold",Font.BOLD,20);
public Font cniFont = new Font("Courier New Italic",Font.PLAIN,20);
public Font tnrFont = new Font("Times New Roman",Font.PLAIN,20);
public Font verFont = new Font("Verdana Bold",Font.BOLD,20);
public Font webFont = new Font("Webdings",Font.BOLD,20);

public ComboBoxFonts()
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
Font [] fonts = ge.getAllFonts ();
Font [] example = {abiFont,bmoFont,cniFont,tnrFont,verFont,webFont};
//The above example array contains the order in which the fonts are displayed
in our ComboBox.
comboBox = new JComboBox(example);
comboBox.setRenderer( new MyFontRenderer() );
comboBox.addItemListener( this );
getContentPane().add( comboBox, BorderLayout.NORTH );
comboBox.setBounds(162,42,180,25);
}

public void itemStateChanged(ItemEvent e)
{
Font font = (Font)e.getItem();
//textArea.setFont( font.deriveFont( textArea.getFont().getSize2D() ) );
comboBox.setFont( font.deriveFont( comboBox.getFont().getSize2D() ) );
}

public static void main(String[] args)
{
ComboBoxFonts frame = new ComboBoxFonts();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
class MyFontRenderer extends BasicComboBoxRenderer
{

public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)

{
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);

Font font = (Font)value;
String fontName = font.getFontName();
//setFont( font.deriveFont(12.0f) );

int size = font.getSize();
System.out.println("fontName : "+fontName+" size : "+size);

if(fontName.equals("Arial Black Italic"))
{
setFont( font.deriveFont(100));
setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)
83+""+(char)72);
//setText("Welcome To Geneva Software Technologies Pvt ltd");
return this;
}

else if(fontName.equals("Times New Roman"))
{
setFont( font.deriveFont(100));
setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)
83+""+(char)72);
setText(""+(char)99+""+(char)117+""+(char)104+""+( char)122+""+(char)77+""+
(char)73+""+(char)102);
return this;
}

else if(fontName.equals("Webdings"))
{
//Webdings font is the last font displayed in our ComboBox drop down list
setFont( font.deriveFont(100));
setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)
83+""+(char)72);
return this;
}
else if(fontName.equals("Verdana Bold"))
{
setFont( font.deriveFont(100));
setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)
83+""+(char)72);
return this;
}
else if(fontName.equals("Book Man Old Style Bold"))
{
setFont( font.deriveFont(100));
setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)
83+""+(char)72);
return this;
}
else
{
setFont(font.deriveFont(100));
setText(""+(char)69+""+(char)78+""+(char)71+""+(ch ar)76+""+(char)73+""+(char)
83+""+(char)72);
return this;
}

}
}

}


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.