How to display the name of the Action in the combo box

242 Views Asked by At

I am trying already some days to get my code working but with no success jet. I think that my code explain my problem. I don't know how to see in the combo box item for example "Serif".. I see the hash code of the action..The part of the code which is in comment are my unsuccessful attempt to make it work correctly. I think here is the solution to my code but I can't get it work: action listener in another class - java

 public class ComboBoxFontFamilyAction extends JFrame implements ItemListener {
    public ComboBoxFontFamilyAction() {
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(new StyledEditorKit.ForegroundAction("Red", Color.red));
        comboBox.addItem(new StyledEditorKit.FontFamilyAction("Serif"
                .toString(), "Serif"));

        // When I click on this item I get the error
        comboBox.addItem(new FontSetting(new StyledEditorKit.ForegroundAction(
                "Red", Color.red), "Read what you want", comboBox));

        comboBox.addItemListener(this);

        getContentPane().add(comboBox, BorderLayout.SOUTH);
        JTextPane textPane = new JTextPane();
        textPane.setText("Some text to change attributes of.");
        getContentPane().add(new JScrollPane(textPane));

        // I think that i should do something like that
        // ItemListener itemListener = new FontSetting(new
        // StyledEditorKit.ForegroundAction("Red", Color.red),
        // "Wohoo",comboBox);
        // comboBox.addItemListener(itemListener);

    }

    public void itemStateChanged(ItemEvent e) {
        Action action = (Action) e.getItem();
        action.actionPerformed(null);
    }

    public static void main(String[] args) {
        ComboBoxFontFamilyAction frame = new ComboBoxFontFamilyAction();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}


    //public class FontSetting implements ItemListener {

public class FontSetting {

    private StyledEditorKit.StyledTextAction textAction;
    private String displayValue;

    // private JComboBox comboBox1;

    public FontSetting(StyledEditorKit.StyledTextAction textAction,
            String displayValue, JComboBox comboBox1) {
        this.textAction = textAction;
        this.displayValue = displayValue;
        // this.comboBox1 = comboBox1;

    }

    // public void itemStateChanged(ItemEvent e) {
    // Action action = (Action) e.getItem();
    // action.actionPerformed(null);
    // }

    public String toString() {
        return displayValue;
    }
}

Thx in advance for any help :)

1

There are 1 best solutions below

1
On

You see hash code probably because you are not overriding toString() method of StyledEditorKit.FontFamilyAction.

You should override it to return reasonable string ("Serif"), or use JComboBox.setRenderer(ListCellRenderer) to set custom renderer that renders created StyledEditorKit.FontFamilyAction to reasonable text.