2

I would make a little rft text editor in Java and I would have that the different kind of fonts are show in my combo box in that font. The reason is because the user can see that font.

I know that you can use a combo box item for C#. But I didn't know for Java.

Can anyone help me?

Edit:

This is what I want more or less

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

2 Answers2

3

Sounds like you need to implement a custom renderer for your combo box. See the java tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

i.e. Something like this (assuming the Objects in your combo box are Fonts):

class CustomRenderer implements ListCellRenderer
{

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

        JLabel label = new JLabel();
        label.setFont(((Font) value).deriveFont(12f));
        label.setText(((Font) value).getFontName());
        return label;
    }

}
Amber
  • 2,413
  • 1
  • 15
  • 20
  • It is unnecessary and wasteful to create the label every time the method is called.. – Andrew Thompson Jan 22 '15 at 01:33
  • My original answer gives you the right idea... But Andrew is correct, it is unneccesary to create a new JLabel every time. To avoid creating a new JLabel, you could make CustomRenderer extend JLabel in addition to implementing ListCellRenderer and then return "this" instead of a new label. Or just make the label a field in CustomRenderer. – Amber Jan 22 '15 at 15:44
  • btw. how place you the icons? –  Jan 22 '15 at 18:02
  • See [JLabel.setIcon](http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setIcon(javax.swing.Icon)) . For example: label.setIcon(new ImageIcon(fileName)); – Amber Jan 26 '15 at 15:11
1

Custom Combo box for font selection in java

Here is code :

package stack;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class CustomComboBox {
  JComboBox fontComboBox;
  JFrame frame;
  String fontName[];
  Integer array[];

  public CustomComboBox() {
    JFrame.setDefaultLookAndFeelDecorated(true);

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    fontName = ge.getAvailableFontFamilyNames();
    array = new Integer[fontName.length];
    for(int i=1;i<=fontName.length;i++) {
      array[i-1] = i;
    }

    fontComboBox = new JComboBox(array);
    ComboBoxRenderar renderar = new ComboBoxRenderar();
    fontComboBox.setRenderer(renderar);

    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(fontComboBox);

    frame.pack();
    frame.setVisible(true);
  }

  public class ComboBoxRenderar extends JLabel implements ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, 
                                                  Object value, 
                                                  int index, 
                                                  boolean isSelected, 
                                                  boolean cellHasFocus) {
      int offset = ((Integer)value).intValue() - 1 ;
      String name = fontName[offset];
      setText(name);
      setFont(new Font(name,Font.PLAIN,20));
      return this;
    }
 }
  public static void main(String args[]) {
    new CustomComboBox();
  }
}

and preview of the code see the Image :enter image description here

Ganesh Patel
  • 450
  • 5
  • 15
  • I don't understand why you went for a `JComboBox` instead of `JComboBox`, but other than that, this is solution works very well. – brimborium Jul 17 '18 at 14:01
  • @brimborium You are right I have to use `Font` instead of `Integer` but when I answer this question I am just learning the `Swing`. – Ganesh Patel Jul 18 '18 at 04:23