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 6 of 6

Thread: Simple Actionlistener problem

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Simple Actionlistener problem

    Hi,

    This is probably a stupid problem, but my gui skills have kind of faded in a year.

    In the following test code I'm adding a button to a panel, which is added to a frame. The button works fine, but it won't change it's color when pressed. Thanks in advance for any help.

    public class test extends JFrame{
     
    	public static void main (String[]args) {
     
    		test t = new test();
    	}
     
    	public test() {
    		JButton knapp = new JButton("button");
    		JPanel panel = new JPanel();
    		knapp.addActionListener(new listener());
    		panel.add(knapp);
    		add(panel);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
    	private class listener implements ActionListener {
     
    		public void actionPerformed(ActionEvent e) {
    			JButton test = (JButton)e.getSource();
    			test.setBackground(Color.BLACK);
    			System.out.println("why hello thar");
    		}
     
    	}
     
    }
    Last edited by olemagro; October 11th, 2010 at 10:53 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Simple Actionlistener problem

    This is not an ActionListener issue as much as it is a JButton issue.

    It is all good and well to set the color of a JButton, but it will have no effect if the JButton is still transparent. You have to remember to use the JComponent.setOpaque(boolean isOpaque) method for the color to show. So, to fix this, simply before (or after) setting the color of your JButton, include the statement:
    test.setOpaque(true);

    and you should be good.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    olemagro (October 11th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Simple Actionlistener problem

    I'm not sure if the fact that I'm using a mac should have anything to say, but all it did was this:



    Did as you said and put knapp/test.setOpque(true); above or under where I set the color. Thanks for your interest in this matter, has been holding me up for a while..

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Simple Actionlistener problem

    Quote Originally Posted by olemagro View Post
    I'm not sure if the fact that I'm using a mac should have anything to say
    The newer jdk's may have somewhat solved this on mac, but I recall the opacity being an issue for JButton's on my older system. I worked around the issue by creating icons for my buttons rather than using the default OS look and feel.

  6. The Following User Says Thank You to copeg For This Useful Post:

    olemagro (October 11th, 2010)

  7. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Simple Actionlistener problem

    Quote Originally Posted by copeg View Post
    The newer jdk's may have somewhat solved this on mac, but I recall the opacity being an issue for JButton's on my older system.
    Cheers, I'll look into it.

  8. #6
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Simple Actionlistener problem

    Couldn't be arsed getting a new eclipse, but I'm fairly confident it's the operating system that prevents the button itself to change its color. I'll just go around it like you did copeg.

    Thanks to both of you

Similar Threads

  1. SOAP / WSDL Simple (ish) Problem
    By Mezz in forum Java Networking
    Replies: 0
    Last Post: September 29th, 2010, 12:01 PM
  2. [SOLVED] My head Hurts... such a simple problem but I'm stuck!
    By Kassino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2010, 08:38 AM
  3. Simple import problem
    By Mekster in forum Java IDEs
    Replies: 3
    Last Post: November 13th, 2009, 08:17 PM
  4. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM
  5. simple problem w/ appelets which i cant figure out
    By JavaGreg in forum Java Applets
    Replies: 7
    Last Post: August 15th, 2009, 07:22 PM