14 Replies - 611 Views - Last Post: 16 March 2012 - 08:55 PM Rate Topic: -----

#1 bballin24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 12

JFrame repaint()

Posted 15 March 2012 - 02:07 PM

Hey,

Right now I am doing a project in my Computer science IB (International baccalaureate) Higher level program, and I am having trouble with a repaint method within my code.

The current issue is that, I have a base frame that needs to be repainted when a button is pressed from another frame in another class.

The base is that the actionPerformed method in class Months needs to call the paintComponent method in class Calander1. Both classes are different and have seperate frames

Code is shown below, I need the monthListener to basically call the paintComponent method in the other class

class monthListener implements ActionListener {
			
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("jan")){
					month = "jan";
					Calander1 calander = new Calander1();
					calander.repaints();
				}
				
			}
			
		}




public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Months month = new Months();
		
		System.out.println(month.getMonth());
		System.out.println("Called the paintComponent outside");
		if (month.getMonth().equals("jan")){
			System.out.println("Called the paintComponent inside");
		}

		// Setting the variables in the frame to the correct amount
		friends = friends();
		school = school();
		activities = activities();
		startTime1 = start();
		endTime1 = end();
		amount = amount();

		if (friends==0 && activities == 0){
			school = 0;
		}
		else{
			school = school - friends - activities;
		}

		// The next section includes drawing the rectangle and the strings
		// inside the rectangle
		for (int count = 0; count < 7; count++) {

			for (int count2 = 0; count2 < 5; count2++) {
				g.drawRect(x, y, 100, 100);

				if (count2 == 0 && count == 1) {
					g.drawString("Total events " + amount, x + 5, y + 11);
					g.drawString("School Events: " + school, x + 5, y + 30);
					g.drawString("Friend Events: " + friends, x + 5, y + 49);
					g.drawString("Activity Events: " + activities, x + 5,
							y + 65);
					g.drawString("Start of Events: " + startTime1, x + 5,
							y + 80);
					g.drawString("End of Events: " + endTime1, x + 5, y + 99);
				}
				y += 100;
			}
			y = 100;
			x += 100;

		}
		// This section includes drawing the days of the week
		for (int count = 0; count < 7; count++) {
			g.setFont(daysFont);
			g.setColor(Color.BLUE);
			g.drawString(days[count], daysX, daysY);

			daysX += 100;
		}

	}



Is This A Good Question/Topic? 0
  • +

Replies To: JFrame repaint()

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1974
  • View blog
  • Posts: 4,819
  • Joined: 10-September 10

Re: JFrame repaint()

Posted 15 March 2012 - 02:21 PM

What does the repaints() method do?

You might have to post the complete code of both JFrames to get useful help.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JFrame repaint()

Posted 15 March 2012 - 02:24 PM

And how does your Calendar1 constructor looks like ?
As you create a new Calendar1 object that is a Jframe (if I understood well), its constructor probably does a setVisible(true) so you won't even need to call repaint() on it.
If its constructor does not do a setVisible(true) and setBounds() to it you will have to do it from your ActionListener (strill nio repaint()) needed

If Calendar1 is not a JFrame, then you will have to add() it to a Container before it can be displayed.
Was This Post Helpful? 1
  • +
  • -

#4 bballin24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 12

Re: JFrame repaint()

Posted 15 March 2012 - 02:55 PM

View PostGregBrannon, on 15 March 2012 - 02:21 PM, said:

What does the repaints() method do?

You might have to post the complete code of both JFrames to get useful help.


The repaints method is simply a method with the repaint method within it, I tried to use it so I could create an object of that class and call it from another but that didn't work

View Postpbl, on 15 March 2012 - 02:24 PM, said:

And how does your Calendar1 constructor looks like ?
As you create a new Calendar1 object that is a Jframe (if I understood well), its constructor probably does a setVisible(true) so you won't even need to call repaint() on it.
If its constructor does not do a setVisible(true) and setBounds() to it you will have to do it from your ActionListener (strill nio repaint()) needed

If Calendar1 is not a JFrame, then you will have to add() it to a Container before it can be displayed.


