0

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 :)

Community
  • 1
  • 1
DJack
  • 631
  • 1
  • 8
  • 34
  • for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable with hardcoded value for JComboBox and Document (for JTextPane/EditorPane) – mKorbel Aug 05 '13 at 12:19
  • I think this is already a SSCCE of my problem. – DJack Aug 05 '13 at 19:05
  • can [be based on](http://stackoverflow.com/q/9958004/714968), or why bothering, isn't it, [Visual Font Designer by Darryl Burke](http://tips4java.wordpress.com/2008/11/30/visual-font-designer/) – mKorbel Aug 05 '13 at 20:17
  • Hm I took a look at both of yours links, I also tried Visual Font Designer. But as I tested it I can said that this is not what I am searching.. In both of the examples it changes the font (for example) for the whole text area..if you try my example you will see that when you change font (for example), the font will be different for the new letters-> it will not change the font formatting of the text before.. – DJack Aug 05 '13 at 21:04
  • I would expect from your question at least two things more - hash code that you see, and code of FontFamilyAction – Piro Aug 06 '13 at 10:35

1 Answers1

1

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.

Piro
  • 1,367
  • 2
  • 19
  • 40
  • Thank you very much, at the end I decided to create a menubar and to put everything in the menubar. I fond it here: http://stackoverflow.com/questions/766396/how-do-i-easily-edit-the-style-of-the-selected-text-in-a-jtextpane – DJack Aug 06 '13 at 15:22