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

Thread: Needed help in actionPerformed statements

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Needed help in actionPerformed statements

    Question:


    My program:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class GUI extends JFrame implements ActionListener
    {
    	JButton btn1;
    	JButton btn2;
    	JButton btn3;
    	JTextArea area1;
     
    	GUI()
    	{
    		setLayout (new BorderLayout());
     
    		btn1 = new JButton ("Elephant");
    		btn2 = new JButton ("Mickey");
    		btn3 = new JButton ("Butterfly");
    		area1 = new JTextArea ("");
     
    		JPanel p1 = new JPanel();
    		JPanel p2 = new JPanel();
     
     
    		p1.add (btn1);
    		p1.add (btn2);
    		p1.add (btn3);
    		p2.add (area1);
     
    		add(p2, BorderLayout. CENTER);
    		add(p1, BorderLayout. SOUTH);
    	}
     
    		public static void main (String []args)
    		{
    			String [] photos = {"elephant.jpg", "mickey.jpg", "butterly.jpg"};
     
    			GUI t = new GUI();
     
    			t.setSize(500,150);
    			t.setVisible(true);
    			t.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    			t.setTitle("Button and Icons");
    		}
    		public void actionPerformed(ActionEvent evt)
    		{
    			if(evt.getSource() == btn1)
    			{
    				area1.append();
    			}
    		}
     
    }

    What shall I write in public void actionPerformed(ActionEvent evt) to display the picture from the array?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Needed help in actionPerformed statements

    Was your question not answered over at the the java forums? If not, I recommend you define the question more precisely so you can get a more informative response.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Needed help in actionPerformed statements

    Quote Originally Posted by copeg View Post
    Was your question not answered over at the the java forums? If not, I recommend you define the question more precisely so you can get a more informative response.
    It was answered but I dont really get it clearly. So, Im trying some other places expecting I can understand any different answers than in Java Forums.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Needed help in actionPerformed statements

    Quote Originally Posted by S-NESH View Post
    It was answered but I dont really get it clearly. So, Im trying some other places expecting I can understand any different answers than in Java Forums.
    With the solution provided, what exactly don't you get? Any other solution may leave you with the same fundamental question, so addressing this directly might be beneficial.

  5. #5
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Needed help in actionPerformed statements

    Quote Originally Posted by S-NESH View Post
    It was answered but I dont really get it clearly. So, Im trying some other places expecting I can understand any different answers than in Java Forums.
    what exactly did you don't get? I provided examples of where you can put your handlers in your code.

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Needed help in actionPerformed statements

    Chill guys.. Im a newbie in Java.. So, Im kinda blur when I get to know something new. Need some time to understand them )=

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Needed help in actionPerformed statements

    Quote Originally Posted by S-NESH View Post
    Chill guys.. Im a newbie in Java.. So, Im kinda blur when I get to know something new. Need some time to understand them )=
    I've never heard of putting an image in a JTextArea.

    I do know that you can have just an image as a JLabel.

    There's a JLabel constructor that only takes an Icon as a parameter.

    Also, JTextArea won't have any size unless you set width and height anyway.

    And append only adds text, not images.

    JLabel(Icion image)

    However, put the files in your constructor. And don't make them String objects. Make them ImageIcon objects(Icon is an interface, an extreme example of an abstract class, so it can't be initialized).

    For your actionListener, use the JLabel class setIcon(Icon image) method to change it.

    However, you should probably have another image, a default one. Just a white background. Call it WhiteBackground.jpg or something.
    Last edited by javapenguin; January 7th, 2011 at 02:00 PM.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Needed help in actionPerformed statements

    JTextArea cannot accept anything except for "plain text" (no images/movies, all text is formatted exactly the same).

    If you want, you can try using a JEditorPane (or JTextPane) as both of these components support formatted text (either RTF, HTML, or your own formatting) as well as images/movies/etc. However, from looking over your project specifications the easiest way to draw a picture is to use a JLabel and set it to contain an Icon.

    See: How to Use Labels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) and How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Your actionPerformed method doesn't need to change the JLabel, simply create an ImageIcon for the three pictures and change the icon for the JLabel using the setIcon() method.

Similar Threads

  1. returning String from ActionPerformed
    By Bill'o in forum AWT / Java Swing
    Replies: 5
    Last Post: December 17th, 2010, 10:16 PM
  2. actionPerformed method
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 4th, 2010, 02:29 AM
  3. actionPerformed
    By Kumarrrr in forum Java Theory & Questions
    Replies: 5
    Last Post: November 23rd, 2010, 09:08 AM
  4. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  5. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM