Results 1 to 14 of 14
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default getting an action by "just" clicking on a button

    The next code works but maybe there is a better way to do it.
    -sizeButtonRef holds the reference of a series of menu radio buttons
    of the font size;
    -the same list is added to a combo box on the toolbar (just a series of
    font size numbers)
    I need the action of the button in order to change the font size,
    and the actions are implemented like this:

    Java Code:
    actionmap.put("font-size-" + fsize, // the font size number as string
                        new StyledEditorKit.FontSizeAction("font-size-" + 
                                                fsize, Integer.parseInt(fsize)));
    I get this action by simply clicking on the bottom…..
    Maybe an odd thing to do? Any better idea?

    Java Code:
      String[] size = new String[sizeButtonRef.length];
                for (int i = 0; i < sizeButtonRef.length; i++) {
                    		size[i]  = sizeButtonRef[i].getText();
                 }
                final JComboBox combo2 = new JComboBox(size);
                combo2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                        		System.out.println(" size combo: " + combo2.getSelectedItem());
                        		int i = combo2.getSelectedIndex();
                        		sizeButtonRef[i].doClick();
                        }
                });

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    1) I can put an action into the ActionMap:

    Java Code:
    actionmap.put("font-size-" + fsize,
                        new StyledEditorKit.FontSizeAction("font-size-" + 
                                                fsize, Integer.parseInt(fsize)));
    2) I can than get that action like this:

    Java Code:
    Action action = actionmap.get("font-size-" + fsize);
    3) I could add the action to a button like this:

    Java Code:
    JRadioButtonMenuItem rbSize = new JRadioButtonMenuItem(action);
    Question but how do I add that action to a combo box?

    Java Code:
    fsize = fsize = fontSizeList[i]; // which holds the font size numbers

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: getting an action by "just" clicking on a button

    What exactly is it you are trying to do? If your combo box holds various fonts,
    why not just implement an ItemListener for the combo box?

    Regards,
    Jim
    Last edited by jim829; 02-24-2014 at 09:11 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    I am changing the font size of a JTextPane by using a actions added to a menu
    (actually menu radio buttons:
    Action action = actionmap.get("font-size-" + fsize);
    rbSize = new JRadioButtonMenuItem(action);
    )

    But when I try to do the same with a combo box i can't!

    -I can add the actions to the combox.
    -I can render the actions in a way that the combo box shows the
    text string of the action!
    -But when I click on the combo it does not generate the action!

    I do undertand why this happens, but what can I do to make a font size selection
    through the combo box?
    Last edited by willemjav; 02-24-2014 at 04:20 PM.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: getting an action by "just" clicking on a button

    You don't need to add an Action to the ActionMap in order to use it. The ActionMap just contains predefined Actions that you can use.

    For the radio button you can just create the Action and add it directly to the radio button. See the section from the Swing tutorial on Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for an example of how to create menu items this way.

    For the combo box you can't use Actions directly, so you just store the integer value in the combo box. Then on then you use an ItemListener or ActionListener to listen for a selection. Then you dynamically create the Action and invoke the Action directly. Something like:

    Java Code:
    int fontSize = Integer.parseInt( comboBox.getSelectedItem() );
    Action fontSizeAction = new StyledEditorKit.FontSizeAction("", fontSize);
    fontSizeAction.actionPerformed( null ); // you may need a real ActionEvent object here.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    Camickr, yes I figures out already something similar!
    Inside the action performed of the combo, I would get the
    selection index. The combo items are the same as those on the font size menu, so I can do the following:

    Java Code:
    combo2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                        int i = combo2.getSelectedIndex();
                        Action action = sizeButtonRef[i].getAction(); // the action stored in the menu button
    		    // I do not need to create an action object here it was created before
    		    // and you are right there are many pre installed actions already present!
    		    action.actionPerformed( null ); // ??? what would be the arg here
    }
    " you may need a real ActionEvent object here. " what would be that object?
    I am a little confused now (reading already many days about all these interfaces
    classes and inner classes that story texcomponent data: man that is not easy stuff for me!)

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    Camickr, I resolved it but I do not understand fully what I did...
    Could you be so kind to explain it to me?

    Java Code:
    combo2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent event) {
                        int i = combo2.getSelectedIndex();
                        Action action = sizeButtonRef[i].getAction();
                        ActionEvent ae =  new ActionEvent(textpane, ActionEvent.ACTION_PERFORMED, "whatever here goes");
                        action.actionPerformed( ae );
                    }
                });

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: getting an action by "just" clicking on a button

    Did you try just using null as the parameter for the actionPerformed() method (that was my original suggestion)? Most Action will work with null because the Action will change the property of the last focus text component.

    However, if you get a NullPointerException, then you need to create the ActionEvent and specify the component that you want the Action to work on.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: getting an action by "just" clicking on a button

    I assume you want to have a different action taken for each element in your JComboBox? Why not just create a class that implements your own interface.
    Then you can call that interface method from your actionPerformed method?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    You're right camickr,
    Action action = sizeButtonRef[i].getAction();
    action.actionPerformed( null );
    works equally well, how come?

    And Jim, once I have the index of the combo item selected,
    I'll fetch the corresponding menu button (out of the array) and get its action!
    I do not need anything else.....

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    There is though a focus problem.
    When I select a text block and use the menu to
    change the font size, it works.
    But when I do the same with the combo on the toolbar,
    it seems that the selection resolves or looses focus?

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    No there was another problem.
    I forgot that I added actions to the combo as combo items.
    So I just get the item like this:
    Action action = (Action) combo.getSelectedItem();
    action.actionPerformed( null );
    And the thing works, great!
    Thank you camickr, for your help!

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,065
    Rep Power
    8

    Default Re: getting an action by "just" clicking on a button

    Great, I am able to select single characters, words, lines or paragraphs and change
    the font type, style, size and color! It all appears correctly on screen, but
    when I save the document and open it again it appears in one letter type, size,
    color etc: so all is gone. This has to do with the MVC, right?
    I will prepare a new thread to discuss how the text data is actually saved!
    I am reading a lot about that and I should say, all I read is not quite as clear as glass!

  14. #14
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: getting an action by "just" clicking on a button

    JTextPane only stores text, not the attributes associated with the text. So you need to save the data in a different format. You can try using HTML. A JTextPane also supports HTML, although I have always found it difficult to work with, so I never use it. You can also try using Rich Text Format (RTF). Again I have never used this so you will need to search the web for information on how you might do this. I'm not sure how much support a JTextPane has for RTF.

Similar Threads

  1. Replies: 0
    Last Post: 11-21-2013, 06:13 PM
  2. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  3. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  4. Listener for action "team->commit"
    By feva in forum Eclipse
    Replies: 0
    Last Post: 07-27-2009, 07:40 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •