Results 1 to 9 of 9
Thread: JFrame repaint()
- 03-15-2012, 09:49 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
JFrame repaint()
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
Java Code:class monthListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("jan")){ month = "jan"; Calander1 calander = new Calander1(); calander.repaints(); } } }
The paintComponent class : Obviously not all of the code in their is relevant to this
Java Code: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; } }
- 03-16-2012, 12:54 AM #2
Re: JFrame repaint()
What does the Calendar1 class's repaints() method do?
If you create a new instance of the Calendar1 class in the listener method, is that the one you want to do the display or is there another one that has the data to be displayed?
- 03-16-2012, 01:26 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Re: JFrame repaint()
I want to repaint the calander1 panel that has already been created. The repaints method is just a method that has the repaint method within it
- 03-16-2012, 01:34 AM #4
Re: JFrame repaint()
You will need a reference to the existing instance of the class so you can call its methods.
You could pass one to the MonthListener class(in its constructor) when you create an instance of it.
- 03-16-2012, 01:41 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
- 03-16-2012, 01:45 AM #6
Re: JFrame repaint()
I was referring to the constructor for the MonthListener class. You need the reference to the Calender1 class inside of the MonthListener class. When you create the MonthListener instance, pass a reference to the Calender1 class in the MonthListener's constructor.
- 03-16-2012, 01:50 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Re: JFrame repaint()
Im not trying to get spoonfed answers here, but how exactly would I do this i'm fairly confused
- 03-16-2012, 01:58 AM #8
Re: JFrame repaint()
I'm not sure where you are having problems.
Do you have a reference to the Calender1 object when you create the MonthListener object?
Do you know how to write a class's constructor?
Do you know how to pass a variable to a constructor when you create an instance of the class using new?
Do you know how to have the constructor save the value that is passed to it in a class level variable?
- 03-16-2012, 02:02 AM #9
Re: JFrame repaint()
dbJava Code:public class A { public void aMethod() { } } public class B { private A a; public B(A a) { this.a = a; } public void bMethod() { a.aMethod(); } public static void main(String[] args) { A a = new A(); B b = new B(a); b.bMethod(); } }Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Repaint on a JFRAME
By Jakers in forum AWT / SwingReplies: 6Last Post: 01-24-2012, 05:17 PM -
Calling another JFrame from a JFrame through a JButton...(Help Reqired)
By nish.singha20 in forum AWT / SwingReplies: 3Last Post: 11-27-2011, 05:18 AM -
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
Repaint the entire JFrame (GroupLayout)
By Willi in forum AWT / SwingReplies: 13Last Post: 12-19-2009, 10:11 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks