JComboBox getSelectedIndex() method

Branislav Lazic picture Branislav Lazic · Aug 22, 2012 · Viewed 13.2k times · Source

I was making a simple text editor where you can set font style,font size, clear all etc. To set font size I added JComboBox and implemented ItemListener. Here is my MainWindow class:

import javax.swing.*;

public class MainWindow extends JFrame{
Editor e = new Editor();

public MainWindow(){
    super(".:My Text Editor:.");
    getContentPane().add(e);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            new MainWindow();
        }
    });

}

}

Here is my Editor class:

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Editor extends JPanel{
JPanel optionPanel = new JPanel();
JTextArea editArea = new JTextArea();
JButton boldBtn = new JButton("Bold");
JButton italicBtn = new JButton("Italic");
JButton plainBtn = new JButton("Plain");
JButton clearBtn = new JButton("Clear all");
String [] fontSizes = {"10","11","12","13","14","15","16","17","18","19","20"};
int fontSize;
JComboBox combo = new JComboBox(fontSizes);

public Editor(){
    createUI();
    addEvents();
}

public void createUI(){
    optionPanel.add(boldBtn);
    optionPanel.add(italicBtn);
    optionPanel.add(plainBtn);
    optionPanel.add(combo);
    optionPanel.add(clearBtn);

    setLayout(new BorderLayout());
    add(optionPanel,BorderLayout.NORTH);
    add(new JScrollPane(editArea),BorderLayout.CENTER);
    setPreferredSize(new Dimension(640,480));
}

public void addEvents(){

    boldBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setFont(new Font("Sans Serif",Font.BOLD,fontSize));
        }
    });

    italicBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setFont(new Font("Sans Serif",Font.ITALIC,fontSize));

        }
    });

    plainBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
        }
    });

    combo.addItemListener(new ItemListener(){

        public void itemStateChanged(ItemEvent e){
            int ind = combo.getSelectedIndex();
            System.out.println(ind); 
            fontSize = Integer.parseInt(fontSizes[ind]);
            editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
        }
    });

    clearBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            editArea.setText("");
        }
    });
}

}

Now, weird thing what happened is when I put System.out.println(ind); line just to see what index the getSelectedIndex() method returns me. Depending on which item I click, it returns me this:

1
1
0
0
2
2
3
3

Why is this happening? Shouldn't return me just 1 0 2 3? Thanks in advance.

Answer

Dan D. picture Dan D. · Aug 22, 2012

Whenever you change the selection in a JComboBox, the itemStateChanged event is triggered twice, once for DESELECT of the old selected item and once for SELECT for the new selected item.

If you only want your code to be executed once, just do:

if (e.getStateChange() == ItemEvent.SELECTED) {
...
}