Actually, I am making a normal text editor which contain a menu "Change" with sub menu "font". so I want to add comboBox of font names in the "font" menu and when I select the value of comboBox the font of the Text pane is changed.Here is my code:
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TextEditor implements ActionListener{
JMenuBar menubar;
JMenu fileMenu;
JMenu changeMenu;
JMenuItem newItem;
JMenuItem editorframe;
JMenu font;
JMenu style;
JMenu size;
JMenu color;
JTextPane textPane;
JScrollPane scrollPane;
JScrollPane contentPane;
/** Contain the list of the font*/
JComboBox<String> box;
/** This array contain font family which are installed on system */
String fontArray[];
JFrame frame;
public TextEditor() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Retrieve all installed Font from system and store them into array
GraphicsEnvironment gg = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontArray = gg.getAvailableFontFamilyNames();
menubar = createMenuBar();
frame.setJMenuBar(menubar);
contentPane = createContentPane();
frame.setContentPane(contentPane);
frame.setVisible(true);
frame.setSize(500,500);
}
/**
* This method create a Menu Bar and Menu and MenuItem in t
* @return menubar MenuBar of frame
*/
public JMenuBar createMenuBar() {
JMenuBar menubar = new JMenuBar();
fileMenu = new JMenu("File");
newItem = new JMenuItem("New");
editorframe = new JMenuItem("EditorFrame");
changeMenu = new JMenu("Change");
//sub menus are used to change the attribute of text of the Text Pane.
font = new JMenu("Font");
style = new JMenu("Style");
size = new JMenu("Size");
color = new JMenu("Color");
//adding all subMenu into changeMenu
changeMenu.add(font);
changeMenu.add(style);
changeMenu.add(size);
changeMenu.add(color);
box = new JComboBox<String>(fontArray);
box.addActionListener(this);
font.add(box);
fileMenu.add(newItem);
fileMenu.add(editorframe);
newItem.addActionListener(this);
menubar.add(fileMenu);
menubar.add(changeMenu);
return menubar;
}
/**
* This method create contentPane for frame and create text pane and add it into contentPane
* @return scrollPane content pane of frame
*/
public JScrollPane createContentPane() {
textPane = new JTextPane();
scrollPane = new JScrollPane(textPane);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return scrollPane;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == box) {
String fontName = box.getSelectedItem().toString();
textPane.setFont(new Font(fontName, Font.PLAIN, 20));
}
}
public static void main(String args[]) {
new TextEditor();
}
}
But the problem is when I add action Listener on the box
(ComboBox) it doesn't work.