Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: ActionPerformed, paintComponent

  1. #1
    MasterJava is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default ActionPerformed, paintComponent

    Hi, I want to draw on JPanel as a reaction from Timer. Therefore I use the ActionListener (actionPerformed). But how can I make then the drawing I mean, how should I call paintComponent() for drawing.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: ActionPerformed, paintComponent

    What have you tried? Where is your example code demonstrating what you've got so far?

    Are you just asking for the repaint() method?
    gimbal2 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: ActionPerformed, paintComponent

    Bingo. The custom painting section of the tutorial has an example of doing it on a click:

    Creating the Demo Application (Step 3) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Performing Custom Painting)

    Which is basically the same deal; only the source event is different (not on a click event, but on a timed event)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: ActionPerformed, paintComponent

    But how can I make then the drawing I mean, how should I call paintComponent() for drawing.
    You don't call paintComponent() directly.

    You need to create a method on your custom panel that changes a property of the panel. That method will then invoke revalidate() and repaint() on itself.

    For example here is a JLabel that updates itself every second:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class TimerTime extends JFrame implements ActionListener
    {
    	JLabel timeLabel;
    
    	public TimerTime()
    	{
    		timeLabel = new JLabel( new Date().toString() );
    		getContentPane().add(timeLabel, BorderLayout.NORTH);
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		System.out.println(e.getSource());
    		timeLabel.setText( new Date().toString() );
    	}
    
    	public static void main(String[] args)
    	{
    		TimerTime frame = new TimerTime();
    		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    		frame.pack();
    		frame.setVisible(true);
    
    		int time = 1000;
    		javax.swing.Timer timer = new javax.swing.Timer(time, frame);
    		timer.setInitialDelay(1);
    		timer.start();
    	}
    }

  5. #5
    MasterJava is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: ActionPerformed, paintComponent

    Thank you guys. repaint did it. I was just painting a clock with a pointer.

Similar Threads

  1. ActionListener - actionPerformed
    By collwill in forum New To Java
    Replies: 2
    Last Post: 04-21-2011, 06:01 PM
  2. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  3. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  4. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 04:02 AM
  5. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 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
  •