Results 1 to 9 of 9
  1. #1
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default Creating a GUI events with action listeners

    I am new to the forums and relatively new to Java, and most certainly very new to GUI programming. I am a high school student who needs a ton of help with this type of programming so I apologize in advance if I frustrate someone because I am slow. I have been searching for days for any sort of leeway on this program.

    I wanted to add an action listener to a radio button, that would resize the window. The radio buttons are is in bold. My code is large, so I'll post it in italicized to differentiate from my words and code. I have no idea how to start and even tried using the Java Documentation, but didn't find it detailed enough to help me. Well, here it is:


    Java Code:
    //This is my package
    
    package package09;
    
    //Imports the necessary files to run the program
    
    import java.awt.*;
    
    import javax.swing.*;
    
    import javax.swing.border.Border;
    
    // This is the class boxDemo which comes from JFrame
    
    class boxDemo extends JFrame {
    
    	//Holds the details of my window
    
    	public boxDemo() {
    
    		// Name of the window
    
    		super("Computer Science E-mail Server by Sid Patel");
    
    		// set the dimensions (width, height) of the frame (in pixels)
    
    		setSize(1000, 500);
    
    		// decides what to do when the frame is closed
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		// make it visible
    
    		setVisible(true);
    
    		// create a Content container for the frame
    
    		Container contentArea = getContentPane();
    
    		contentArea.setBackground(Color.CYAN);
    
    		// set up a layoutManager for the Container
    
    		BorderLayout myManager = new BorderLayout();
    
    		contentArea.setLayout(myManager);[/B]
    
    		//___________________________________________________ Panel divider
    
    		//South Panel and color
    
    		JPanel South = new JPanel ();
    
    		South.setBackground (Color.GREEN);
    
    		// Create and add a "New User: Sign-Up" button and location
    
    		JButton depositButton = new JButton("New User: Sign-Up");
    
    		South.add(depositButton, BorderLayout.SOUTH);
    
    		// Create and add a "Forgot Password?" button and location
    
    		JButton balanceButton = new JButton("Forgot Password?");
    
    		South.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    		
    		South.add(balanceButton, BorderLayout.SOUTH);
    
    		//Add the items to the window in that location
    
    		contentArea.add (South, BorderLayout.SOUTH);
    
    		//___________________________________________________ Panel divider
    
    		//North Panel and color
    
    		JPanel North = new JPanel ();
    
    		North.setBackground (Color.BLUE);
    
    		//Create and add a "E-mail Address: " text label and location
    
    		JLabel field1Label = new JLabel("E-mail Address: ");
    
    		North.add(field1Label, BorderLayout.NORTH);
    
    		//Create and add a "Enter E-mail Address" text field and location
    
    		JTextField field1 = new JTextField("Enter E-mail Address",15);
    
    		North.add(field1, BorderLayout.NORTH);
    
    		////Create and add a "Password: " text label and location
    
    		JLabel field2Label = new JLabel("Password: ");
    
    		North.add(field2Label, BorderLayout.NORTH);
    
    		//Create and add a "Enter E-mail Address" text field and location
    
    		JTextField field2 = new JTextField("Enter Password",15);
    
    		North.add(field2, BorderLayout.NORTH);
    
    		//Add the items to the window in that location
    
    		contentArea.add (North, BorderLayout.NORTH);
    
    		//___________________________________________________ Panel divider
    
    		[B]//Create and add a new panel in that location
    
    		JPanel Center = new JPanel ();
    
    		contentArea.add (Center, BorderLayout.CENTER);
    
    		//Sets the background color and initiates the buttons
    
    		Center.setBackground (Color.YELLOW);
    
    		ButtonGroup sizes = new ButtonGroup();
    
    
    
    		// Create radio buttons for the group
    
    		JRadioButton sizeSmall = new JRadioButton("Small Window");
    		
    		JRadioButton sizeLarge = new JRadioButton("Large Window");
    
    		// Add the sizes to the group
    
    		sizes.add(sizeSmall);
    
    		sizes.add(sizeLarge);
    
    
    
    		//Add the items to the window in that location
    
    		Center.add(sizeSmall, BorderLayout.CENTER);
    
    		Center.add(sizeLarge, BorderLayout.CENTER);[/B]
    
    		//___________________________________________________ Panel divider
    
    		// Create and add a new panel and color
    
    		JPanel East = new JPanel ();
    
    		East.setBackground (Color.RED);
    
    		//Create a new checkbox in that location
    
    		JCheckBox mySpam = new JCheckBox("Remember my E-mail Address");
    
    		East.add(mySpam, BorderLayout.EAST);
    
    		//Create a new checkbox in that location
    
    		JCheckBox myKindling = new JCheckBox("Remember my password");
    
    		East.add(myKindling, BorderLayout.EAST);
    
    		//Add the items to the window in that location
    
    		contentArea.add (East, BorderLayout.EAST);
    
    		//___________________________________________________ Panel divider
    
    		// Create a new panel and set it a certain color
    
    		JPanel West = new JPanel ();
    
    		West.setBackground (Color.CYAN);
    
    		//Create a new JComboBox and add it to that location
    
    		JComboBox myCustomers = new JComboBox();
    
    		West.add(myCustomers, BorderLayout.WEST);
    
    		// Add items to the ComboBox
    
    		myCustomers.addItem("Choose a color");
    
    		myCustomers.addItem("Red");
    				
    		myCustomers.addItem("Blue");
    
    		myCustomers.addItem("Yellow");
    
    		//Add the items to the window in that location
    
    		contentArea.add (West, BorderLayout.WEST);
    
    		//Create a new JComboBox in that location
    
    		JComboBox myCustomers2 = new JComboBox();
    
    		West.add(myCustomers2, BorderLayout.WEST);
    
    		// Add items to the ComboBox
    
    		myCustomers2.addItem("Select Server");
    
    		myCustomers2.addItem("Server 1");
    
    		myCustomers2.addItem("Server 2");
    
    		myCustomers2.addItem("Server 3");
    
    		//Add the items to the window in that location
    
    		contentArea.add (West, BorderLayout.WEST);
    
    		//___________________________________________________ Panel divider
    
    		// Create a new panel and set it a color
    
    		JPanel panel1 = new JPanel();
    
    		panel1.setBackground(Color.BLUE);
    
    		//Create a new JButton and add it to the panel
    
    		JButton stopButton = new JButton("Sign In");
    
    		panel1.add(stopButton);
    
    		//Add panel in the center
    
    		Center.add(panel1);
    
    		//Create new panel and set it a color
    
    		JPanel panel2 = new JPanel();
    
    		panel2.setBackground(Color.RED);
    
    		//Create a Button and add it to the panel
    
    		JButton goButton = new JButton("Clear Form");
    
    		panel2.add(goButton);
    
    		//Add panel to window
    
    		Center.add(panel2);
    
    		// Actually make the changes
    
    		setContentPane(contentArea);
    	}
    }
    
    [B]//This is my class
    
    public class myGUIevents{
    
    	//This is the class that will run the Demo
    
    	public static class panelDemo {
    
    		//This is the main method
    
    		public static void main (String[] args) {
    
    			// create a new boxDemo object
    
    			boxDemo empty = new boxDemo( );
    
    		}
    
    	}
    }
    Last edited by sidd0123; 04-02-2010 at 11:04 PM. Reason: add code tags

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    well... first of all put your code into "code" tags :) it is too unclear :(

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

    Default

    Quote Originally Posted by sidd0123 View Post
    I wanted to add an action listener to a radio button, that would resize the window. The radio buttons are is in bold. My code is large, so I'll post it in italicized to differentiate from my words and code.
    I strongly suggest that you don't do this, that you instead use code tags when posting code in the forum so that your code will retain its formatting and be readable. Please read the link in my signature below for details. You can still edit your original post, so I'd get rid of the bold and italics and instead use code tags.

    I have no idea how to start and even tried using the Java Documentation, but didn't find it detailed enough to help me. Well, here it is:
    Keep studying. The docs (like most things in life) get easier to understand the more you read them, and it is almost certain that all your answers are present there.

    Also, read the section below on "how to ask smart questions". Your current post essentially states: "I don't know how to do this, no clue at all, and here's a bunch of code I want you to go through and fix". Much better is to tell us as specifically as possible what's working, what's not, what error messages you see,... etc... the key is to try to make it as easy as possible for a volunteer here to be able to help you, and that link will help (it's helped me, that's for sure).

    Best of luck!

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    I also agree the Documentation can be dry at times, but the Java Documentation has a link to the Swing tutorial which has examples of using radio buttons and ActionListeners. So all you have to do is read the API for the Swing component and click on the link to the tutorial.

  5. #5
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    //This is my package

    if your instructor told you to make comments of this nature, take another class

  6. #6
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    can someone tell me where to find the code box?

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

  8. #8
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    ok, i got the code box in. I'll try to clarify my question, and look at the Docs again. I'll post back on my progress soon. Thanks for the advice
    Last edited by sidd0123; 04-02-2010 at 11:22 PM.

  9. #9
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    in a GUI events, do the methods go at the very bottom of your code?

Similar Threads

  1. Help with Listeners
    By Psyclone in forum AWT / Swing
    Replies: 8
    Last Post: 02-09-2010, 07:21 PM
  2. JOptionPane, action listeners, and the Enter key.
    By DigitalMan in forum AWT / Swing
    Replies: 5
    Last Post: 01-26-2010, 02:51 AM
  3. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  4. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10:41 AM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04:26 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
  •