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.

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

cb1=newCheckbox("Checkbox1");cb2=newCheckbox("Checkbox2"); cb3=newCheckbox("Checkbox3");b1=newButton("Button1");b2=newButton("Button2");b3=newButton("Button3");add(cb1);add(cb2);add(cb3);add(b1);add(b2);add(b3);cb1.setLocation(20,20); // has no effectcb2.setLocation(20,40);// has no effectcb3.setLocation(20,60);// has no effectb1.setLocation(100,20); b1.setSize(80,20);// has no effectb2.setLocation(100,50); b2.setSize(80,20);// has no effectb3.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.

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...

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...

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.

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...

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.

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.

Well, i dont wanna waste time, designing buttons and checkboxes from ground up is much faster and more objective.

So you'd rather waste my time? 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)

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 *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.

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.

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> truebut 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.

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.

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.

Ok, if im drawing this string on the screen:> g.drawString(""+checkbox.isVisible(),400,20);and it draws> truebut the checkbox is not on the screen (and its location is within the viewing area), what else am i supposed to do?

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

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.

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.htmlI 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.

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.

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.

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.

Hmmm... i thought what i had in update() method was supposed to take care of double-buffering. Anyways, its not really important, im not gonna use optionpanes in this project.Ok, to elaborate on some of my code, here is a piece of it from actionPerformed(ActionEvent e):

1 2 3 4 5 6 7 8 9 10 11

elseif(e.getSource()==b_quiz){function=3; // this sets it to quiz modemap_index=-1;b_atlas.setVisible(false); b_quiz.setVisible(false); // this hides quiz and atlas buttons so that they dont showloadAsia(); // this loads all the graphical stuff and flags of the continentquestion_mode=1; // this mode is multiple choice question newQuestion(question_mode); // this generates new questionhideAWT(false); // this shows all the components on the screenpositionAWT(); // this positions and resizes buttons and checkboxesshowComponents(question_mode); // this hides some of the components that are not needed. }

This is a piece of code from showComponents() that sets visible those components that need to be shown. Notice that if mode=1, then the 4 checkboxes should be visible.

Maybe those buttons that are named "submit" are never shown by java (same goes for the checkboxes). But then again, i tried renaming them and still doesnt work, so its not it. Maybe there is a limit on how many components you can have and maybe i have too many... If someone would just tell me that AWT stuff is very buggy, i will use my own components cause i wont have no other choice...

if you change the UI you may be asked to call for a repaint() of the whole contents. Swing or AWT, when a child component is modified the repaintManager is not necessarily notified, depending on the implementation of the components.e.g. for an Animation component I built myself :

1 2 3 4 5 6 7 8

/*the player is a Swing Timer that validates the new animation frame index and THEN REPAINTS THE PARENT IF EXISTING */newActionListener() { publicvoidactionPerformed(ActionEvente) {validate(); // animation time ms is updated and the frame is loaded and made visible, in some way.if(Animation.this.getParent() instanceofContainer)Animation.this.getParent().repaint();elserepaint();}}

This scheme must work for all Component or JComponent implementations.

Us writing down the/any solution won't get you anywhere. When the next problem occurs, you'll find yourself running back to us.

One of the problems you found yourself into, is that your problem grew too big, and thus cannot be easily fixed.

When you find yourself running into any coding problem, narrow it down until you understand what is happening. At this point, it's easy to fix the bugs.

In your case, programming is not just knowing the language, it's about knowing the APIs and proper coding styles. You have to understand what your code does, and how it handles the APIs, because when you don't, you'll never know whether your code, or the API caused that bug you're seeing. It's pretty safe to assume the bug is in your code most of the time, though. When in doubt, insert lots of System.out's, to see what state your application is in, as your assumptions can be very wrong.

To be a bit more specific: either Google your way out, read a book about AWT, or keep finding yourself getting stuck all the time. Build test-cases of a few dozen lines, where you test functionality (like: what happens when I remove a button from a panel, should I repaint/invalidate/revalidate?)

Good luck!

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

In your case, programming is not just knowing the language, it's about knowing the APIs and proper coding styles. You have to understand what your code does, and how it handles the APIs, because when you don't, you'll never know whether your code, or the API caused that bug you're seeing. It's pretty safe to assume the bug is in your code most of the time, though. When in doubt, insert lots of System.out's, to see what state your application is in, as your assumptions can be very wrong.

Its not a problem its a java bug. Ive seen a share of them (like the same program starting out in different states, etc.) Usually im able to work around those bugs, but not this one. I would never touch AWT if this project was only for myself, but im "supposed to" use java API.I have a question then (i asked it before): How could a component not be visible when its state is set on visible? If there are some ambiguous functions (like setEnabledVisibility() or setVisibleWhenSettingVisible() or showComponentOnTheScreen()) that control visibility, its an API flaw, not mine.

OMG, i shouldve known it. When setting location of a component, you need to set its size too, otherwise it would not show. So all components, including checkboxes need to have their size set (setting a size of a checkbox has no effect by default, but u need to set its size when u change its position). That makes hole lotta sence, no brainer. Works out pretty nice when you need to change a button's text, you would have to guess the buttons required length or use FontMetrics class. I love java OMG.Now i know some of the experts here knew this and yet didnt tell me.

How about READ THE TUTORIAL. You are not using AWT properly and this is causing ALL of your problems. This is not a bug in Java. The first sign of an inexperience programmer is one who blames other code before their own. If you learn how to use AWT properly your problems will disappear. You spend all this time trying to figure out what is wrong when you could have been half way through the tutorial by now.

You spend all this time trying to figure out what is wrong when you could have been half way through the tutorial by now.

Well, considering that stuff about positioning is at the end, i saved some time.Still, normal assumption is that if you want to change position of a button only (and keep its size to default) you would only need to use setLocation() without using setBounds() and changing its size. The same goes for checkboxes, i dont see any reason why anyone would change its size. Lets use common sense, people. If everyone referred to java tutorial without making assumptions we would not get anywhere. Anyways, its easy to blame it all on me since i was the one who found the answer. OMG.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org