Results 1 to 4 of 4
  1. #1
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default JFrames and JPanels with ActionListeners

    I'm just writing some code for practice and have ran into some problems. I am brand new to JFrames and JPanels. Should I be using something else besides JPanels to create two grids of buttons? This code should just read the color of a JRadioButton and set the color of the JButton that is pressed to that color.

    My problems are:
    1. This opens two windows, a 400x400 empty window and a new window that contains the buttons
    2. I don't think the actionPerformed ever gets called on the button press
    3. Should I be using something besides JPanels for the button groups?


    Here is the code, it's kind of ugly:

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    import javax.swing.*;
    import java.applet.Applet;
    
    public class TestingButton extends Program {
    
    	public void init() {
    		JFrame frame = new JFrame();
    		setSize(400,400);
    			
    		// Create Radio Buttons
    		radRed = new JRadioButton(" ");
    		radBlack = new JRadioButton(" ");
    		radGreen = new JRadioButton(" ");
    		radBlue = new JRadioButton(" ");
    		radYellow = new JRadioButton(" ");
    		radWhite = new JRadioButton(" ");
    		
    		radRed.setBackground(Color.RED);
    		radBlack.setBackground(Color.BLACK);
    		radGreen.setBackground(Color.GREEN);
    		radBlue.setBackground(Color.BLUE);
    		radYellow.setBackground(Color.YELLOW);
    		radWhite.setBackground(Color.WHITE);
    		
    		colors = new ButtonGroup();
    		colors.add(radRed);
    		colors.add(radBlack);
    		colors.add(radGreen);
    		colors.add(radBlue);
    		colors.add(radYellow);
    		colors.add(radWhite);
    		
    		radRed.setSelected(true);
    
    		JPanel gamePanel = new JPanel(new GridLayout(7,5));
    		// Layout for grid of buttons
    		//setLayout(new GridLayout(7,5));
            
    		gamePanel.add(new JButton("1"));
    		gamePanel.add(new JButton("2"));
    		gamePanel.add(new JButton("3"));
    		gamePanel.add(new JButton("4"));
    		gamePanel.add(new JButton("Enter 1"));  // replace with ivar enterRow1 or something
            
    		gamePanel.add(new JButton("5"));
    		gamePanel.add(new JButton("6"));
    		gamePanel.add(new JButton("7"));
    		gamePanel.add(new JButton("8"));
    		gamePanel.add(new JButton("Enter 2"));
            
    		gamePanel.add(new JButton("9"));
    		gamePanel.add(new JButton("10"));
    		gamePanel.add(new JButton("11"));
    		gamePanel.add(new JButton("12"));
    		gamePanel.add(new JButton("Enter 3"));
            
    		gamePanel.add(new JButton("13"));
    		gamePanel.add(new JButton("14"));
    		gamePanel.add(new JButton("15"));
    		gamePanel.add(new JButton("16"));
    		gamePanel.add(new JButton("Enter 4"));
            
    		gamePanel.add(new JButton("17"));
    		gamePanel.add(new JButton("18"));
    		gamePanel.add(new JButton("19"));
    		gamePanel.add(new JButton("20"));
    		gamePanel.add(new JButton("Enter 5"));
            
    		gamePanel.add(new JButton("21"));
    		gamePanel.add(new JButton("22"));
    		gamePanel.add(new JButton("23"));
            gamePanel.add(new JButton("24"));
            gamePanel.add(new JButton("Enter 6"));
            
            JPanel colorPanel = new JPanel(new GridLayout(1,6));
            
            colorPanel.add(radRed);
            colorPanel.add(radBlack);
            colorPanel.add(radBlue);
            colorPanel.add(radGreen);
            colorPanel.add(radYellow);
            colorPanel.add(radWhite);
            
            
            
            
            Container cp = frame.getContentPane();
            cp.add(gamePanel, BorderLayout.NORTH);
            cp.add(colorPanel, BorderLayout.SOUTH);
            
            frame.pack();
            frame.setVisible(true);
            
            addActionListeners();
           
    	}
    	
    	
    	
    	public void actionPerformed(ActionEvent e) {
    		JRadioButton jrb = (JRadioButton)colors.getSelection();
    		Color color = jrb.getBackground();
    		JButton jb = (JButton)e.getSource();
    		jb.setBackground(color);
    		crash();  // added this just to test if this is getting called
    		
    	}
    	
    	
    	ButtonGroup colors;
    	JRadioButton radRed;
    	JRadioButton radBlack;
    	JRadioButton radBlue;
    	JRadioButton radGreen;
    	JRadioButton radYellow;
    	JRadioButton radWhite;
    }

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

    Default

    A couple of suggestions:
    • You should get all that ACM stuff out of this code including having your class extend "Program". It should be a pure Swing program, no more and no less.
    • Actually add ActionListeners to your JButtons. The Swing tutorials will show you how, but you'll need to call addActionListener(ActionListener l) for each JButton.
    • JButtons and ButtonGroups are fine to use.
    • Since you want to create a grid (or two ) of JButtons, consider declaring and using a 2-D array of JButton for each grid. Then you can create your buttons and add your listeners in a pair of nested for loops.


    Luck!

  3. #3
    atomant is offline Member
    Join Date
    May 2010
    Posts
    45
    Rep Power
    0

    Default

    I have a couple of questions. How do i get this to work without extending Program? Anything else I try doesn't seem to work. Also, if I use an array for the JButtons will that make it easy to disable/enable rows of buttons? Here is the code I have so far:


    Java Code:
    import acm.program.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TestingButton extends Program {
    
    	public void init() {
    		JFrame frame = new JFrame();
    		setSize(400,400);
    			
    		// Create Radio Buttons
    		radRed = new JRadioButton(" ");
    		radBlack = new JRadioButton(" ");
    		radGreen = new JRadioButton(" ");
    		radBlue = new JRadioButton(" ");
    		radYellow = new JRadioButton(" ");
    		radWhite = new JRadioButton(" ");
    		
    		radRed.setBackground(Color.RED);
    		radBlack.setBackground(Color.BLACK);
    		radGreen.setBackground(Color.GREEN);
    		radBlue.setBackground(Color.BLUE);
    		radYellow.setBackground(Color.YELLOW);
    		radWhite.setBackground(Color.WHITE);
    		
    		colors = new ButtonGroup();
    		colors.add(radRed);
    		colors.add(radBlack);
    		colors.add(radGreen);
    		colors.add(radBlue);
    		colors.add(radYellow);
    		colors.add(radWhite);
    		
    		radRed.setSelected(true);
    		
    		radRed.setFocusable(false);
    		radBlack.setFocusable(false);
    		radGreen.setFocusable(false);
    		radBlue.setFocusable(false);
    		radYellow.setFocusable(false);
    		radWhite.setFocusable(false);
    		
    		
    		JPanel gamePanel = new JPanel(new GridLayout(6,5));
    		// Layout for grid of buttons
    		//setLayout(new GridLayout(7,5));
            
    		// Create buttons with nested for loop
    		for (int i = 0; i < 6; i++) {
    			for (int j = 0; j < 4; j++) {
    				JButton btn = new JButton("  ");
    				btn.addActionListener(this);
    				btn.setFocusable(false);
    				gamePanel.add(btn);
    			}
    			JButton enter = new JButton(" ");
    			enter.addActionListener(this);
    			enter.setFocusable(false);
    			gamePanel.add(enter);
    		}
    		
    		// Panel for JRadioButtons
            JPanel colorPanel = new JPanel(new GridLayout(1,6));
            
            colorPanel.add(radRed);
            colorPanel.add(radBlack);
            colorPanel.add(radBlue);
            colorPanel.add(radGreen);
            colorPanel.add(radYellow);
            colorPanel.add(radWhite);
            
            // Add panels to frames
            Container cp = frame.getContentPane();
            cp.add(gamePanel, BorderLayout.NORTH);
            cp.add(colorPanel, BorderLayout.SOUTH);
            
            frame.pack();
            frame.setVisible(true);
            
            addActionListeners();
           
    	}
    
    		
    	public void actionPerformed(ActionEvent e) {
    		
    		// cycle through JRadioButtons to find selected
    		JRadioButton jrb = null;
    		if (radRed.isSelected()) jrb = radRed;
    		else if (radBlack.isSelected()) jrb = radBlack;
    		else if (radBlue.isSelected()) jrb = radBlue;
    		else if (radGreen.isSelected()) jrb = radGreen;
    		else if (radYellow.isSelected()) jrb = radYellow;
    		else if (radWhite.isSelected()) jrb = radWhite;
    		
    		// get color of JRadioButton
    		Color color = jrb.getBackground();
    		
    		// get source of button clicked
    		JButton jb = (JButton)e.getSource();
    		
    		if (jb.getActionCommand() == " ") {
    			// TODO enter method goes here
    		}
    		
    		else {		
    			// set color of button clicked
    			jb.setBackground(color);
    		}
    	}
    	
    
    	ButtonGroup colors;
    	JRadioButton radRed;
    	JRadioButton radBlack;
    	JRadioButton radBlue;
    	JRadioButton radGreen;
    	JRadioButton radYellow;
    	JRadioButton radWhite;
    }
    Thanks,
    Adam

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How do i get this to work without extending Program?

    Did you find the Swing tutorial? How to use Buttons etc has an example of a program using buttons. The code given there is a very good example of a basic Swing starting point. Notice especially the last two methods of the class: main() and createAndShowGUI(). The other methods will be specific to your program.

    if I use an array for the JButtons will that make it easy to disable/enable rows of buttons?

    It is easy to identify a row of buttons by their place in the array.

    ----------------

    Work a step at a time towards your goal. Get the layout right and then add the behaviour.

Similar Threads

  1. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  2. Replies: 2
    Last Post: 05-28-2010, 02:48 PM
  3. Question about ActionListeners and JAR files
    By Psyclone in forum AWT / Swing
    Replies: 6
    Last Post: 03-20-2010, 12:08 PM
  4. Replies: 1
    Last Post: 12-07-2008, 05:50 PM
  5. Adding JPanels to JFrames based on x-y co-ordinates
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 07-24-2008, 10:41 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
  •