Results 1 to 6 of 6
  1. #1
    Adithya is offline Member
    Join Date
    Jun 2014
    Posts
    24
    Rep Power
    0

    Default function does not calls in actionListener

    in my given below code..in Action listener #Report function does not calling.
    Help me to find out pls
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.Random;
    
    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    class Quiz extends JFrame implements ActionListener{
    /**
    	 * 
    	 */
    	
    JPanel panel;
    JPanel panelresult;
    	JRadioButton choice1;
    	JRadioButton choice2;
    	JRadioButton choice3;
    	JRadioButton choice4;
    		ButtonGroup bg;
    			JLabel lblmess;
    			JButton btnext;
    			JButton mark;
    			String line;
    			int i=0;
    			String[] word;
    			String[] wor;
    			String[] mean;
    			Random rand;
    			String ques;
    			String[] names;
    			String[] name;
    			int l;
    			int count=0;
    			String selectedChoice;
    			
    
    Quiz(){
       initialize();
       
       setTitle("Quiz Program");
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setSize(430,350);
       setLocation(300,100);
       setResizable(true);
           
           Container cont=getContentPane();
           cont.setLayout(null);
           cont.setBackground(Color.GRAY);
                  bg=new ButtonGroup();
     
                  choice1=new JRadioButton(); 
                  choice2=new JRadioButton();
                  choice3=new JRadioButton();
                  choice4=new JRadioButton();
                  
                    bg.add(choice1);
                    bg.add(choice2);
                    bg.add(choice3);
                    bg.add(choice4);
                    
                      lblmess=new JLabel("Choose a correct anwswer");
                      lblmess.setForeground(Color.BLUE);
                      lblmess.setFont(new Font("Arial", Font.BOLD, 11));
                      
                         btnext = new JButton("next");
                         btnext.setForeground(Color.GREEN);
                         btnext.setBackground(Color.DARK_GRAY);
                         btnext.addActionListener(this);
                              
                          mark = new JButton("mark");
                          mark.setForeground(Color.RED);
                          mark.setBackground(Color.DARK_GRAY);
                          mark.addActionListener(this);
                              
                            panel=new JPanel();
                            panel.setBackground(Color.LIGHT_GRAY);
                            panel.setLocation(10,10);
                            panel.setSize(400,300);
                            panel.setLayout(new GridLayout(6,2));
                            panel.add(lblmess);
                            panel.add(choice1);
                            panel.add(choice2);
                            panel.add(choice3);	
                            panel.add(choice4);
                            panel.add(mark);
                            panel.add(btnext);
                            cont.add(panel);
                            	setVisible(true);
                            		askq();
    
    }
    
    public void actionPerformed(ActionEvent s) {
    	
    	
    	 
    	
    	      if(btnext.getText().equals("next")){
    	    	
    	    	      getSelection();
    	    	  
    	    		
    	    		//   System.out.println(selectedChoice);
    					if(selectedChoice == mean[l]){
    					
    	    			  calc();
    	    		      }
    	      askq();
    	      }
    	       
    	      else  
    	      
    	    	  if(mark.getText().equals("mark"))
    	    	  
    		         Report();                                          // Here am  Struggling !!!!
    	      
    	}
    
    public void initialize()
    {
      try
      {
      System.out.println("\nReading word\n");
      BufferedReader br = new BufferedReader(new FileReader("inp.txt"));
      String line;
      int i=0;
      wor = new String[9];
      mean= new String[9];
      do
      {
       line=br.readLine();
       if(line != null)
        {
    	  word = new String[3];  
          word=line.split(";");
         wor[i]=word[0];
         mean[i]=word[1];
    	//System.out.println(wor[i]);
           i++;        	//System.out.println(mean[i]);
    	
          }
     }while(line != null);
    }catch (IOException e) {
        e.printStackTrace();
    }
     }
    
    
    
    public void askq()
    
    {
       Random rand =new Random();
      ques = wor[(l=(rand.nextInt(4)))];
    
    
    String[] name = new String[4];
      for(int j=0; j<3 ;j++)
        name[j] = mean[rand.nextInt(8)];
        name[3]=mean[l];
        
        String[] names = {name[0], name[1], name[2], name[3]}; 
        Collections.shuffle( Arrays.asList(names));
        
    lblmess.setText(ques);
    choice1.setText(names[0]);
    choice2.setText(names[1]);
    choice3.setText(names[2]);
    choice4.setText(names[3]);
    
    
    
    }
    
    public void getSelection(){
    	
    	Enumeration<AbstractButton> buttons=bg.getElements();
    	while(buttons.hasMoreElements())
    	{
    		JRadioButton temp=(JRadioButton)buttons.nextElement();
    		if(temp.isSelected())
    		{
    			selectedChoice=temp.getText();
    	   
    		}
    	}
    	
    }	
    
     public int calc()
     {
        
    	 count++;
       return count;	
    	
     }
    
    public void Report()
    {
    	JOptionPane.showMessageDialog( null, "The sum is " + count, "Results", JOptionPane.PLAIN_MESSAGE );
    	setVisible(true);
    	System.exit(0);
    }
    }
    
    
    public class Quizz{
    public static void main(String args[]){
    new Quiz();
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: function does not calls in actionListener

    Your logic doesn't make sense: you have two buttons btnext and mark, labeled "next" and "mark"; you add a single ActionListener to them; in the body of that listener you test whether or not the button btnext is labeled "next"; it is; always, so the else clause is never reached.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Adithya is offline Member
    Join Date
    Jun 2014
    Posts
    24
    Rep Power
    0

    Default Re: function does not calls in actionListener

    Thank you sir..
    now i added two listener.but i can't get GUI.what's the problem is.i given the code below
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.Random;
    
    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    
    class Quiz extends JFrame {
    /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    /**
    	 * 
    	 */
    	
    JPanel panel;
    JPanel panelresult;
    	JRadioButton choice1;
    	JRadioButton choice2;
    	JRadioButton choice3;
    	JRadioButton choice4;
    		ButtonGroup bg;
    			JLabel lblmess;
    			JButton btnext;
    			JButton mark;
    			String line;
    			int i=0;
    			String[] word;
    			String[] wor;
    			String[] mean;
    			Random rand;
    			String ques;
    			String[] names;
    			String[] name;
    			int l;
    			int count=0;
    			String selectedChoice;
    			
    
    void Quiz(){
       initialize();
       
       setTitle("Quiz Program");
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setSize(430,350);
       setLocation(300,100);
       setResizable(true);
           
           Container cont=getContentPane();
           cont.setLayout(null);
           cont.setBackground(Color.GRAY);
                  bg=new ButtonGroup();
     
                  choice1=new JRadioButton(); 
                  choice2=new JRadioButton();
                  choice3=new JRadioButton();
                  choice4=new JRadioButton();
                  
                    bg.add(choice1);
                    bg.add(choice2);
                    bg.add(choice3);
                    bg.add(choice4);
                    
                      lblmess=new JLabel("Choose a correct anwswer");
                      lblmess.setForeground(Color.BLUE);
                      lblmess.setFont(new Font("Arial", Font.BOLD, 11));
                      
                         btnext = new JButton("next");
                         btnext.setForeground(Color.GREEN);
                         btnext.setBackground(Color.DARK_GRAY);
                         btnext.addActionListener(new next());
                              
                          mark = new JButton("mark");
                          mark.setForeground(Color.RED);
                          mark.setBackground(Color.DARK_GRAY);
                          mark.addActionListener(new mark());
                              
                            panel=new JPanel();
                            panel.setBackground(Color.LIGHT_GRAY);
                            panel.setLocation(10,10);
                            panel.setSize(400,300);
                            panel.setLayout(new GridLayout(6,2));
                            panel.add(lblmess);
                            panel.add(choice1);
                            panel.add(choice2);
                            panel.add(choice3);	
                            panel.add(choice4);
                            panel.add(mark);
                            panel.add(btnext);
                            cont.add(panel);
                            	setVisible(true);
                            		askq();
    
    }
    
    
    
    public void initialize()
    {
      try
      {
      System.out.println("\nReading word\n");
      BufferedReader br = new BufferedReader(new FileReader("inp.txt"));
      String line;
      int i=0;
      wor = new String[9];
      mean= new String[9];
      do
      {
       line=br.readLine();
       if(line != null)
        {
    	  word = new String[3];  
          word=line.split(";");
         wor[i]=word[0];
         mean[i]=word[1];
    	//System.out.println(wor[i]);
           i++;        	//System.out.println(mean[i]);
    	
          }
     }while(line != null);
    }catch (IOException e) {
        e.printStackTrace();
    }
     }
    
    
    
    public void askq()
    
    {
       Random rand =new Random();
      ques = wor[(l=(rand.nextInt(4)))];
    
    
    String[] name = new String[4];
      for(int j=0; j<3 ;j++)
        name[j] = mean[rand.nextInt(8)];
        name[3]=mean[l];
        
        String[] names = {name[0], name[1], name[2], name[3]}; 
        Collections.shuffle( Arrays.asList(names));
        
    lblmess.setText(ques);
    choice1.setText(names[0]);
    choice2.setText(names[1]);
    choice3.setText(names[2]);
    choice4.setText(names[3]);
    
    
    
    }
    
    public void getSelection(){
    	
    	Enumeration<AbstractButton> buttons=bg.getElements();
    	while(buttons.hasMoreElements())
    	{
    		JRadioButton temp=(JRadioButton)buttons.nextElement();
    		if(temp.isSelected())
    		{
    			selectedChoice=temp.getText();
    	   
    		}
    	}
    	
    }	
    
     public int calc()
     {
        
    	 count++;
    	 System.out.println(count);
       return count;	
    	
     }
    
    
    }
    class next extends Quiz implements ActionListener{
    	
    		public void actionPerformed(ActionEvent s) {
    			
    			
    			 
    			
    		      if(btnext.getText().equals("next")){
    		    	
    		    	      getSelection();
    		    	  
    		    		
    		    		//   System.out.println(selectedChoice);
    						if(selectedChoice == mean[l]){
    						
    		    			  calc();
    		    		      }
    		      askq();
    		      }	      	
    	}
    }
    
    
    class mark extends Quiz implements ActionListener{
    	public void actionPerformed(ActionEvent e)
    	{
    		if(mark.getText().equals("mark"))
    		{
    			JOptionPane.showMessageDialog( null, "The sum is " + count, "Results", JOptionPane.PLAIN_MESSAGE );
    			
    			System.exit(0);
    		}
    			
    	}
    	
    	
    }
    
    public class Quizz{
    public static void main(String args[]){
    new Quiz();
    }
    }
    Last edited by Adithya; 06-21-2014 at 11:13 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: function does not calls in actionListener

    Quote Originally Posted by Adithya View Post
    Thank you sir..
    now i added two listener.but i can't get GUI.what's the problem is.
    What do you mean by 'I can't get the GUI'?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Adithya is offline Member
    Join Date
    Jun 2014
    Posts
    24
    Rep Power
    0

    Default Re: function does not calls in actionListener

    actually i cannot get the Jframe on window.there is no output.moreover there is no error

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: function does not calls in actionListener

    I still don't understand what you mean; b.t.w. change line #58 in you code to:

    Java Code:
    public Quiz() { ... }
    constructors don't have a return type, not even void.

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Recursive function to iteractive function
    By mikeZet in forum New To Java
    Replies: 0
    Last Post: 03-13-2012, 01:42 AM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  3. Making calls from pc
    By ilyaufo in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 05-17-2010, 04:38 PM
  4. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  5. Replies: 2
    Last Post: 08-03-2007, 04:06 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
  •