Results 1 to 5 of 5
  1. #1
    gaeronf is offline Member
    Join Date
    Nov 2012
    Location
    PA
    Posts
    7
    Rep Power
    0

    Default Action Listening/Handling Troubles.

    Alright guys, in advance I thank you for reading this. I am doing a project and the prompt is here: http://pafbla.org/handbook/12-13-Pol...rogramming.PDF. You don't really need to read the prompt to fix my problem. So far I have some code (which will be below my ramblings) but I have not put in any ActionHandler yet. I kind of know the basics to ActionHandling (as in I've read numerous tutorials and can sort of get by). However, when you run the code below there is a JButton and a JComboBox. The JComboBox has 4 options besides the Default. So here is what my goal is: When either the JButton is Clicked, or one of the 4 (not including the "Select One" option...) selections in the JComboBox, a new screen appears. This will from there I want to be able to design the layout and content just like the first "panel". However, some of you may think it would be more beneficial to add a main or home button option throughout the entire program so they can get back to this original screen. I unfortunately don't know how to do that. Thanks! Code:

    Java Code:
    //Gaeron Friedrichs
    import javax.swing.*;
    import java.awt.*;
    
    public class swing {
    		
    	String[] options = {"Select One", "New Employee", "New Employer", "Edit Employee", "Edit Employer"};	
    	JComboBox combobox = new JComboBox(options);
    			
    	
    	public swing(){
    		frame();
    	}
    	
    	public void frame (){
    		
    		JFrame frame = new JFrame("Employee Assessment Tool - Gaeron Friedrichs");
    		Container panel = frame.getContentPane();
    		SpringLayout layout = new SpringLayout();
    		panel.setLayout(layout);
    		frame.setSize(950, 600);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				
    		JLabel label = new JLabel("EAT (Employee Assessment Tool) is a system used for employee assessment and evaluation. Please select an    option below.");
    		label.setFont(new Font("Serif", Font.PLAIN, 18));
    		panel.add(label);
    		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, label, 0, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.NORTH, label, -150, SpringLayout.VERTICAL_CENTER, panel);
    		
    		JLabel label2 = new JLabel("Gaeron Friedrichs 2012-2013");
    		label2.setFont(new Font("Serif",Font.PLAIN, 10));
    		panel.add(label2);
    		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, label2, 0, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.VERTICAL_CENTER, label2, -30, SpringLayout.SOUTH, panel);
    		
    		JButton button = new JButton ("New Evaluation");
    		button.setToolTipText("Create a New Evaluation");
    		panel.add(button);
    		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, button, -200, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.SOUTH, button, -50, SpringLayout.VERTICAL_CENTER, panel);
    		
    		combobox.setSelectedIndex(0);
    		panel.add(combobox);
    		combobox.setToolTipText("Select from other options.");
    		layout.putConstraint(SpringLayout.EAST, combobox, 200, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.SOUTH, combobox, -50, SpringLayout.VERTICAL_CENTER, panel);
    	}
    		
    	public static void main (String[] args){
    		new swing();
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    gaeronf is offline Member
    Join Date
    Nov 2012
    Location
    PA
    Posts
    7
    Rep Power
    0

    Default Re: Action Listening/Handling Troubles.

    Well I did have the action handler in there before, but redid the whole thing without even touching the action handler. I can get the action handler in, put it in, no problem. And I could have a message or dialog box come up no problem once a certain thing has occurred. But I was always under the impression that message or dialog boxes were just for messages or dialogs? I need a new "panel" "window" whatever you would refer to it as, so I can take data from the user then assign it later to various strings, arrays, and write it into text files. I hope that helps a little bit more?

  4. #4
    gaeronf is offline Member
    Join Date
    Nov 2012
    Location
    PA
    Posts
    7
    Rep Power
    0

    Default Re: Action Listening/Handling Troubles.

    Alright, so I read some more and kept trying and I did manage to get it to work. I ended up doing it within button.addActionListener...Although, I do know that you can do it outside of (in this case) public void frame1(). However, I could not manage to get that to work. You guys are the experts, or at least know much more than I do. Should I keep it like how I have it? Or do it a better way (which may have to be elaborated on a little). Other than that, thank you so much for your help!

    Java Code:
    //Gaeron Friedrichs
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GUI {
    		
    	String[] options = {"Select One", "New Employee", "New Employer", "Edit Employee", "Edit Employer"};	
    	JComboBox combobox = new JComboBox(options);
    			
    	
    	public GUI(){
    		frame1();
    	}
    	
    	public void frame1 (){
    		
    		JFrame frame = new JFrame("Employee Assessment Tool - Gaeron Friedrichs");
    		frame.setVisible(true);
    		Container panel = frame.getContentPane();
    		SpringLayout layout = new SpringLayout();
    		panel.setLayout(layout);
    		frame.setSize(950, 600);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				
    		JLabel label = new JLabel("EAT (Employee Assessment Tool) is a system used for employee assessment and evaluation. Please select an option below.");
    		label.setFont(new Font("Serif", Font.PLAIN, 18));
    		panel.add(label);
    		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, label, 0, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.NORTH, label, -150, SpringLayout.VERTICAL_CENTER, panel);
    		
    		JLabel label2 = new JLabel("Gaeron Friedrichs 2012-2013");
    		label2.setFont(new Font("Serif",Font.PLAIN, 10));
    		panel.add(label2);
    		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, label2, 0, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.VERTICAL_CENTER, label2, -30, SpringLayout.SOUTH, panel);
    		
    		JButton button = new JButton ("New Evaluation");
    		button.setToolTipText("Create a New Evaluation");
    		panel.add(button);
    		//button.addActionListener(AL1);
    		layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, button, -200, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.SOUTH, button, -50, SpringLayout.VERTICAL_CENTER, panel);
    		
    	    button.addActionListener(new ActionListener() {
    	        public void actionPerformed(ActionEvent e)
                {
    	        	JFrame frame = new JFrame("Employee Assessment Tool - Gaeron Friedrichs");
    	    		frame.setVisible(true);
    	    		Container panel = frame.getContentPane();
    	    		SpringLayout layout = new SpringLayout();
    	    		panel.setLayout(layout);
    	    		frame.setSize(950, 600);
    	    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    		
    	    		JLabel label = new JLabel("You clicked a button");
    	    		panel.add(label);
                }
            });      
     				
    		combobox.setSelectedIndex(0);
    		panel.add(combobox);
    		combobox.setToolTipText("Select from other options.");
    		layout.putConstraint(SpringLayout.EAST, combobox, 200, SpringLayout.HORIZONTAL_CENTER, panel);
    		layout.putConstraint(SpringLayout.SOUTH, combobox, -50, SpringLayout.VERTICAL_CENTER, panel);
    	}
    
    }
    Here is the main method:

    Java Code:
    class MethCaller 
    {
    	public static void main (String[] args)
    	{
    		new GUI();
    	}
    
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action Listening/Handling Troubles.

    For starters your anonymous inner class ActionListener looks good. Next, if you absolutely must show a secondary window, you should have it be a dialog window, either modal or non-modal so that it is secondary to the main window. A JDialog would work well for this and in many ways is built similar to how you'd build your JFrame except that you'd pass the main JFrame in as a parameter to the dialog's constructor. The API will tell you what the constructor needs.

Similar Threads

  1. Replies: 1
    Last Post: 06-30-2011, 10:49 AM
  2. Replies: 3
    Last Post: 08-19-2010, 09:27 AM
  3. Listening for variables?
    By martypapa in forum New To Java
    Replies: 6
    Last Post: 02-09-2010, 10:14 AM
  4. MDB listening
    By nipunalk in forum Advanced Java
    Replies: 5
    Last Post: 01-20-2010, 06:10 AM
  5. Listening to a Printer
    By Mosd in forum New To Java
    Replies: 0
    Last Post: 08-12-2008, 01:21 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
  •