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

    Default JTextPane and actions

    So I turned to the JTextPane (instead of JTextArea) as the bases of my text writer application.
    I first created the text component and its Default Model like this:

    Java Code:
    StyledDocument doc = new DefaultStyledDocument();
    JTextComponent textpane = new JTextPane(doc);
    I am not doing anything with doc for the moment.

    I noticed that I need to change several things at the GUI part of the application
    which I programmed previously (I was indeed inventing the wheel again!).

    Now, I planned to build a actionmap like this:
    ActionMap actionmap = textpane.getActionMap();
    But noticed, when looking into it (getActionMap().allKeys();) that there is
    already a lot of stuff present (not very surprising of course).
    When you want a BOLD letter you'll find the key: "font-bold" and I was
    able to add icons and change names like this:

    Java Code:
    Action a = actionmap.get("font-bold");
            a.putValue(Action.SMALL_ICON, new ImageIcon("icons/bold.gif"));
            a.putValue(Action.NAME, "BOLD");
            fontMenu.add(a);
    My first question is: what would you do when you want the Icon NOT
    to appear on the menu, but you DO want it present at the ToolBar.
    (When the action is added to the tool bar the button automatically
    holds the Icon and not the text: great!).

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

    Default Re: JTextPane and actions

    When you create a JMenuItem using an Action, it just copies the text/icon from the Action. So after you create the menu item you can just set the icon to null.

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

    Default Re: JTextPane and actions

    camick, what I understand of it is, that there is only one swing item at the time,
    so when you set the Icon to null it will be null also for the one that you created before?
    That is what is happening, tell me if I got it wrong?
    Last edited by willemjav; 02-22-2014 at 07:04 PM.

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

    Default Re: JTextPane and actions

    Might this be a good idea?

    Java Code:
    ActionMap actionmap = textpane.getActionMap();
    
            GraphicsEnvironment e =
                                GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fontNameList = e.getAvailableFontFamilyNames();
            Action a;
            for (int i = 0; i < fontNameList.length; i++) {
                actionmap.put(fontNameList[i], new
                 StyledEditorKit.FontFamilyAction(fontNameList[i], fontNameList[i]));
                a = actionmap.get(fontNameList[i]);
                a.putValue(Action.NAME, fontNameList[i]);
                fontMenu.add(a);
            }
    or maybe not?

    It works! but I get the actionmap cluthered with tons of fonts
    Last edited by willemjav; 02-22-2014 at 07:07 PM.

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

    Default Re: JTextPane and actions

    I'm not sure what you are asking.

    Post your Short, Self Contained, Correct Example that demonstrates the problem when you attempted to implement my suggestion.

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

    Default Re: JTextPane and actions

    I mean to say, that there are about a 200 hundred fonts on my comp
    and I add them all to the Actionmap, is that a good idea to do?
    Or any other suggestions?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,405
    Rep Power
    20

    Default Re: JTextPane and actions

    Here's an example of code that loads the installed fonts and allows a selection (and setting other Font attributes):
    Visual Font Designer « Java Tips Weblog

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JTextPane and actions

    Thanks db, I'l certainly look into that one!

    But first I have this other problem!

    I am getting this null pointer which, I believe, has to do with the current thread the application is running in.
    There are two combo boxes, one is initiated right from the start of the application.
    But the second one gets installed during the time the application runs and that one is giving me a hard time.
    Here is what happens:

    After that combo is built it (automatically) fires an action-performed of the first comb item of the list, right?

    I have an action listener registered with both combos:

    Java Code:
    if(sc instanceof JComboBox)  { // for both combos
                              JComboBox combo = (JComboBox) sc;
                              String name = "";
                              if (combo.getName().equals("fontfamily")) { // the second problematic combo
    
                                  action = (Action) fontcombo.getSelectedItem();
                                  System.out.println("action: " + action); // <-here the action is a null pointer*
                                  action.actionPerformed( null ); // generates the action
    .........
    * the code of the first combo box is very similar to the above one,
    but does not have a null pointer because it is created when the application starts
    (at the event-dispatch-thread right?).

    Here is the first combo that gets installed right away which works perfect:

    Java Code:
    } else {
    
                                  int i = sizecombo.getSelectedIndex();
                                  action = sizeButtonRef[i].getAction();
    
                                  action.actionPerformed( null ); // generates the action
    
                                  name = (String) combo.getSelectedItem();
                                  System.out.println("fontsize combo " + name);
                                  updateFontSelect("fontsize", name);
                              }
    Last edited by willemjav; 02-28-2014 at 06:17 PM.

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

    Default Re: JTextPane and actions

    So the action at line 6 prints null and the application crashes at line 7.
    When I take line 7 out and run it again, line 6 prints null but after
    I select an item at the combo it prints: action: javax.swing.text.StyledEditorKit$FontFamilyAction@ 10d4f27
    Now is not null any more (but, of course I can not get my font action because the line
    that should generate it is out).

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

    Default Re: JTextPane and actions

    Still don't see a SSCCE anywhere.

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

    Default Re: JTextPane and actions

    Wow, you got me on this one!
    You are right I should write more small program models
    to see if thinks work!

    Anyway in order to write it I should first understand what I did, right?

    Yet another turn of the screw: I put most actions together in one action performed method,
    because I got lost over the internal logics of the application. After revising it all ones again,
    I have it finally working and accomplished phase two of my project sucsesfully:
    I installed a JTextPane and have all the font stuff working!
    Now I will do a page presentation of the text and try to put all text through a drawn rectangled box.

    Here is the code of the listener class that handles most of the actions (the good thing is that I have
    all the lines together here:

    Java Code:
     class FontActionListener implements ActionListener{
            public void actionPerformed(ActionEvent evt) {
                String ac = evt.getActionCommand();
                Object sc = evt.getSource();
                JRadioButtonMenuItem rb;
                JComboBox combo;
                Action action;
                String name = "";
                Set entries;
                Iterator iter;
                int count = 0;
                int index = 0;
                // here I do the font short list which appears at the menu
                // and the second tool bar's combo box
                if (rbSetup.isSelected()){ // the short font setup radio button
                    makeShortMenu(ac);
    
                    
                    if (shortFontListMap.size() > 0) {
                         fontcombo.setEnabled(true);
                         rbShortList.setEnabled(true);
                    } else { // if no font was installed at the short list
                         fontcombo.setEnabled(false);
                         rbShortList.setEnabled(false);
                         //here put the empty combo dummy
                         changeFontSupport.firePropertyChange("SHORTLIST",false,true);
                    }
    
                } else {   // from here, I readout all the font selection, 
                    if(sc instanceof JRadioButtonMenuItem)  {
                        rb = (JRadioButtonMenuItem) sc;
                        if (rb.getName().equals("fontfamily")) {
                            updateFontSelect("fontfamily", ac);
                            System.out.println(ac);
                        }
                        if (rb.getName().equals("fontfamilyshort")) {
                            updateFontSelect("fontfamilyshort", ac);
                            System.out.println(ac);
                        }
                        if (rb.getName().equals("fontcolor")) {
                            updateFontSelect("fontcolor", ac);
                            System.out.println(ac);
                        }
                        if (rb.getName().equals("fontsize")) {
                            updateFontSelect("fontsize", ac);
                            System.out.println(ac);
                        }
                    }
    
                    if (sc instanceof JToggleButton)  {
                        JToggleButton toggle = (JToggleButton) sc;
                        if (toggle.getName().equals("fontstyle")) {
                            setStyleBoolean(ac);
                            updateFontSelect("fontstyle", "");
                            System.out.println(ac);
                        }
                        if (toggle.getName().equals("fontcolor")) {
                           updateFontSelect("fontcolor", ac);
                           System.out.println(ac);
                        }
                    }
    
                    if(sc instanceof JCheckBoxMenuItem)  { // style buttons
                        setStyleBoolean(ac);
                        updateFontSelect("fontstyle", "");
                        System.out.println(ac);     
                    }
    
                    if (sc instanceof JComboBox)  {
                        combo = (JComboBox) sc;
                        if (combo.getName().equals("fontfamily")) {
                            index = combo.getSelectedIndex();
    
                            // get the font name of the selected index
                            // font name is saved in the action
                            index = fontcombo.getSelectedIndex();
                            entries = shortFontListMap.entrySet();
                            iter = entries.iterator();
                            count = 0;
                            while (iter.hasNext()) {
                                Map.Entry entry = (Map.Entry)iter.next();
                                name = (String) entry.getKey();
                                rb = (JRadioButtonMenuItem) entry.getValue(); 
                                rb.getAction().actionPerformed( null ); // generates the action
                                if (index == count)
                                break;
                                count++;
                            }
                            updateFontSelect("fontfamily", name);
                            System.out.println(name);
                        }
                        if (combo.getName().equals("fontsize")) {
                            int i = sizecombo.getSelectedIndex();
                            action = sizeButtonRef[i].getAction();
    
                            action.actionPerformed( null ); // generates the action
                            name = (String) combo.getSelectedItem();
                            updateFontSelect("fontsize", name);
                            System.out.println(name);
                        }
                    }
                }
            }
         }
    
         public void setStyleBoolean(String style) { // remembers the style select
             // style: "font-bold", "font-italic","font-underline"
             for (int i = 0; i < fontStyleList.length; i++) {
                 if (fontStyleList[i].getFontStyleName().equals(style)) {
                     if (fontStylevalue[i] == false) {
                         fontStylevalue[i] = true;
                     } else {
                         fontStylevalue[i] = false;
                     }
                 }
             }
         }
    
         public void updateFontSelect(String type, String name) {
             int index = -1;
             String key;
             Set entries;
             Iterator iter;
             Map.Entry entry;
             JRadioButtonMenuItem rb;
             int count = 0;
    
             // the font family menu and combo for all and short font list
             if (type.equals("fontfamily") || type.equals("fontfamilyshort")) {
                for (int i = 0; i < allFontNameList.length; i++)  { // all font list
                    if (allFontNameList[i].equals(name)) {
                        entries = allFontListMap.entrySet(); 
                        iter = entries.iterator();
                        count = 0;
                        while (iter.hasNext()) {
                            entry = (Map.Entry)iter.next();
                            if (i == count) {
                                rb = (JRadioButtonMenuItem) entry.getValue();
                                if (!rb.isSelected())
                                rb.setSelected(true);
                                index = count;
                                break;
                            }
                            count++;
                        }
                        break;
                    }
                }
                
                entries = shortFontListMap.entrySet(); // the short font list
                iter = entries.iterator();
                
                count = 0;
                while (iter.hasNext()) {
                    entry = (Map.Entry)iter.next();
                    key = (String) entry.getKey();
                    rb = (JRadioButtonMenuItem) entry.getValue();
                    if (key.equals(name)) {
                        index = count;
                        if (!rb.isSelected())
                        rb.setSelected(true);
                        
                        if (fontcombo.getSelectedIndex() != index &
                                fontcombo.getItemCount() > 0)
                        fontcombo.setSelectedIndex(index);
                        break;
                    }
                    count++;
                }
             } 
    
             if (type.equals("fontsize")) {
                for (int i = 0; i < fontSizeList.length; i++)  {
                    if (fontSizeList[i].equals(name)) {
                        index = i;
                        sizeButtonRef[index].setSelected(true);
                        if (sizecombo.getSelectedIndex() != index)
                        sizecombo.setSelectedIndex(index);
                        break;
                    }
                }
             }
    
             if (type.equals("fontstyle")) { // sets all styles at menu and buttons
                for (int i = 0; i < fontStyleList.length; i++)  {      
                    styleButtonRef[i].setSelected(fontStylevalue[i]);
                    styleToggleButtonRef[i].setSelected(fontStylevalue[i]);
                }
             }
    
             if (type.equals("fontcolor")) {
                entries = colorMap.entrySet(); 
                iter = entries.iterator();
                
                while (iter.hasNext()) {
                    entry = (Map.Entry)iter.next();
                    key = (String) entry.getKey();
                
                    if (key.equals(name)) {
                        index = count;
                        colorindex = index; // the variable for the color index*
                        if (!colorButtonRef[index].isSelected())
                        colorButtonRef[index].setSelected(true);
                        // *the color dialog window is created each time when selected
                        break;
                    }
                    count++;
                }
             }
             System.out.println("index is " + index); 
         }
    Last edited by willemjav; 03-01-2014 at 09:33 PM.

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

    Default Re: JTextPane and actions

    Still not a SSCCE. We can't execute that code. There is no frame, not main() method. Plus it is still way too much code to look at. A SCCE should only contain code that demonstrate the problems you are having. Part of problem solving is learning to simplify the problem and isolate the code causing the problem. That is your job, not ours.

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

    Default Re: JTextPane and actions

    I did not post really any question concerning the last code, because it is working correctly. I sometimes post code to receive any critical remark that might help me on this difficult Java path.
    The point is that I am a 100% autodidact and have never taken any classes of programming. What I know I know from reading books and manuals and studying code examples (very important). I understand the importance of small example programs and I will, for next thread, try to write one (I'll tell you soon). But in the previous code case there are more than 20 classes involved that work together…….
    So you can see it working yourself when running the main of the package willemwriter which you can get right here:

    http://willemii.orgfree.com/downloads/willemwriter.zip

    Thanks for helping me again

    ps the link works but the site is sometimes down
    Last edited by willemjav; 03-03-2014 at 06:52 PM.

Similar Threads

  1. Why actions in url?
    By SomeoneHere in forum Struts
    Replies: 5
    Last Post: 01-10-2012, 05:07 AM
  2. Actions
    By What2do in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 04:45 PM
  3. actions for buttons
    By steel in forum AWT / Swing
    Replies: 1
    Last Post: 12-02-2010, 05:42 AM
  4. How to Use Actions
    By AJArmstron@aol.com in forum New To Java
    Replies: 4
    Last Post: 04-18-2010, 09:03 PM
  5. Actions
    By alley in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 02:52 PM

Posting Permissions

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