AWT stuff is not reliable

Hi,
Im using AWT buttons and checkboxes in an applet. Often the AWT components do not show, even though they are set on visible. I tried everything including moving them around, enabling them (setEnable()), repainting them in different places… Nothing works. >:(

Alex

Care to post some code?

I wish i could, its over 1000 lines. I couldnt replicate it on a small scale. I mean there is gotta be something buggy about Buttons and other stuff just because their positioning and resizing has to be done in run() method. So if i put this in init() method:


cb1=new Checkbox("Checkbox1");
cb2=new Checkbox("Checkbox2");		
cb3=new Checkbox("Checkbox3");
b1=new Button("Button1");
b2=new Button("Button2");
b3=new Button("Button3");
add(cb1);add(cb2);add(cb3);
add(b1);add(b2);add(b3);
cb1.setLocation(20,20);            // has no effect
cb2.setLocation(20,40);// has no effect
cb3.setLocation(20,60);// has no effect
b1.setLocation(100,20);		b1.setSize(80,20);// has no effect
b2.setLocation(100,50);		b2.setSize(80,20);// has no effect
b3.setLocation(100,80);		b3.setSize(80,20);// has no effect

It wont do what it is supposed to do. But ok, i position/resize them in run() method and it works fine, but making them show up when i want to is beyond me.
Another example:
I load the applet in IE first time, in the code i resize and reposition 2 buttons, they show up in wrong places. So i click the first button, this should make 4 checkboxes appear, they do.
The second time i load it in IE, the 2 buttons are in the right positions and of the right sizes. So i click the first button, and the 4 checkboxes do not show. I guess its like drawing the same picture twice, they’ll always be different.
I woulda created my own buttons and checkboxes only i think i shouldnt have to.

try setBounds() instead of setLocation()

Setting location and size has no effect like you noticed, when there is a layoutmanager active (which it is by default)

You might want to go through the Java Tutorial on AWT (or even Swing)

[quote]You might want to go through the Java Tutorial on AWT (or even Swing)
[/quote]
Well, i dont wanna waste time, designing buttons and checkboxes from ground up is much faster and more objective.
Anyways, i just wanted to see if other people had those problems and what they used to deal with them. I see that not many people are using AWT in applets, for obvious reasons… :-\

Because reinventing the wheel is just efficient.

Before swing came along everyone used AWT and some complained about it’s speed mostly. These days we use swing and ppl complain about new things. I’m afraid your the first with these complains.

fixed layouts don’t handle differend resolutions or dpi,

Anyways try looking at the javadoc, or if that is pulling in too much information at once read the Java tutorial. Stuff 99% of the time does what it said, and is reliable it that way. If you don’t read the the javadoc and gues away at things then it probebly doesn’t do what you expect. And yes I gues in that way it isn’t reliable. ::slight_smile:

perhaps I’m harsh :-\

The obvious reason is that you are using it incorrectly. You spend more time trying to work around things or just create your own buttons than you would if you just read The Java Tutorial. People ask these questions all the time and most of the people that have the answers have read The Java Tutorial.

With the code you posted the first thing I can think of this that you didn’t do setLayout(null) on your container.

If your code is that long, post it somewhere in a zip file and place a link here so we can download it and see what’s wrong.

[quote=“alexxz4,post:6,topic:31439”]
I agree! Layout managers are useless. Try setLayoutManager(null)

Layout managers have their purpose. Think of dynamic texts in labels and buttons due to internationalization. Also keep in mind, that there are different DPI settings for different users, so the texts might overflow your hand designed button sizes on highres notebooks etc.

[quote]Well, i dont wanna waste time, designing buttons and checkboxes from ground up is much faster and more objective.
[/quote]
So you’d rather waste my time? :stuck_out_tongue: :-*
I don’t mean to sound harsh, but learning how stuff works first (instead of complaining that AWT buttons are buggy when you don’t understand why they don’t behave like you expect) is never a waste of time if you ask me… Understanding that you don’t understand something is a good start.

Anyway, if you keep having problems, post or upload your code somewhere.

[quote]Layout managers are useless. Try setLayoutManager(null)
[/quote]
I never use setLayout(null). Once you get the hang of LayoutManagers, they make good GUI design a lot easier (IMHO) and prevent numerous problems (as cylab pointed out).
Using setLayout(null) might work for trivial cases, but I’d rather be on the safe side.

I agree 100%. I have never had a problem making layout managers do what I want them to do.

[quote]I don’t mean to sound harsh, but learning how stuff works first (instead of complaining that AWT buttons are buggy when you don’t understand why they don’t behave like you expect) is never a waste of time if you ask me… Understanding that you don’t understand something is a good start.
[/quote]
I dont mean to sound harsh, but its very easy to say “Maybe you should read the tutorial”. I could say that all the time even if i didnt know a thing.

Ok, if im drawing this string on the screen:

g.drawString(""+checkbox.isVisible(),400,20);
and it draws
true
but the checkbox is not on the screen (and its location is within the viewing area), what else am i supposed to do?
I could waste my time trying all the different things to make it appear or read the (300 page) swing tutorial, to find out after all that that it didnt help me, i would be forced to reinvent the wheel anyways. So thats what i done, i created my own checkboxes and buttons; works like butter.

[quote]Because reinventing the wheel is just efficient.
[/quote]
Yeah, i agree, more efficient than reading a tutorial. ::slight_smile:

You are sounding harsh, because you come here for help and the people are trying to help and your just blowing off their answers. You are also in a community where people want to help each other. We’re not going to just throw out lame advice for the hell of it. We’re speaking from experience.

How about listening to advice? Show more code so we can figure out where you are going wrong.

Yeah works like butter now, but when you want to have a new feature you’ll spend countless hours adding and debugging each feature. That time will grow bigger and bigger as you add more features. Swing and AWT have the combined experience of the Java community and developers over the past 12 years debugging all the features that already exist.

OK, here is my code (with all the files and classes).
http://www.4shared.com/file/39225173/9db16f56/Game.html
I dont want you to get wild with it, just press ‘Quiz’ button when it loads.
On line 468 i draw some stuff about ‘submit’ button. As u see, its visible yet its not showing. Im not even complaining about ‘Quiz’ and ‘Atlas’ buttons on the start page in convoluted places, at least THEY ARE THERE (I set their positions and sizes in positionAWT() function, line 1030. For some reason java decided to position all the buttons in correct places except those 2 ???). The four checkboxes are not there also.
Sorry for putting all that coding monstrosity here. Its just that it functions fine if the program is smaller, i noticed.

ok missing 3 classes, Info, InfoStorage, CityInfo, so I can’t test.

also what a monster class, I can dump 100 best practices on you right now but’ll start with stuff which has a direct effect.

try changing:


public void show_popup(String msg) {
	pop_up = new Frame("GeoQuiz");
	popup_label = new Label(msg);
	b_ok = new Button("OK");
	b_ok.setSize(50, 30);
	b_ok.setLocation(85, 35);
	pop_up.add(b_ok);
	pop_up.add(popup_label);
	pop_up.setSize(200, 100);
	pop_up.setLocation(700, 220);
	pop_up.setVisible(true);
	b_ok.addActionListener(this);
	pop_up.addWindowListener(this);
}

with:

http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
//custom title, no icon
JOptionPane.showMessageDialog(frame,
“Eggs are not supposed to be green.”,
“A plain message”,
JOptionPane.PLAIN_MESSAGE);


public void show_popup(String msg) {
	JOptionPane.showMessageDialog(this,  msg, "Info", JOptionPane.PLAIN_MESSAGE);
}

As for the layout issue if your gonna specify sizes and places you should adleast setLayout(null);

On a other note study has shown that in general users don’t like components popping up out of no where/disappearing. Disable is preferred.

Yes, you can fix your bug by adding setLayout(null); as the first line of your init() method, and call your positionAWT() method at the end of your init().
(I called it just before you called restart(). Not knowing how the rest of your code works, this kind of made sense :)).
The buttons at the start screen at least now show up where you expected. I didn’t test anything else but I suppose it’ll solve your other layout problems as well.

Riven already suggested this in reply #4

[quote]ok missing 3 classes, Info, InfoStorage, CityInfo, so I can’t test.
[/quote]
The classes are there as class files, just add them to your classpath

yeah I noticed. :stuck_out_tongue:

[quote]also what a monster class, I can dump 100 best practices on you right now but’ll start with stuff which has a direct effect.
[/quote]
As u probably noticed, this is not a complete project. I prefer “coding it out” before dealing with OOP logistics.
The pop-up window is custom made if i need to add more stuff to it. Plus, im not sure if applets support JOptionPanes (i know JApplets probably do but JApplets flicker and mess up my drawing stuff).

[quote]I called it just before you called restart(). Not knowing how the rest of your code works, this kind of made sense Smiley).
The buttons at the start screen at least now show up where you expected. I didn’t test anything else but I suppose it’ll solve your other layout problems as well.
[/quote]
Yeah, it solved it, but the 4 checkboxes and the submit button are still not showing in the ‘quiz’ mode.
Here is how it looks with AWT buttons and checkboxes:
http://img146.imageshack.us/img146/4814/img1dd2.png
And this is what its supposed to look like (using my own Checkboxes and Buttons):
http://img150.imageshack.us/img150/2656/img2no7.png

You need to use double buffering to stop flickering.