 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Passero Guest
|
Posted: Fri Dec 19, 2003 1:06 pm Post subject: Strange problem with a JFrame |
|
|
When i load my program i get an empty pane and when i resize the JFrame, i
get my JButton on the screen. I don't see where the problem is, i use to do
it on that way and it always worked...
Can someone see the problem?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Hoofdscherm extends JFrame
{
private Beheerder deBheerder;
private JButton btnMatrixVolledig, btnMatrixVariabelen, btnMatrixVgl;
public Hoofdscherm(Beheerder deBeheerder)
{
super("SimplexMethode");
this.deBheerder = deBeheerder;
ActionEventHandler handler = new ActionEventHandler();
Container c = getContentPane();
c.setLayout(new FlowLayout());
btnMatrixVolledig = new JButton("Volledige matrix ingeven");
btnMatrixVolledig.addActionListener(handler);
c.add(btnMatrixVolledig);
setVisible(true);
setBounds(0,0,800,600);
}
private class ActionEventHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
|
|
| Back to top |
|
 |
Passero Guest
|
Posted: Fri Dec 19, 2003 1:47 pm Post subject: Stranger solution.... |
|
|
Here's the solution:
I have to write:
setBounds(0,0,800,600);
setVisible(true);
instead of first setVisible and then setBounds....
I really don't get it
|
|
| Back to top |
|
 |
johnR@notmail.com Guest
|
Posted: Fri Dec 19, 2003 1:54 pm Post subject: Re: Strange problem with a JFrame |
|
|
| Quote: | When i load my program i get an empty pane and when i
resize the JFrame, i get my JButton on the screen. I don't
see where the problem is, i use to do it on that way and it
always worked...
Can someone see the problem?
|
It works for me after I swapped the following
lines like this :
setBounds(0,0,800,600);
setVisible(true);
|
|
| Back to top |
|
 |
johnR@notmail.com Guest
|
Posted: Fri Dec 19, 2003 2:00 pm Post subject: Re: Stranger solution.... |
|
|
| Quote: | setBounds(0,0,800,600);
setVisible(true);
instead of first setVisible and then setBounds....
I really don't get it
|
Well, the button can only be placed after the size of the
panel is known.
And setVisible() just paints the screen (with or without
the button).
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Fri Dec 19, 2003 4:19 pm Post subject: Re: Strange problem with a JFrame |
|
|
"Passero" <yanongena (AT) pandora (DOT) be> wrote
| Quote: | When i load my program i get an empty pane and when i resize the JFrame, i
get my JButton on the screen. I don't see where the problem is, i use to
do
it on that way and it always worked...
Can someone see the problem?
import javax.swing.*; |
import java.awt.event.*;
import java.awt.*;
public class Hoofdscherm extends JFrame
{
// private Beheerder deBheerder;
private JButton btnMatrixVolledig, btnMatrixVariabelen, btnMatrixVgl;
// public Hoofdscherm(Beheerder deBeheerder)
public Hoofdscherm()
{
super("SimplexMethode");
// this.deBheerder = deBeheerder;
ActionEventHandler handler = new ActionEventHandler();
Container c = getContentPane();
c.setLayout(new FlowLayout());
btnMatrixVolledig = new JButton("Volledige matrix ingeven");
btnMatrixVolledig.addActionListener(handler);
c.add(btnMatrixVolledig);
pack();
setVisible(true);
setBounds(0,0,800,600);
}
private class ActionEventHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
public static void main(String[] args)
{
Hoofdscherm h = new Hoofdscherm();
}
}
1) You need to improve your 'small self contained,
compileable example' style, I had to comment
out various lines and add a main.
2) Learn some Layout managers, you may be
usedf to AWT where default llayout was FlowLayout,
but in Swing it is BorderLayout, does not go down
well with adding multiple components the way you
added the button.
HTH
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
|
|
| Back to top |
|
 |
Jon A. Cruz Guest
|
Posted: Mon Jan 05, 2004 8:41 pm Post subject: Re: Strange problem with a JFrame |
|
|
Passero wrote:
public class Hoofdscherm extends JFrame
| Quote: | {
private Beheerder deBheerder;
private JButton btnMatrixVolledig, btnMatrixVariabelen, btnMatrixVgl;
public Hoofdscherm(Beheerder deBeheerder)
{
|
....
| Quote: | setVisible(true);
setBounds(0,0,800,600);
}
|
There are two problems with that.
1) You should not set visible from within a constructor.
Hoofdscherm tmp = new Hoofdscherm( thing );
tmp.pack();
tmp.show();
2) You should not be explicitly setting bounds on a JFrame. Instead call
pack() on it. Leave the numbers up to layout managers.
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Tue Jan 06, 2004 3:50 am Post subject: Re: Strange problem with a JFrame |
|
|
"Jon A. Cruz" <jon (AT) joncruz (DOT) org> wrote
| Quote: | Passero wrote:
public class Hoofdscherm extends JFrame
{
private Beheerder deBheerder;
private JButton btnMatrixVolledig, btnMatrixVariabelen,
btnMatrixVgl;
public Hoofdscherm(Beheerder deBeheerder)
{
...
setVisible(true);
setBounds(0,0,800,600);
}
There are two problems with that.
1) You should not set visible from within a constructor.
|
Why? [ I do it almost invariably.. ]
[ but note that I am also doing work in AWT,
for which Component.show was deprecated in 1.1.. ]
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
|
|
| Back to top |
|
 |
