Results 1 to 17 of 17

Thread: Event source

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

    Default Event source

    The source of an action event can be
    1) JCheckBoxMenuItem
    2) JButton

    And I am finding that out like this:

    Java Code:
    public void actionPerformed(ActionEvent evt) {
                if (evt.getSource().getClass().getName().equals("javax.swing.JCheckBoxMenuItem"))  {
                    JCheckBoxMenuItem menusource = (JCheckBoxMenuItem) evt.getSource();
                    if (menusource.isSelected()) {
                        flag = true;
                    } else flag = false;
                } else {
                    JButton buttonsource = (JButton) evt.getSource();
                    if (buttonsource.isSelected()) {
                        flag = true;
                    } else flag = false;
                }
    Maybe there is an easier way to ask?

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Event source

    Why do you need to know the class of the source? Especially in this case, since you do exactly the same thing no matter what the class is.
    Get in the habit of using standard Java naming conventions!

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

    Default Re: Event source

    In order to make the if's work....
    What other way you suggest?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Event source

    Look at what class the isSelected() method is defined in. Then look at the classes that extend that class:
    JButton and JCheckBoxMenuItem

    There is no need to have separate variables.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Event source

    Norm, the source can be various, actually two things: combo box or radio button menu item.
    -I can not say: evt.getSource().getName(); it will give an error: can not find symbol!
    -If I asume the source is the combo: JComboBox source = (JComboBox) evt.getSource();
    will work
    -But if it is a radio button I'll get again an error: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JRadioButtonMenuItem cannot be cast to javax.swing.JComboBox

    So I should check first what source I am dealing with
    (maybe I am not getting what both of you are saying.....)

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

    Default Re: Event source

    Sorry this comment is about an other spot of my application , but the same thing happening here:

    Java Code:
    class MenuActionListener implements ActionListener{
            public void actionPerformed(ActionEvent evt) {
             
                // here setup the font short list which appears on the menu and
                // and the second tool bar
                 
                if (rbSetup.isSelected()){          
                    makeShortFontlist(evt);
                } else { // here: readout the font family selection
                    if (evt.getSource().getClass().getName().equals(
                                            "javax.swing.JComboBox"))  {
                        JComboBox source = (JComboBox) evt.getSource();
                        if (source.getName() != null)
                        currentFont = source.getName();
                    } else {
                        currentFont = evt.getActionCommand();
                    }
                    changeSupport2.firePropertyChange("FONTCHANGE",false,true);
                        System.out.println("current font FAMILY: "
                                                + currentFont);
                } 
            }
         }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Event source

    What class the isSelected() method is defined in? Cast to that class to call the method.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Event source

    It is indeed not working Norm! I can get the font from the radio button (1) by asking for the action, which
    works ok: evt.getActionCommand();
    But how to get the combo selection (see the final part of string (2))
    my code does not work correctly:

    String ac = evt.getActionCommand();
    Object sc = evt.getSource();

    Java Code:
    if (sc.getClass().getName().equals(
                                            "javax.swing.JComboBox"))  {
                        JComboBox source = (JComboBox) sc;
                        if (source.getName() != null) {
                            currentFont = source.getName();
                        }
                    } else {
                        currentFont = ac;
                    }

    event string 1:
    javax.swing.JRadioButtonMenuItem[,0,25,197x20,invalid,alignmentX=0.0,alignmentY=0.0 ,border=com.apple.laf.AquaMenuBorder@749abce9,flag s=264,maximumSize=,minimumSize=,preferredSize=,def aultIcon=,disabledIcon=,disabledSelectedIcon=,marg in=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=false,pressedIcon=,ro lloverEnabled=false,rolloverIcon=,rolloverSelected Icon=,selectedIcon=,text=Adobe Arabic]


    event string 2:
    javax.swing.JComboBox[,62,5,204x27,layout=com.apple.laf.AquaComboBoxUI$A quaComboBoxLayoutManager,alignmentX=0.0,alignmentY =0.0,border=,flags=16777536,maximumSize=,minimumSi ze=,preferredSize=,isEditable=false,lightWeightPop upEnabled=true,maximumRowCount=8,selectedItemRemin der=Adobe Caslon Pro]

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

    Default Re: Event source

    OK, resolved I should not ask for source.getName(); but for source.getSelectedItem();
    In case the combo box.

    Java Code:
     class MenuActionListener implements ActionListener{
            public void actionPerformed(ActionEvent evt) {
                String ac = evt.getActionCommand();
                Object sc = evt.getSource();
                
                // here setup the font short list which appears on the menu and
                // and the second tool bar
                 
                if (rbSetup.isSelected()){          
                    makeShortFontlist(evt);
                } else { // here: readout the font family selection
                    if (sc.getClass().getName().equals(
                                            "javax.swing.JComboBox"))  {
                        JComboBox source = (JComboBox) sc;
                        currentFont = (String) source.getSelectedItem();
                    } else {
                        currentFont = ac;
                    }
                    changeFontSupport.firePropertyChange("FONTCHANGE",false,true);
                        System.out.println("current font FAMILY: "
                                                + currentFont);
                } 
            }
         }

  10. #10
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Event source

    I gather that English is not your first language, so I am patient... but it's like you're not even reading the comments you receive.

    Do you not understand Norm's answers in comments #4 and #7? If you don't understand his question, or don't know how to answer it, then say so. Don't just keep charging ahead without understanding what you're doing wrong.

    And it doesn't help that you've changed the subject mid-thread. We were talking about code that deals with JButton and JCheckBoxMenuItem, but now you're talking about JComboBox. Make up your mind. Then maybe we can help you.
    Get in the habit of using standard Java naming conventions!

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

    Default Re: Event source

    At first language is not the problem.
    I made a mistake in copying the wrong method, but I explained that the problem is similar:
    it is buttons (check or radio) versus combo box.
    -The buttons (both) have their action command set, so I use evt.getActionCommand() which works fine!
    -That wont work with combo box, so I used first getText(), which also does not work!
    -The isSelected() is not relevant (it is radio button menu item for setting the font short list)
    -The source needs to be casted (that is why I am asking for combo or button) in order to get
    the method getSelectedItem(); which finally works.
    -When I do the wrong cast I get an error eg asking for getSelectedItem(); for a button!

    Asking questions like Norm helps me to get on track.....
    And yes English is not my native language (I struggle with five languages).

    This thread is resolved and preparing a new one about inner classes....
    My brain want stop, kjkrum!

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

    Default Re: Event source

    Finally to answer the question of Norm concerning the radio button,
    I had to struggle a little with the basic design of the GUI.
    Now I can articulate its working:


    Java Code:
    //radio button one: All Fonts:
    	public void actionPerformed(ActionEvent ae) {
                    fontMenu.remove(4);
                    fontMenu.insert(allFontMenu, 4);
                }
    
    //radio button two: "Short Fontlist"
    	public void actionPerformed(ActionEvent ae) {
                    fontMenu.remove(4);
                    // build the menu content first see MenuActionListener
                    fontMenu.insert(shortFontMenu, 4);
                    // next fires to construct the short list for the combo
                    // at the tool bar
                    changeFontSupport.firePropertyChange("SHORTLIST",false,true);
                }
    
    //radio button three: "Setup Shortlist"
    	public void actionPerformed(ActionEvent ae) {
                    fontMenu.remove(4);
                    fontMenu.insert(selectFontMenu, 4);
                }

    Java Code:
    //MenuActionListener's (attached to button situation one and two) , MenuActionListener:
    	public void actionPerformed(ActionEvent evt) {
                String ac = evt.getActionCommand();
                Object sc = evt.getSource();
    
                // here I do the font short list which appears at the menu
                // and the second tool bar's first combo box
                if (rbSetup.isSelected()){ // this is the third radio button
                    makeShortMenu(evt);
                    // short font list when empty
                    if (shortFontListMap.size() > 0) {
                        rbShortList.setEnabled(true);
                    } else {
                        rbShortList.setEnabled(false);
                        // from here on the font combo box will be created
                        // The short font list has at least one item from here
                        changeFontSupport.firePropertyChange("SHORTLIST",false,true);
                    }
                
    
                } else {    // here, I readout the font family selection, button one
                            // Both as a All font list or Short font list
                        fontMenu2.getCurrentFontInfo().setFontFamily(ac);
                }
             }
    Last edited by willemjav; 02-09-2014 at 11:16 AM.

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

    Default Re: Event source

    There are two more relevant methods:
    One is at the MenuFontMenu class as the above sample code and is called: makeShortMenu()
    and the other is at the class that makes the tool bar (WriterToolBar), called: instalShortFontListatCombo()
    If you wish I could print them as well?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Event source

    Well, I'm jumping in here late. But why do you check on the source class name and not the action command? That is what it is there for. And if your action listener is devoted to a single event source, then why check anything since only that source could cause the event to be generated. The other situation is when you do the same thing regardless of the event source. No need to check the source there either. If you have a shared action listener then you can switch on the action command or use if/else clauses.

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

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

    Default Re: Event source

    You're right, but I did not do that anymore from the more recent code examples (12#), right?
    Futher you'll find the important method that makes the short list of fonts for the menu,
    I think now the syntax is correct, right?
    All three menus, which you'll find under the above menu radio buttons, are registered on the class MenuActionListener
    (object menulistener)!
    I will use the shortFontListMap to store the selected font name (or the absolute index number of the all-font-list),
    under KEY, and the ref to the radio menu button under VALUE, so I can "remember" the selection (kind of
    of complicated because there is 1) a menu-all-font, 2) a menu-short-font (selected by the user) and 3) a combo box
    on the toolbar which copies the short-font-menu.
    The current font variables (family, style, size and color) are all stored (as well as the absolute index of the all-font-list) at
    a central object (kjkrum's mvc idea).


    Java Code:
     public void makeShortMenu(ActionEvent evt)  { // User builds the short list
    
             String cmd = evt.getActionCommand();
             JRadioButtonMenuItem rb = null;
             Font ft = fontMenu.getFont();
             
             //scanes through all the font names, getActionCommand is the font name
             for (int i = 0; i < allFontNameList.length; i++) {
                final String fontname = checkboxShort[i].getActionCommand();
                // when a font is selected the index is the position at the all font
                // list
                if  (cmd.equals(fontname)) {
                //if the radio button is selected create a new button to add to the
                // list
                    if (checkboxShort[i].isSelected()) {
                        Font font = new Font(fontname, Font.PLAIN, 16);
                        rb = new JRadioButtonMenuItem(fontname);
                        rb.addActionListener(menulistener);
                        rb.setFont(font);
                        group2.add(rb);
                        shortFontMenu.add(rb);
                        shortFontListMap.put(fontname, rb);
                     // when the selection of the radio button is chanceld
                     // removing from the list
                    } else {
                        Set entries = shortFontListMap.entrySet();
                        Iterator iter = entries.iterator();
                        while (iter.hasNext()) {
                            Map.Entry entry = (Map.Entry)iter.next();
                            String name = (String) entry.getKey();
                            if (fontname.equals(name)) {
                                rb = (JRadioButtonMenuItem) entry.getValue();
                                shortFontMenu.remove(rb);
                                group2.remove(rb);
                                shortFontListMap.remove(fontname);
                                break;
                            }
                        }
    
                    }
                }
            }
         }

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

    Default Re: Event source

    The final making the combo method (for the short font list which
    is the same as the short-font-list-menu)

    Java Code:
     // adding the short font list as a combo box
            public void instalShortFontListatCombo()  {
                // the call is from
                fontMenu.addPropertyChangeListener("SHORTLIST",
                                                new PropertyChangeListener() {
    
                    public void propertyChange(PropertyChangeEvent event) {
                        combo1.removeAllItems();
                        shortFontListMap = fontMenu.getShortFontNameList();
                         if (shortFontListMap.size() == 0) {
                             combo1.removeAllItems();
                             combo1.addItem("Setup Short Fontlist");
                             combo1.setEnabled(false);
                         }
                         if (shortFontListMap.size() > 0) {
                             combo1.setEnabled(true);
                           comboNames =
                                     new String[shortFontListMap.size()];
                            Set entries = shortFontListMap.entrySet();
                            Iterator iter = entries.iterator();
                            JRadioButtonMenuItem rb;
                            int count=0;
    
                            while (iter.hasNext()) {
                                Map.Entry entry = (Map.Entry)iter.next();
                                String name = (String) entry.getKey();
                                comboNames[count] = name;
                                count++;
                            }
    
                            for (int i =0; i <  comboNames.length; i++) {
                                combo1.addItem(comboNames[i]);
                            }
    
                            Font f = combo1.getFont();
                            combo1.setRenderer(new ComboRenderer1());
                            combo1.setFont(f);
                        }
                   }
    
                });
            }

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

    Default Re: Event source

    This one (I believe) is a good one:
    here I set and reset the selections at:
    1) all font menu list
    2) short font menu
    3) short font combo box

    All runs through a central object that monitors
    what font is selected: great!

    Java Code:
    // This method remebers the selections of the all_font and short-font
         // lists and the first combo box
         public void setSelectedStateFontFamily(int index, String fontname)  {
               System.out.println(" combo " + combo);
                radiobuttonAll[index].setSelected(true);
                Set entries = shortFontListMap.entrySet();
                Iterator iter = entries.iterator();
                int count = 0;
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry)iter.next();
                    String name = (String) entry.getKey();
                    if (fontname.equals(name)) {
                        JRadioButtonMenuItem rb =
                                      (JRadioButtonMenuItem) entry.getValue();
                        rb.setSelected(true);
                        combo.setSelectedIndex(count);
                        break;
                    }
                    count++;
                } 
                // In case the All-file-menu selection is not present at the
                // the short list: clear all selections of the short list.
                int mapsize = shortFontListMap.size();
                if (mapsize != 0 & count >= mapsize) {
                    group2.clearSelection();
                    combo.setSelectedIndex(0);
                }
         }

Similar Threads

  1. Web : frame source, not page source
    By FunkyProg in forum New To Java
    Replies: 0
    Last Post: 03-30-2011, 12:49 AM
  2. python source to java source converter?
    By conor147 in forum New To Java
    Replies: 0
    Last Post: 01-15-2010, 05:14 AM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. Replies: 0
    Last Post: 01-29-2009, 10:43 AM
  5. Replies: 1
    Last Post: 11-28-2008, 06:27 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
  •