Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4061 > unrolled thread

ultimate chalenge in JCom

Started by"vaidhu" <vaidhu@THRWHITE.remove-dii-this>
First post2011-04-27 15:48 +0000
Last post2011-04-27 15:48 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  ultimate chalenge in JCom "vaidhu" <vaidhu@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
    Re: ultimate chalenge in "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
      Re: ultimate chalenge in "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
        Re: ultimate chalenge in "vaidhu" <vaidhu@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000

#4061 — ultimate chalenge in JCom

From"vaidhu" <vaidhu@THRWHITE.remove-dii-this>
Date2011-04-27 15:48 +0000
Subjectultimate chalenge in JCom
Message-ID<890abd332fe0a@uwe>
  To: comp.lang.java.gui
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.

Example:
items 1,2,3--"Arial"
items 4,5,6--"Times New Roman"
items 7,8,9--"verdana"
items 10,11,12--Again "Times New Roman"




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;
}

}
}

}

-- 
Message posted via http://www.javakb.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#4065 — Re: ultimate chalenge in

From"John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this>
Date2011-04-27 15:48 +0000
SubjectRe: ultimate chalenge in
Message-ID<nospam-27FA94.06213022082008@aioe.org>
In reply to#4061
  To: comp.lang.java.gui
In article <890abd332fe0a@uwe>, "vaidhu" <u45630@uwe> wrote:

[...]
> 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.
[...]

You might look at this:

<http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer>

Let MyFontRenderer extend JLabel and implement ListCellRenderer.
Let it contain sufficient data to map the item index to your
desired font. Set the desired font in getListCellRendererComponent().

-- 
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#4068 — Re: ultimate chalenge in

From"John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this>
Date2011-04-27 15:48 +0000
SubjectRe: ultimate chalenge in
Message-ID<nospam-1E1B7A.10544522082008@web.aioe.org>
In reply to#4065
  To: comp.lang.java.gui
In article <nospam-27FA94.06213022082008@aioe.org>,
 "John B. Matthews" <nospam@nospam.invalid> wrote:

> In article <890abd332fe0a@uwe>, "vaidhu" <u45630@uwe> wrote:
> 
> [...]
> > 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.
> [...]
> 
> You might look at this:

<http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer>

> Let MyFontRenderer extend JLabel and implement ListCellRenderer. Let 
> it contain sufficient data to map the item index to your desired 
> font. Set the desired font in getListCellRendererComponent().

For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author John B. Matthews
 */
public class FontCombo extends JPanel {

  private static Font dialog = new Font("Dialog", Font.PLAIN, 18);
  private static Font mono = new Font("Monospaced", Font.PLAIN, 18);
  private static Font sans = new Font("SansSerif", Font.PLAIN, 18);
  private static Font serif = new Font("Serif", Font.PLAIN, 18);
  private JComboBox choices;

  public FontCombo() {
    this.setLayout(new BorderLayout());

    choices = new JComboBox();
    choices.addItem(new FontComboItem("Dialog one", dialog));
    choices.addItem(new FontComboItem("Dialog two", dialog));
    choices.addItem(new FontComboItem("Monospaced", mono));
    choices.addItem(new FontComboItem("SansSerif", sans));
    choices.addItem(new FontComboItem("Serif one", serif));
    choices.addItem(new FontComboItem("Serif two", serif));
    choices.setRenderer(new FontCellRenderer());
    choices.setSelectedIndex(2);
    choices.setFont(mono);
    choices.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int index = choices.getSelectedIndex();
        FontComboItem item =
            (FontComboItem) choices.getItemAt(index);
        choices.setFont(item.font);
      }
    });

    this.add(choices, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new FontCombo());
        frame.setSize(250, 125);
        frame.setVisible(true);
      }
    });
  }
}

class FontCellRenderer extends JLabel implements ListCellRenderer {

  public FontCellRenderer() {
    setOpaque(true);
  }

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

    FontComboItem item = (FontComboItem) value;
    setText(item.text);
    setFont(item.font);
    setBackground(isSelected ? Color.gray : Color.white);
    setForeground(isSelected ? Color.white : Color.black);
    return this;
  }
}

class FontComboItem {
  public String text;
  public Font font;
  
  public FontComboItem(String text, Font font) {
    this.text = text;
    this.font = font;
  }
}

-- 
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#4079 — Re: ultimate chalenge in

From"vaidhu" <vaidhu@THRWHITE.remove-dii-this>
Date2011-04-27 15:48 +0000
SubjectRe: ultimate chalenge in
Message-ID<892ef5b10831c@uwe>
In reply to#4068
  To: comp.lang.java.gui
Thanks for the logic.I appreciate your efforts,
                                            Thanks man.

-- 
Message posted via http://www.javakb.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web