Jon A. Cruz Guest
|
Posted: Wed Jan 14, 2004 5:26 pm Post subject: Re: Strange problem with a JFrame |
|
|
Andrew Thompson wrote:
| Quote: | "Jon A. Cruz" <jon (AT) joncruz (DOT) org> wrote in message
news:3FF9CBE8.7080001 (AT) joncruz (DOT) org...
| Passero wrote:
| public class Hoofdscherm extends JFrame
| > {
| > private Beheerder deBheerder;
| > private JButton btnMatrixVolledig, btnMatrixVariabelen,
btnMatrixVgl;
|
| > public Hoofdscherm(Beheerder deBeheerder)
| > {
| ...
|
| > setVisible(true);
| > setBounds(0,0,800,600);
| > }
|
|
| There are two problems with that.
|
| 1) You should not set visible from within a constructor.
Why? [ I do it almost invariably.. ]
|
The reason *for* doing it is bascially "Gee, I usually do this when I
make one, so let me just clump the displaying on in the constructor".
That's different than "this is the logical, encapsulated, modular place
for this operation to be".
For an example of a problem with that... what if you find that
constructing an instance of your frame object is slow? One way to fix
this would be to place needed slow construction on a low-priority
background thread, then use things after all needed objects are ready.
However, if the act of constructing an object actually shows it, then a)
it is not thread safe, and b) it can not be constructed in the
background if more than a single thing needs constructing.
Another case would be if you're going to use the instance of your
special frame, but find that you need to tweak a few things on it before
it's ready to display. If just constructing one shows it, then you're
faced with either hiding it again right away (and getting ugly flashing
on the screen) or with it showing up and then changing immediately.
Again, a visually unappealing solution.
There are more reasons, but in general it's due to forcing together
operations that logically don't belong together, even though they often
follow each other in order.
| Quote: |
[ but note that I am also doing work in AWT,
for which Component.show was deprecated in 1.1.. ]
|
Yes. show() for java.awt.Component is deprecated. However,
java.awt.Window (which java.awt.Frame is derived from) overrides show()
with a version that is not deprecated. So for general components/widgets
it has been deprecated, but not for top level windows.
|
|
| Back to top |
|
 |
Tony Morris Guest
|
Posted: Wed Jan 14, 2004 10:42 pm Post subject: Re: Strange problem with a JFrame |
|
|
"You should not set visible from within a constructor"
More specifically, you should not call an overridable (non-static,
non-final, non-private and a member of a non-final class) method from a
constructor.
See "Effective Java Programming", Joshua Bloch.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"Jon A. Cruz" <jon (AT) joncruz (DOT) org> wrote
| Quote: | Andrew Thompson wrote:
"Jon A. Cruz" <jon (AT) joncruz (DOT) org> wrote in message
news:3FF9CBE8.7080001 (AT) joncruz (DOT) org...
| Passero wrote:
| public class Hoofdscherm extends JFrame
| > {
| > private Beheerder deBheerder;
| > private JButton btnMatrixVolledig, btnMatrixVariabelen,
btnMatrixVgl;
|
| > public Hoofdscherm(Beheerder deBeheerder)
| > {
| ...
|
| > setVisible(true);
| > setBounds(0,0,800,600);
| > }
|
|
| There are two problems with that.
|
| 1) You should not set visible from within a constructor.
Why? [ I do it almost invariably.. ]
The reason *for* doing it is bascially "Gee, I usually do this when I
make one, so let me just clump the displaying on in the constructor".
That's different than "this is the logical, encapsulated, modular place
for this operation to be".
For an example of a problem with that... what if you find that
constructing an instance of your frame object is slow? One way to fix
this would be to place needed slow construction on a low-priority
background thread, then use things after all needed objects are ready.
However, if the act of constructing an object actually shows it, then a)
it is not thread safe, and b) it can not be constructed in the
background if more than a single thing needs constructing.
Another case would be if you're going to use the instance of your
special frame, but find that you need to tweak a few things on it before
it's ready to display. If just constructing one shows it, then you're
faced with either hiding it again right away (and getting ugly flashing
on the screen) or with it showing up and then changing immediately.
Again, a visually unappealing solution.
There are more reasons, but in general it's due to forcing together
operations that logically don't belong together, even though they often
follow each other in order.
[ but note that I am also doing work in AWT,
for which Component.show was deprecated in 1.1.. ]
Yes. show() for java.awt.Component is deprecated. However,
java.awt.Window (which java.awt.Frame is derived from) overrides show()
with a version that is not deprecated. So for general components/widgets
it has been deprecated, but not for top level windows.
|
|
|
| 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
|
|