Results 1 to 2 of 2
  1. #1
    Avey is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Unhappy Handling different types of events

    Hi all, I'm doing an assignment for a class of mine that I would know how to do, and easily, If I were allowed to do it my way. Alas, my teacher wants we to develope a GUI project that allows for input and storage or words with definitions and their type (verb/adjective/noun). If I were developing this on my own I'd just do it all in one file.. But We must have separate files for our extended JFrame, actionlistener, and driver app.

    I am using radio buttons to select either to store the text as a noun, verb, or adjective. And I want the "add entry" button to do that. But before any of that, I must be able to handle both the selection of the radio buttons and the JButton at the same time. I have a generic type class WordLocker that takes Word objects (Verbs, Nouns, and Adjectives) and stores them. The main problem I see with creating two classes where one will handle a radiobutton selection and one a button click is that I wouldn't know how to use my WordLocker class in both handlers simultaneously. Any suggestions would be greatly appreciated.

    tl;dr --- how to handle multiple events in the same class? possible?

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class Controller implements ActionListener {
    	
    	WordLocker myWordLocker = new WordLocker();
    	
    	
    	@Override
    	public void actionPerformed(ActionEvent event) {
    		
    	//handle input	
    		
    		JRadioButton selectRadioButton = (JRadioButton) event.getSource();
    		
    //		JButton buttonClicked = (JButton) event.getSource();
    		
    		System.out.println(selectRadioButton.getActionCommand());
    
    //		System.out.println(buttonClicked.getActionCommand());
    		
    		
    	}
    	
    	Window screen;
    	
    	public void putscreen(Window s)
    	{
    		screen = s;
    	}
    	
    	
    	
    }
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Window extends JFrame {
    	
    	private static final int FRAME_HEIGHT = 200;
    	private static final int FRAME_WIDTH = 300;
    	private static final int FRAME_X_ORIGIN = 150;
    	private static final int FRAME_Y_ORIGIN = 250;
    	
    	private static final int BUTTON_HEIGHT = 50;
    	private static final int BUTTON_WIDTH = 150;
    	
    	private JButton addEntry;
    	private JButton meaning;
    	private JButton saveExit;
    	private JLabel myLabel;
    	private JRadioButton noun, adjective, verb;
    	private JPanel myPanel;
    	
    	public void putListener(Controller l) 
    	{
    		addEntry.addActionListener(l);
    		meaning.addActionListener(l);
    		saveExit.addActionListener(l);
    		noun.addActionListener(l);
    		verb.addActionListener(l);
    		adjective.addActionListener(l);
    	}
    	
    	public Window() {
    		
    		super("My Dictionary");
    		
    		setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		setResizable(false);
    		setLocation(FRAME_X_ORIGIN,FRAME_Y_ORIGIN);
    		setLayout(new FlowLayout());
    		
    		Dimension dim = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);
    		
    		myLabel = new JLabel("This is a sentence.");
    		add(myLabel);
    		
    		addEntry = new JButton("Add Entry");
    		add(addEntry);
    		
    		
    		meaning = new JButton("Definition");
    		add(meaning);
    		
    		saveExit = new JButton("Save and Close");
    		add(saveExit);
    		
    		myPanel = new JPanel();
    		
    		noun = new JRadioButton("Noun");
    		myPanel.add(noun);
    		
    		adjective = new JRadioButton("Adjective");
    		myPanel.add(adjective);
    		
    		verb = new JRadioButton("Verb");
    		myPanel.add(verb);
    		
    		ButtonGroup group = new ButtonGroup();
    		group.add(noun);
    		group.add(verb);
    		group.add(adjective);
    		
    		
    		add(myPanel);
    		
    		
    		setVisible(true);
    		
    		
    		
    		
    		
    	}
    	
    	
    	
    }
    This spits out the text of the radiobutton so far.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Handling different types of events

    Since you have the Actionlistener added just do this in your event handling:

    Then i would handle the events like this:

    Java Code:
    public void actionPerformed(ActionEvent event) {
    
    if(event.getSource() == myButton) {
    
    System.out.print("My button pressed");
    
    }
    else if(event.getSource() == anotherButton) {
    
    //do something
    
    }
    
    }
    You can put as many else if conditions and such as you like so each button does what you want it to, hope that clears it up

Similar Threads

  1. Key Events
    By rdjava in forum Java Gaming
    Replies: 0
    Last Post: 04-15-2011, 08:02 AM
  2. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  3. handling events of a class from another class
    By vandaloop in forum AWT / Swing
    Replies: 4
    Last Post: 10-21-2009, 03:49 AM
  4. Need Help with events
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 09-23-2008, 03:18 AM

Tags for this Thread

Posting Permissions

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