0

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.

Ganesh Patel
  • 450
  • 5
  • 15
  • Could you check if `actionPerformed` gets called ? – Arnaud Jul 18 '17 at 09:42
  • Yes i check but `actionPerformed()` never call when i click on he combo box – Ganesh Patel Jul 18 '17 at 09:52
  • 1
    Possible duplicate of [JComboBox submenu items](https://stackoverflow.com/questions/13576600/jcombobox-submenu-items). Her's [another way](https://stackoverflow.com/a/8127463/261156). – Catalina Island Jul 18 '17 at 10:49
  • @Catalina Island issue is `actionListener `is not work when combo box in sub menu – Ganesh Patel Jul 18 '17 at 11:06
  • @GaneshPatel: Right. I mentioned two workarounds. – Catalina Island Jul 20 '17 at 10:15
  • @Catalina Island first solution is about comboBox cotain menu and subMenu, Second is adding comboBox on menu bar, but they don't give me solution because in my program combo box inside a sub Menu. ComboBox seen properly but it doesn't generate ActionEvent when I click on it. – Ganesh Patel Jul 20 '17 at 10:30

0 Answers0