Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 47
Like Tree1Likes

Thread: need help: writing an interface

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

    Default Re: need help: writing an interface

    lines crossed I mend to say his mail and my mail crossed???

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

    Default Re: need help: writing an interface

    I have a real weird error which I can not explain!
    Look please at line 52 124
    and find the variable: private PropertyChangeSupport changeSupport2;
    why is the one at line 178: changeSupport.firePropertyChange("SHORTLIST",false ,true);
    not marked as an error!
    It took me two hours to find it!
    When I change that changeSupport into changeSupport2
    the program works!




    Java Code:
    package willemwriter;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeSupport;
    import java.util.ArrayList;
    import javax.swing.event.MenuListener;
    import javax.swing.*;
    
    import willemwriter.MenuFontMenu2.ColorAction;
    import willemwriter.MenuFontMenu2.SizeAction;
    import willemwriter.MenuFontMenu2.StyleAction;
    
    
    /**
     *
     * @author willem
     */
     class MenuFontMenu  {
    
         private MenuFontMenu2 fontMenu2;
         private Container textcanvas;
         private Font font;
         private IconImport iconImport;
         
         private JRadioButtonMenuItem rbShortList, rbSetup, rbAllfonts;
         private JRadioButtonMenuItem[] radiobAll, radiobShortlist;
         private JCheckBoxMenuItem[] checkbMenuList;
         private ButtonGroup shortlistgroup;
    
         private String currentFont;
         private int currentStyle, currentSize;
         private Color currentColor;
    
         private JMenu fontMenu, allFontMenu, selectFontMenu, shortFontMenu;
         private JMenuItem dummyItem;
    
     //  private FontStyleKeyValue[] fontStyleList;
         private String[] allFontNameList; //fontSizeList;
         private ArrayList shortFontNameList;
    
         private StyleAction[] styleActions;
         private SizeAction[] sizeActions;
         private ColorAction[] colorActions;
    
         private MenuActionListener menulistener;
         private MenuListener fontlistener;
         private AbstractAction shortListFontsAction;
         private PropertyChangeSupport changeSupport2;
    
    
    
         public MenuFontMenu(Container textcanvas) {
    
             // the text component reference
             this.textcanvas = textcanvas;
    
             changeSupport2 = new PropertyChangeSupport(this);
    
             // Icon iamge import
             iconImport = new IconImport("http://www.java-forums.org/images/");
    
             // Get the system fonts
             Fontclass fontclass = new Fontclass();
             allFontNameList = fontclass.getAllFontNames();
    
             shortlistgroup = new ButtonGroup();
    
             menulistener = new MenuActionListener();
             shortFontNameList = new ArrayList();
        
             //font menu extention for setting style, size and color
             fontMenu2 = new MenuFontMenu2(textcanvas);
    
             
         }
    
         public JMenu getMenu()    {
             return fontMenu;
         }
         
         //the relevant getters and setters
    
         public String getCurrentFont()    {
             return currentFont;
         }
         public int getCurrentSize()    {
             return fontMenu2.getCurrentSize();
         }
    
         public int getCurrentStyle()    {
             return fontMenu2.getCurrentStyle();
         }
    
         public Color getCurrentColor()    {
             return fontMenu2.getCurrentColor();
         }
    
    
         //relevant setters getters
         public ColorAction[] getColorAction()  { 
             return fontMenu2.getColorAction();
         }
    
          public StyleAction[] getStyleAction() { 
             return fontMenu2.getStyleAction();
         }
    
         public SizeAction[] getSizeAction() {
             return fontMenu2.getSizeAction();
         }
    
         public String[] getSizeList() {
             return fontMenu2.getSizeList();
         }
    
         public ArrayList getShortFontNameList()  {
             return shortFontNameList;
         }
         public PropertyChangeSupport getchangeSupport()  {
             return changeSupport2;
         }
    
    
    
         /**
         * Create the Font menu to support family, style, size and color
         * The family, style size and color elements are declared in
         * the Fontclass.
         * The buildFontMenu returns a populated menu: fontMenu
         * The variable currentFont contains the selected font family
         * The single font menu list gets removed and installed constantly
         * 1) for selection radio buttons 2) for setting up the short list
         * check boxes. The font family list is at the 4th position
         * of the font menu.
         * The second class MenuFontMenu2 adds the style size and color to
         * the menu (down part from postion 4)
         */
    
         public JMenu buildFontMenu() {
            // The actions of the tree radio buttons: all/short list/make selection
            // Action for Font menu items / the all font list
            AbstractAction allFontsAction = new AbstractAction() {
                {	// "Constructor"
                    putValue(NAME, "All Fonts");
                    putValue(SMALL_ICON, iconImport.getImageIcon("whatever"));
                    putValue(MNEMONIC_KEY, new Integer('N'));
                    putValue(SHORT_DESCRIPTION, "Creates a whatever");
                    putValue(LONG_DESCRIPTION, "Creating new whatever...");
                }
                public void actionPerformed(ActionEvent evt) {
                    fontMenu.remove(4);
                    fontMenu.insert(allFontMenu, 4);
                    //remember the previus selection
                    for (int i= 0; i < allFontNameList.length; i++) {
                        radiobAll[i].setSelected(false); 
                        if (allFontNameList[i].equals(currentFont)) {
                            radiobAll[i].setSelected(true);
                        }
                    }
                }
            };
            // the short font list after construction (see next block)
            shortListFontsAction = new AbstractAction() {
                {
                    putValue(NAME, "Short Fontlist");
                    putValue(SMALL_ICON, iconImport.getImageIcon("whatever"));
                    putValue(MNEMONIC_KEY, new Integer('N'));
                    putValue(SHORT_DESCRIPTION, "Creates whatever");
                    putValue(LONG_DESCRIPTION, "Creating whatever...");
                }
                public void actionPerformed(ActionEvent evt) {
                    fontMenu.remove(4);
                    fontMenu.insert(shortFontMenu, 4);
                    changeSupport.firePropertyChange("SHORTLIST",false,true);
                    // removing the dummy of the
                    if (shortFontMenu.getItemCount() != 0 &
                                            !shortFontMenu.equals(dummyItem))  {
                        shortFontMenu.remove(dummyItem);
                    } else {
                        shortFontMenu.add(dummyItem);
                    }
                    
                    //remember the previous selection
                    if (shortFontMenu.getItemCount() > 0 & currentFont != null)  {
                        shortlistgroup.clearSelection();
                        
                       int i = shortFontNameList.indexOf(currentFont);
                       if (i >= 0) {
                            JRadioButtonMenuItem rb =
                              (JRadioButtonMenuItem) shortFontMenu.getItem(i);
                            if (rb != null)
                            rb.setSelected(true);
                       }
                    }
                }
            };
            // constructing the short list see also makeShortFontlist
            // and MenuActionListener. the list is stored in shortMenuMap
            // and shortFontMenu
            AbstractAction setupshortListFontsAction = new AbstractAction() {
                {
                    putValue(NAME, "Setup Shortlist");
                    putValue(SMALL_ICON, iconImport.getImageIcon("whatever"));
                    putValue(MNEMONIC_KEY, new Integer('N'));
                    putValue(SHORT_DESCRIPTION, "Creates a new whatever");
                    putValue(LONG_DESCRIPTION, "Creating new whatever...");
                }
                public void actionPerformed(ActionEvent evt) {
                    fontMenu.remove(4);
                    // the selection was constructed at MenuActionListener
                    fontMenu.insert(selectFontMenu, 4);
                
                }
            };
    
            // the several menus
            fontMenu = new JMenu("Fonts");
            ButtonGroup group = new ButtonGroup();
    
            // first the three fixed radio buttons All/ Short list/
            //selecting short list
            rbAllfonts = new JRadioButtonMenuItem(allFontsAction);
            rbAllfonts.setSelected(true);
            fontMenu.add(rbAllfonts);
            group.add(rbAllfonts);
    
            rbShortList = new JRadioButtonMenuItem(shortListFontsAction);
            rbShortList.setSelected(false);
            rbShortList.setEnabled(true);
            fontMenu.add(rbShortList);
            group.add(rbShortList);
    
            rbSetup = new JRadioButtonMenuItem(setupshortListFontsAction);
            rbSetup.setSelected(false);
            fontMenu.add(rbSetup);
            group.add(rbSetup);
            fontMenu.addSeparator();
    
            // a second menu added to show the font listings All/Short
            allFontMenu = new JMenu("Font List");
            selectFontMenu = new JMenu("Font List");
            shortFontMenu = new JMenu("Font List");
    
            buildFontMenuItems(allFontNameList);
            dummyItem = new  JMenuItem("EMPTY");
            dummyItem.setEnabled(false);
    
            // add all system fonts to the menu
            fontMenu.add(allFontMenu);
    
            MenuScroller.setScrollerFor(allFontMenu);
            MenuScroller.setScrollerFor(selectFontMenu);
    
            // the menu  extion from the class MenuFontMenu2
            fontMenu.addSeparator();// the addional font menus of style, size and
            fontMenu.add(fontMenu2.buildFontMenu2(fontMenu)); // color
    
            
    
            return fontMenu; // returns the populated menu
         }
    
        
         // create the fourth menu the all font menu or select short list menu
         // building all radio buttons and checkboxes for selection (done only once)
         public void buildFontMenuItems(String[] names)  {// font name
              radiobAll = new JRadioButtonMenuItem[names.length];
              radiobShortlist = new JRadioButtonMenuItem[names.length];
              checkbMenuList = new JCheckBoxMenuItem[names.length];
              ButtonGroup allFontgroup = new ButtonGroup();
              Font ft = fontMenu.getFont();
              //the names-array contains all font families see FontClass
              for (int i = 0; i < names.length; i++) {
                    font = new Font(names[i], Font.PLAIN, 16);
                    radiobAll[i] = new JRadioButtonMenuItem(names[i]);
                    checkbMenuList[i] = new JCheckBoxMenuItem(names[i]);
    
                    radiobAll[i].setActionCommand(names[i]);
                    checkbMenuList[i].setActionCommand(names[i]);
    
                    radiobAll[i].setFont(font);
                    checkbMenuList[i].setFont(font);
    
                    allFontgroup.add(radiobAll[i]);
    
                    radiobAll[i].addActionListener(menulistener);
                    checkbMenuList[i].addActionListener(menulistener);
    
                    selectFontMenu.add(checkbMenuList[i]);
                    allFontMenu.add(radiobAll[i]);
              }
              allFontMenu.setFont(ft);
              selectFontMenu.setFont(ft);
         }
    
         
    
         // this method gets called when selecting the font families for the
         // short font menu list stored in: shortMenuMap and shortFontMenu
         // this menu gets removed and installed at the 4th position of the font menu
         public void makeShortFontlist(ActionEvent evt)  {
            String cmd = evt.getActionCommand();
            JRadioButtonMenuItem rb;
            Font ft = fontMenu.getFont();
            for (int i = 0; i < allFontNameList.length; i++) {
                String select = checkbMenuList[i].getActionCommand();
                if  (cmd.equals(select)) {
                    if (checkbMenuList[i].isSelected()) {   // adding onto the list
                        font = new Font(select, Font.PLAIN, 16); // when selected
                        rb = new JRadioButtonMenuItem(select);
                        rb.setActionCommand(select);
                        rb.addActionListener(menulistener);
                        rb.setFont(font);
                        shortlistgroup.add(rb);
                        shortFontMenu.add(rb);
                        shortFontNameList.add(select);
    
                    } else { // removing from the list
                        int i2 = shortFontNameList.indexOf(select);
                        if (i2 >= 0) {
                            System.out.println("remove " + select);
                            rb = (JRadioButtonMenuItem) shortFontMenu.getItem(i2);
                            shortFontNameList.remove(select);
                            shortlistgroup.remove(rb);
                            shortFontMenu.remove(rb);
                        }
                    }
                }
            }
          //  shortFontMenu.setFont(ft);
         }
    
          class MenuActionListener implements ActionListener{
            public void actionPerformed(ActionEvent evt) {
                Object menusource = evt.getSource();
                String cmd = evt.getActionCommand();
    
                // here setup the font short list
                if (rbSetup.isSelected()){
                    makeShortFontlist(evt);
                } else { // here: readout the font family selection
                    currentFont = cmd;
                    System.out.println("current font: " + currentFont);
                }
    
                
            }
         }
    }
    Last edited by willemjav; 01-31-2014 at 02:04 AM.

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

    Default Re: need help: writing an interface

    here is the proof see image (look the top lien and bottom line)
    The variable is declared as: private PropertyChangeSupport changeSupport2;

    http://willemii.orgfree.com/downloads/proof1.pdf

    http://willemii.orgfree.com/downloads/proof2.pdf
    Last edited by willemjav; 01-31-2014 at 02:20 AM.

  4. #24
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: need help: writing an interface

    I'm not certain how that proves anything. Perhaps it exists somewhere else in your package hierarchy or in another class. After all, you have not shown all of your code.

    Regards,
    Jim
    Last edited by jim829; 01-31-2014 at 04:00 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: need help: writing an interface

    It is indeed used in other classes of the package, but always
    declared private!
    I agree, jim there must be a reason that this happens.
    The fact that some thing else set that variable wasted my time,
    since I could not make the property fire to work and did not
    understand the season!
    One of my (limited) debugging tricks is that I write an X or number after
    the source variable in finding out where it all appears.....
    That was the way I found out!
    You see I had some other cases as well and (almost) believe
    there is a METAPHYSICAL side of programming (hahahaha)!
    Last edited by willemjav; 01-31-2014 at 08:24 AM.

  6. #26
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: need help: writing an interface

    I would choose to keep the PropertyChangeSupport in B private and not exposed via a getter, but add a method "addPropertyChangeListener" to B. You should also make a removePropertyChangeListener so you won't get a memoryleak on objects thatn never get garbage collected.

    Java Code:
    class A {
        B b = new B();
        b.addPropertyChangeListener("Shortlist",
                                                new PropertyChangeListener() {
                    public void propertyChange(PropertyChangeEvent event) {
                        infoLabel.setText("BINGO");
                    }
            }));
    }
    
    class B {
    
       public void addPropertyChangeListener(String property, PropertyChangeListener p) {
          changeSupport.addPropertyChangeListener(property, p);
       }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: need help: writing an interface

    good, that makes since!

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

    Default Re: need help: writing an interface

    So this would be it:


    Java Code:
    package callback;
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /**
     *
     * @author willem
     */
    public final class ClassA {
         private JLabel infoLabel;
    
         ClassA() {
    
             final ClassB b = new ClassB();
             
             b.addPropertyChangeListener("SHORTLIST",
                                                new PropertyChangeListener() {
                    public void propertyChange(PropertyChangeEvent event) {
                        infoLabel.setText("BINGO");
                    }
             });
    
    
             JFrame frame = new JFrame();
            
             Container contentPane = frame.getContentPane();
             contentPane.add(b.createButton(), BorderLayout.NORTH);
             contentPane.add(createInfoPanel(), BorderLayout.SOUTH);
             frame.pack();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
         }
    
         public JPanel createInfoPanel()  {
            JPanel infoPanel = new JPanel();
            infoLabel = new JLabel("info");
            infoPanel.add(infoLabel);
    
            return infoPanel;
         }
    
         public static void main(String arg[]) {
             new ClassA();
         }
    }

    Java Code:
    package callback;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    /**
     *
     * @author willem
     */
    public class ClassB extends JPanel{
        private PropertyChangeSupport changeSupport;
    
        public ClassB() {
            changeSupport = new PropertyChangeSupport(this);
        }
    
    
        public JPanel createButton()  {
            
            JPanel panel = new JPanel();
            JButton button = new JButton("Action");
             button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    System.out.println("action pushed");
                    changeSupport.firePropertyChange("SHORTLIST",false,true);
                }
             });
             panel.add(button);
             return panel;
        }
    
        public void addPropertyChangeListener(String property, PropertyChangeListener p) {
          changeSupport.addPropertyChangeListener(property, p);
       }
    
       public void removePropertyChangeListener(String property, PropertyChangeListener p) {
            changeSupport.removePropertyChangeListener(p);
        }
    
    
    }
    Last edited by willemjav; 01-31-2014 at 11:25 AM.

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

    Default Re: need help: writing an interface

    Thanks, you guys really helped me a lot this time!
    Things like saying: "interfaces is not the way to go!"
    Is very important because one could do this stuff, using interfaces
    (It is all basically about moving around references, in "both directions").
    But by using interfaces you would kind of corrupt the idea of interfaces, right!
    So showing alternatives really helped!

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

    Default Re: need help: writing an interface

    The mystery of changeSupport and changeSupport2 stays!
    But one day I will resolve that conspiracy theory......

  11. #31
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: need help: writing an interface

    You shouldn't be using an interface because you want to use an interface. You should use one when you need one. The same goes for design patterns. Some developers want to use singletons, factories, facades, builders, adapters, et al just because they can, it makes you look 1337, or it makes them superior over their junior coworkers. Use a pattern or interface because you need to, not because it's cool.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: need help: writing an interface

    Don't worry I do things mostly because of ignorance, hahahaha!
    What is 1337 btw is it like "cross lines"?
    Last edited by willemjav; 01-31-2014 at 11:35 AM.

  13. #33
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: need help: writing an interface

    I like how you don't know what leet-speak is. 1337 = "leet", as in "elite". And I apologize that I exposed you to that "knowledge", better forget it again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: need help: writing an interface

    Only for gimbal2 (btw it is NOT written by that supposed author)
    Jordan Maxwell THE PRIESTHOOD OF THE ILLES pdf free download

  15. #35
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: need help: writing an interface

    Why do I get that present?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: need help: writing an interface

    Because I am a generous person

  17. #37
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: need help: writing an interface

    Generously confusing at least :) But thank you!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: need help: writing an interface

    Your welcome,( at least now you feel what I feel when dealing with JAVA)

  19. #39
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: need help: writing an interface

    Its Java, not JAVA. Its a name, not an acronym. Things get really difficult when you can't even properly name the beast you're trying to tame!

    And no I can't really know how you feel, I learned Java the "proper" way which was very controlled and organized.

    a) learn the basics in school (3 years)
    b) REALLY learn it on the job as a junior under the guidance of a proper mentor (2 years)
    c1) further apply knowledge as a dev in a team of developers (5 years)
    c2) professionalize while instructing juniors (same 5 years)
    d) master it as a senior developer ( <--- currently I'm here, 1.5 years and counting )

    Although I'm describing the path I took to learn the art of software engineering here, not specifically Java programming. Java is only a hammer, software engineering is the trade.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: need help: writing an interface

    SurfMan/ gimbal2
    What lit. would you recommend concerning design patterns?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 06:23 PM
  2. writing a program to interface with other programs
    By gib65 in forum Advanced Java
    Replies: 4
    Last Post: 11-04-2011, 11:09 PM
  3. Replies: 1
    Last Post: 02-27-2010, 02:29 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
  •