Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Create JRadioButtons from HashMap by loop

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Starving
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Create JRadioButtons from HashMap by loop

    I am trying to use a loop to create listeners and radio buttons from a HashMap.

    1.	package javaap;
    2.	
    3.	import java.awt.Color;
    4.	import java.awt.Component;
    5.	import java.awt.GridLayout;
    6.	import java.awt.event.ActionEvent;
    7.	import java.awt.event.ActionListener;
    8.	import java.util.HashMap;
    9.	import java.util.Iterator;
    10.	import java.util.Map;
    11.	import javax.swing.JFrame;
    12.	import javax.swing.JPanel;
    13.	import javax.swing.JRadioButton;
    14.	import javax.swing.JTextArea;
    15.	
    16.	
    17.	public class JavaAp extends JFrame {
    18.	    private static Object button;
    19.	    private static JTextArea jtamessage = new JTextArea("What color", 5, 20);
    20.	     
    21.	    
    22.	     
    23.	   public JavaAp (){  
    24.	          
    25.	   }
    26.	    public static void main(String[] args) throws IllegalArgumentException, 
    27.	            IllegalAccessException{
    28.	         
    29.	        Map<String, String> colorMap = new HashMap<>();
    30.	         
    31.	          colorMap.put ("DANGER","RED");
    32.	          colorMap.put ("CAUTION","YELLOW");
    33.	          colorMap.put ("CLEAR","GREEN");
    34.	        System.out.println (colorMap);
    35.	        final JPanel display = new JPanel(new GridLayout(8, 3));
    36.	        
    37.	            
    38.	        //Iterator it = colorMap.entrySet().iterator();
    39.	        for (Iterator<Map.Entry<String, String>> it = colorMap.entrySet().iterator(); it.hasNext();) {
    40.	            final Map.Entry mEntry = it.next();
    41.	            System.out.println(mEntry.getKey()+ ":" + mEntry.getValue());
    42.	            button = new JRadioButton(""+mEntry.getKey()+"");
    43.	            display.add((Component) button);
    44.	            
    45.	            button.addActionListener(new ActionListener(){
    46.	               
    47.	                @Override
    48.	       public void actionPerformed(ActionEvent e){
    49.	                   
    50.	             display.setBackground(Color.(mEntry.getValue()));
    51.	
    52.	       }                      
    53.	       });        }
    54.	        
    55.	    JavaAp frame = new JavaAp();
    56.	       
    57.	            frame.pack();
    58.	            frame.setTitle("Color");
    59.	            frame.setLocationRelativeTo(null); // Center the frame
    60.	            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    61.	            frame.setVisible(true);
    62.	    }}
    The code gives me these ! :
    @45 cannot find symbol
    Symbol: method add ActionListener (<anonymous ActionListener>)
    Location: variable button of Object type
    @50 <identifier> expected
    This is the output from console. No GUI, the hashMap prints before the loop and then only one element when in the loop.
    run:
    {CAUTION=YELLOW, DANGER=RED, CLEAR=GREEN}
    CAUTION:YELLOW
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: java.lang.Object.addActionListener
    	at javaap.JavaAp.main(JavaAp.java:45)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Create JRadioButtons from HashMap by loop

    (mEntry is accessed from inner class needs to be declared final).
    define mEntry as final. local variables must be saved for inner classes to use.

    error at 43 (Erroneous tree type-)
    Please post the full text of the compiler's error message. I don't understand what you have posted

  3. The Following User Says Thank You to Norm For This Useful Post:

    poundnmonitor (March 30th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Starving
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create JRadioButtons from HashMap by loop

    I made mEntry and display final; I am still getting errors. I have edited the original post.

  5. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Create JRadioButtons from HashMap by loop

    I am still getting errors
    Please copy the full text of the compiler's error messages and paste it here.
    The posted code can't be copied and compiled because of the line numbering.

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Starving
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create JRadioButtons from HashMap by loop

    Console--run:
    {CAUTION=YELLOW, DANGER=RED, CLEAR=GREEN}
    CAUTION:YELLOW
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: java.lang.Object.addActionListener
    at javaap.JavaAp.main(JavaAp.java:45)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)


    NetBeans--@45 cannot find symbol
    Symbol: method add ActionListener (<anonymous ActionListener>)
    Location: variable button of Object type
    @50 <identifier> expected
    Thanks!

  7. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    My Mood
    Starving
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Create JRadioButtons from HashMap by loop

    Netbeans gives me these ! :
    @45 cannot find symbol
    Symbol: method add ActionListener (<anonymous ActionListener>)
    Location: variable button of Object type
    @50 <identifier> expected
    This is the output from console. No GUI, the hashMap prints before the loop and then only one element when in the loop.
    run:
    {CAUTION=YELLOW, DANGER=RED, CLEAR=GREEN}
    CAUTION:YELLOW
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: java.lang.Object.addActionListener
    at javaap.JavaAp.main(JavaAp.java:45)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

Similar Threads

  1. loop help need to create the letter L AND Y
    By YG N JYP FAM4EVA in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 12:56 PM
  2. Hashmap
    By ryan12345 in forum Java Theory & Questions
    Replies: 6
    Last Post: November 2nd, 2012, 07:04 PM
  3. I need to create a certain SHAPE with FOR LOOP.
    By Rage1337 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 4th, 2012, 09:15 AM
  4. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  5. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM