Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with setIcon or something(JButton)

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Problem with setIcon or something(JButton)

    Im having a weird problem with the if statement and the setIcon for the JButton. What I wanted the program to do is, the first button clicked on will show a image on and O then the next button clicked on will have a image of an X. for some reason it only shows the O. It also has the O on the even button clicked on. If say I change the order like have the X show up for the first button clicked then every button I click will just show an X. The while loop and if statement seems to run fine, I ran in debug mode, so idk what could be wrong.
    Thanks


    while(xxx <=9){
     
    	if (xxx  % 2 == 0){
    		a.addActionListener(new EnlargeListener(a));
    		b.addActionListener(new EnlargeListener(b));
    		c.addActionListener(new EnlargeListener(c));
    		d.addActionListener(new EnlargeListener(d));
    		e.addActionListener(new EnlargeListener(e));
    		f.addActionListener(new EnlargeListener(f));
    		g.addActionListener(new EnlargeListener(g));
    		h.addActionListener(new EnlargeListener(h));
    		i.addActionListener(new EnlargeListener(i));
     
    	}
     
    	else {
     
    		a.addActionListener(new o(a));
    		b.addActionListener(new o(b));
    		c.addActionListener(new o(c));
    		d.addActionListener(new o(d));
    		e.addActionListener(new o(e));
    		f.addActionListener(new o(f));
    		g.addActionListener(new o(g));
    		h.addActionListener(new o(h));
    		i.addActionListener(new o(i));
     
    	}
     
    	xxx++;





    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
     
     public class mouse extends JFrame {
     
    		private JButton a = new JButton();
    		private JButton b = new JButton();
    		private JButton c = new JButton();
    		 private JButton d = new JButton();
    		 private JButton e = new JButton();
    			private JButton f = new JButton();
    			private JButton g = new JButton();
    			 private JButton h = new JButton();
    			 private JButton i = new JButton();
     
     
     
     public mouse() {
     
     JPanel p1 = new JPanel(); // Use the panel to group buttons
     p1.setLayout(new GridLayout(3,3));
     
    	p1.add(a);
    	p1.add(b);
    	p1.add(c);
    	p1.add(d);
    	p1.add(e);
    	p1.add(f);
    	p1.add(g);
    	p1.add(h);
    	p1.add(i);
     
    	 JPanel p2 = new JPanel(new BorderLayout());
    	 p2.add(new JTextField("test"),BorderLayout.SOUTH);
    	 p2.add(p1,BorderLayout.CENTER);
    	 add(p2);
     
    int xxx = 1;
    while(xxx <=9){
     
    	if (xxx  % 2 == 0){
    		a.addActionListener(new EnlargeListener(a));
    		b.addActionListener(new EnlargeListener(b));
    		c.addActionListener(new EnlargeListener(c));
    		d.addActionListener(new EnlargeListener(d));
    		e.addActionListener(new EnlargeListener(e));
    		f.addActionListener(new EnlargeListener(f));
    		g.addActionListener(new EnlargeListener(g));
    		h.addActionListener(new EnlargeListener(h));
    		i.addActionListener(new EnlargeListener(i));
     
    	}
     
    	else {
     
    		a.addActionListener(new o(a));
    		b.addActionListener(new o(b));
    		c.addActionListener(new o(c));
    		d.addActionListener(new o(d));
    		e.addActionListener(new o(e));
    		f.addActionListener(new o(f));
    		g.addActionListener(new o(g));
    		h.addActionListener(new o(h));
    		i.addActionListener(new o(i));
     
    	}
     
    	xxx++;
    }
     
     
     
     
     }
     /** Main method */
     public static void main(String[] args) {
     JFrame frame = new mouse();
     frame.setTitle("ControlCircle2");
     frame.setLocationRelativeTo(null); // Center the frame
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(500, 500);
     frame.setVisible(true);
     }
     
     
    		 }

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
     
     
     
    class o implements ActionListener {
    	 private CirclePanel canvas = new CirclePanel();
     
     
    	 JButton cc;
     
    	public o(JButton a) {
    		this.cc=a;
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		canvas.o(cc);
    	}
    }
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
     
    class EnlargeListener implements ActionListener {
    	 private CirclePanel canvas = new CirclePanel();
     
     
    	 JButton x;
     
    	public EnlargeListener(JButton a) {
    		this.x=a;
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		canvas.x(x);
    	}
    }
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JPanel;
     
    class CirclePanel extends JPanel {
    	 private ImageIcon imagei = new ImageIcon("C:/Users/chris/Desktop/x.JPG");
    	 private ImageIcon imageii = new ImageIcon("C:/Users/chris/Desktop/circle.JPG");
    	 Image image = imagei.getImage();
    	 Image imagee = imageii.getImage();
     
     
     
    		public void o(JButton x) {
    			x.setIcon(new ImageIcon(imagee));
     
    		}
     
     
     
    		public void x(JButton x) {
    			// TODO Auto-generated method stub
    			x.setIcon(new ImageIcon(image));
    		}
     
     
     
     
     
    		 }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem with setIcon or something(JButton)

    I've not gone through all that code, but I strongly advise you to use an array to prevent your code from having a lot of unnecessary redundancy. Also, are you sure that you want to add so many ActionListeners to each button? I think that your program would work *much* better if each button only had one ActionListener added to it.

    --- Update ---

    More suggestions:
    Problems:
    • Lack of use of an array of JButtons is causing a lot of unnecessary code redundancy.
    • In your while loop, you are adding 9 ActionListeners to each button which makes no sense whatsoever.
    • Instead each button should have one ActionListener added to it, and it should be the same listener. It should check the state of the button to see the X or O ImageIcon have been set already, and if so then simply return since the button's already been set. If not, it should check the state of the game to see if x or o should be added and then add that icon to the button.
    • There's no need to pass the JButton itself into the listener since you can tell which button has been pressed by extracting it from the ActionEvent object's getSource() method. This will return the button that is tripping the ActionListener to be called.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    leonne (January 6th, 2013)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Problem with setIcon or something(JButton)

    o ok ill do that, yeah I first had it in an array but kept getting compile errors, but ill try it again.
    thxs for the tip

Similar Threads

  1. problem of creating matrix with JButton or Panel
    By ms_ceng in forum AWT / Java Swing
    Replies: 3
    Last Post: December 16th, 2011, 04:53 PM
  2. JButton constructor problem
    By Nieuwenhuizen-jk in forum AWT / Java Swing
    Replies: 14
    Last Post: September 12th, 2011, 11:35 AM
  3. Problem with java swing JButton in Netbeans IDE
    By vrp in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2011, 11:38 PM
  4. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM