There are many great Swing components around, but we could not find a nice combobox for font selection, that includes not only the font names but also some preview characters and maybe a history of the recently selected fonts. You know such a combobox from OpenOffice.org and other word processors.
Well, here it is now. Our FontChooserComboBox
offers font preview, customizable preview strings, optionally a list of the recent fonts and even auto completion for the font name. And best of all, the code is public domain, so you can use it for whatever you want. However, if you improve it, please share your work with us!
Here is a demo video:
A webstart demo:
Here are the files:
- FontChooserComboBox.java (just the component, which is a single Java file)
- FontChooserComboBox.zip (Eclipse project with demo frame)
Comments are welcome 🙂
Hi, I will use this for my project: http://code.google.com/p/tellapic
Is this GPL?
Best regards,
I had to comment out this line:
// @Override public Dimension getPreferredSize()
// {
// //default height: like a normal combo box
// return new Dimension(super.getPreferredSize().width, new JComboBox().getPreferredSize().height);
// }
Why would you do that? I needed a ComboBox smaller in a JPanel. I create a Dimension according the width of the strings, and a custom panel height.
Cheers,
Hi Sebastián,
the source code tells you: “This file is public domain. However, if you improve it, please share your work with … Thanks!” 🙂
I don’t remember the reason for getPreferredSize(), but it really looks strange! 😉 Have fun with the component.
Bye,
Andi
I am working on a font editor with font, style, size, color and took your code. It works well with what I am doing so I copied and made some edits to allow selecting the style (bold, italic) but it is giving me a strange bug where I cant seem to select a style when the editor is enabled. When I disable the editor, though, the selection works but the text at the top of the box become off center vertically. Ant Ideas?
fixed the centering problem. Replacing
this.setLayout(new FlowLayout(FlowLayout.LEFT));
in the Item class with
FlowLayout f = new FlowLayout(FlowLayout.LEFT);
f.setVgap(0);
this.setLayout(f);
solves that one.
Thank you very much for sharing your work!
Thank you for sharing your helpful work 🙂 !