My calendar1 constructor looks like :

	public Calander1() {
		this.add(button, BorderLayout.SOUTH);

		class buttonListener implements ActionListener {

			public void actionPerformed(ActionEvent e) {
				// Repainting the frame and resetting variables.
				x = 1;
				y = 100;
				daysX = 15;
				daysY = 97;
				repaints();
				
				
			}

		}


Im really confused on how to get it to repaint though unfortunetly

The repaints method looks like this

public void repaints(){
		repaint();
		System.out.println("repaints method called");
	}


View Postpbl, on 15 March 2012 - 02:24 PM, said:

And how does your Calendar1 constructor looks like ?
As you create a new Calendar1 object that is a Jframe (if I understood well), its constructor probably does a setVisible(true) so you won't even need to call repaint() on it.
If its constructor does not do a setVisible(true) and setBounds() to it you will have to do it from your ActionListener (strill nio repaint()) needed

If Calendar1 is not a JFrame, then you will have to add() it to a Container before it can be displayed.


The Calander1 object is a JPanel sorry that I mis-spoke in the above post !



My calendar1 constructor looks like :

	public Calander1() {
		this.add(button, BorderLayout.SOUTH);

		class buttonListener implements ActionListener {

			public void actionPerformed(ActionEvent e) {
				// Repainting the frame and resetting variables.
				x = 1;
				y = 100;
				daysX = 15;
				daysY = 97;
				repaints();
				
				
			}

		}


Im really confused on how to get it to repaint though unfortunetly

The repaints method looks like this

public void repaints(){
		repaint();
		System.out.println("repaints method called");
	}

Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,730
  • Joined: 29-July 11

Re: JFrame repaint()

Posted 15 March 2012 - 02:58 PM

As long as both objects are swing components, you can just call their repaint() methods in actionPerformed().

jframe1.repaint();
jframe2.repaint();



What is the problem?
Was This Post Helpful? 0
  • +
  • -

#6 bballin24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 12

Re: JFrame repaint()

Posted 15 March 2012 - 03:52 PM

View Postfarrell2k, on 15 March 2012 - 02:58 PM, said:

As long as both objects are swing components, you can just call their repaint() methods in actionPerformed().

jframe1.repaint();
jframe2.repaint();



What is the problem?


The panel I want to repaint is in a different class so I cant just call it
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JFrame repaint()

Posted 15 March 2012 - 06:22 PM

Obviously your Calendar1 extends JFrame
you will have to set its bounds and to setVisible(true)

No need to call repaint()

P.S.
very seldom you'll have to call repaint() on a JComponent if you do not do custom drawing
All JComponent call repaint() themselve if you change one of their properties (size, color, text, ...)
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,730
  • Joined: 29-July 11

Re: JFrame repaint()

Posted 15 March 2012 - 07:17 PM

View Postbballin24, on 15 March 2012 - 10:52 PM, said:

View Postfarrell2k, on 15 March 2012 - 02:58 PM, said:

As long as both objects are swing components, you can just call their repaint() methods in actionPerformed().

jframe1.repaint();
jframe2.repaint();



What is the problem?


The panel I want to repaint is in a different class so I cant just call it


You could pass a reference to the other JFrame via the constructor of the Calendar Class if you need to communicate with the other JFrame, or you *could* even write a little static method in each of your JFrame classes that could do a repaint, which you could then call from anywhere. The first option i like more.
Was This Post Helpful? -1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JFrame repaint()

Posted 15 March 2012 - 07:25 PM

As I have already said at least twice: there is no need to call repaint(). The problem is the Calendar1 not set visible and not having a size.

And there is no need to pass a reference to second element, the second JFrame, that is not displayed, is build inside the actionPerformed() of the first JFrame.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9028
  • View blog
  • Posts: 33,489
  • Joined: 27-December 08

Re: JFrame repaint()

Posted 15 March 2012 - 07:56 PM

Why separate your Listener from the GUI class to begin with? It would be easier to have the GUI classes handle their own events.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JFrame repaint()

Posted 15 March 2012 - 07:58 PM

Might be an inner class ? Without all the code difficult to say
Was This Post Helpful? 0
  • +
  • -

#12 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,730
  • Joined: 29-July 11

Re: JFrame repaint()

Posted 16 March 2012 - 02:34 PM

From what I gather, he has two separate JPanels in two separate JFrames It seems like he wants to add a button and ActionListener to one JFrame that will call repaint() on both JPanels. I admit I am not very good at Java, but it seems to me that the class containing his actionListener needs to be aware of both JPanels. Am I missing something?

This post has been edited by farrell2k: 16 March 2012 - 02:35 PM

Was This Post Helpful? -1
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JFrame repaint()

Posted 16 March 2012 - 02:43 PM

For the third/fourth times:
- unless you do not do customize drawing you do not need to call repaint() on any JComponent
- the problem is a JFrame created without a setVisible() and a setSize()
Was This Post Helpful? 0
  • +
  • -

#14 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,730
  • Joined: 29-July 11

Re: JFrame repaint()

Posted 16 March 2012 - 03:11 PM

View Postpbl, on 16 March 2012 - 09:43 PM, said:

For the third/fourth times:
- unless you do not do customize drawing you do not need to call repaint() on any JComponent
- the problem is a JFrame created without a setVisible() and a setSize()


He's talking about paintComponent() being called in the other JPanel. It sounds like custom painting to me, unless you think he's doing something else. He also told you his Calendar1 object was a JPanel. Ah, whatever.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JFrame repaint()

Posted 16 March 2012 - 08:55 PM

View Postfarrell2k, on 16 March 2012 - 06:11 PM, said:

Ah, whatever.

Unless we have clarification from the OP you are right
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1