 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rohit Gupta Guest
|
Posted: Mon May 22, 2006 9:07 am Post subject: Problem in reloading a JFrame |
|
|
I am having a JFrame which constitutes of a JComponent and a panel.
The JComponent is a separate class and the contents it displays is from
a variable in its class.
The problem is that when I change the values of that particular
varibale the JFrame isn't changed , means it still displays the
original diagram. However when I maximize it, I get now 2 JFrames one -
the original small size and another second - maximized one with the
modified values from the field behind the first one (which I am able to
see because I have written the pan function).
I have tried the following methods vaildate(), invalidate(), repaint(),
pack().
Can anyone tell me where does the problem lie and how could I manage to
fix that. Since I am short on time, I couldn't write a short self
compilable code because it would take some time.
I figure validate, invalidate fucntions don't work because I am not
adding or removing any particular component or maybe I am somewhere
wrong in using it. Could someone please elaborate on this.
An early reply would be highly appreciated.
TIA
Rohit |
|
| Back to top |
|
 |
Rohit Gupta Guest
|
Posted: Mon May 22, 2006 9:07 am Post subject: Re: Problem in reloading a JFrame |
|
|
I am updating that field using an update function since it doesnt pass
by value. Also I displayed that value to check wheher it is getting
updated or not. So no problem regarding that side.
Maybe my program has some intrinsic bugs, I shall try to preapre the
SSCCE and post soon. If you could give me some other lines of thougt
that would certainly help!
Rohit |
|
| Back to top |
|
 |
hiwa Guest
|
Posted: Mon May 22, 2006 9:07 am Post subject: Re: Problem in reloading a JFrame |
|
|
Java does pass-by-value for passing method arguments.
So, even if you re-assigned the caller reference, the callee
reference doesn't refer newly assigned object.
You need setXxx() method or something similar to update
the object referred by the callee reference.
However, I feel your code has its intrinsic bug(s).
Post a small demo code that is generally compilable,
runnable and could reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html |
|
| Back to top |
|
 |
Thomas Weidenfeller Guest
|
Posted: Mon May 22, 2006 9:07 am Post subject: Re: Problem in reloading a JFrame |
|
|
Rohit Gupta wrote:
| Quote: | The problem is that when I change the values of that particular
varibale the JFrame isn't changed , means it still displays the
original diagram. However when I maximize it, I get now 2 JFrames one -
the original small size and another second - maximized one with the
modified values from the field behind the first one (which I am able to
see because I have written the pan function).
|
There is a bug on line 42 of your code.
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/ |
|
| Back to top |
|
 |
Rohit Gupta Guest
|
Posted: Mon May 22, 2006 10:07 am Post subject: Re: Problem in reloading a JFrame |
|
|
Ok, finally I have made the SSCCE, you can try it now and tell me what
is the problem...
What I am doing in this piece of code is that I am displaying initially
a button with text "start", on pressing the reload button the text
should alternately change from "dony" and "pony", try maximizing the
applet to see the problem.
Can anyone tell me now what to do.
I am in desperate need of help!!
TIA
Rohit
/*Launch.java*/
package javaapplication1;
public class Launch {
public static void main(String args[]){
Viewer view = new Viewer("start");
view.displayFrame();
view.displayResults();
}
}
/*Viewer.java*/
package javaapplication1;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import java.awt.Panel;
import java.awt.Frame;
class Viewer implements ActionListener {
private String str;
private Button reload;
private JFrame mainFrame;
private Jbit jc;
boolean flag = true;
public Viewer (String str) {
this.str = str;
}
public void displayFrame() {
mainFrame = new JFrame("MODEL VIEWER");
mainFrame.setLocation(200, 100);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void updateModel(String str) {
this.str = str;
}
public void displayResults () {
jc = new Jbit(str);
mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.getContentPane().add("Center", jc);
reload = new Button("Reload");
reload.addActionListener(this);
Panel p = new Panel();
p.add(reload);
mainFrame.getContentPane().add("South", p);
mainFrame.pack();
mainFrame.setSize(600,600);
mainFrame.setVisible(true);
//Default view
}
public void refreshResults(String str) {
jc = new Jbit(str);
mainFrame.setSize(mainFrame.getSize().width,
mainFrame.getSize().height);
mainFrame.getContentPane().add("Center", jc);
mainFrame.invalidate();
mainFrame.validate();
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand();
if (cmd.equals("Reload")) {
if(flag){
refreshResults("pony");
flag = false;
}
else {
refreshResults("dony");
flag = true;
}
jc.validate();
}
}
}
/*Jbit.java*/
package javaapplication1;
import java.applet.Applet;
import java.awt.Button;
import java.awt.BorderLayout;
public class Jbit extends Applet {
private String str;
private Button button1;
//Applet started with a model
public Jbit (String str) {
this.str = str;
init();
}
public void init() {
setLayout(new BorderLayout());
button1 = new Button(str);
add(button1);
}
public void start () {
repaint();
}
public String getString () {
return str;
}
} |
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Wed May 31, 2006 9:10 pm Post subject: Re: Problem in reloading a JFrame |
|
|
"Rohit Gupta" <rg.iitk (AT) gmail (DOT) com> wrote in message
news:1148291381.164115.202880 (AT) i39g2000cwa (DOT) googlegroups.com...
| Quote: | Ok, finally I have made the SSCCE, you can try it now and tell me what
is the problem...
What I am doing in this piece of code is that I am displaying initially
a button with text "start", on pressing the reload button the text
should alternately change from "dony" and "pony", try maximizing the
applet to see the problem.
Can anyone tell me now what to do.
[code snipped] |
There's a lot of problems with this code.
* First of all, you have to decide whether you are trying to write an
application, or an applet.
* Second, you should decide whether you want to use Swing or AWT.
* You should also collect all your GUI creation code into one method,
instead of having half of it in displayFrame() and the other half in
displayResults(). This GUI construction code shouldn't be made publicly
accessible. Instead, call it within the constructor of the class managing
the window.
* The add(String, Component) method you're using has been obsolete since
Java 1.1. We're now at Java 1.5, with 1.6 coming out soon. Use the
add(Component, Object) method instead.
* You say you want to change the text of the button, but your
refreshResults() method doesn't ever even refer to the button.
- Oliver |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|