Friday, December 3, 2010

Call Font Face into ComboBox

Long time no write off my blog. Today wanted to write, how to call the font and inserted into the ComboBox in Java. Code more or less like this:

import java.awt.BorderLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class clCombo extends javax.swing.JFrame {
private JPanel jPanel1;
private JComboBox jComboBox1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
clCombo inst = new clCombo();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public clCombo() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
{
ComboBoxModel jComboBox1Model = new DefaultComboBoxModel();
jComboBox1 = new JComboBox();
jPanel1.add(jComboBox1);
jComboBox1.setModel(jComboBox1Model);
jComboBox1.setPreferredSize(new java.awt.Dimension(288, 21));
Font[] fonts = callFont();
for (Font f : fonts) {
jComboBox1.addItem(f.getFontName());
}
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
public Font[] callFont() {
GraphicsEnvironment e = GraphicsEnvironment
.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts(); // Get the fonts
return fonts;
}
}


output :




if you trouble in making the GUI in Java, you can use Jigloo to help make the creation GUI. Congratulations to give it a try.

No comments:

Post a Comment