aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Applet not displaying correctly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Applet not displaying correctly" Watch "Applet not displaying correctly" New topic
Author

Applet not displaying correctly

Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Hi all,

I have coded an applet which accepts name and occupation of use and displays the total characters entered on click of a button.
COde:


If you execute this code,you will notice that controls appears in a way which give a look of an Iron piece struck to an electro magnet in the center. I know it is something to do with constraints of the components, but I cant figure out what. I tried specifying weightx as 0.5 but that seperates the coponents in an ugly way. Can you suggest me how can I make the controls arrangement look pleasant.

Also, you will notice that on Enter button appears immediately below the Occupation label. Whereas accoding my layout arrangement it should leave some row space for the hidden fields.On entering values and pressing enter the hidden fields ,must become visible as per my codes logic. But it is not happening. For some reason it happens once the applet is resized.

Please guide me to fix these logic errors.



Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Let's try moving this to the AWT/Swing forum where there are more regulars to answer AWT questions. This one isn't specifically applet-related anyway.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Your best bet is to use more than one layout manager in nested Panels (or JPanels if this were a Swing app -- which I recommend by the way). You can find the tutorial on how to use layout managers here: http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

Also, when using GridBagLayout, don't forget to use insets and fills. e.g.,
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Thank you so much for posting reply. I thought my post was erased as I could not find it in its original location.
I have read the suns document on Gridbaglayouts and learned a lot. But I have found it difficult to implement the things practically. It will be great if you can share some sources on gridbag layout which show practical examples and explains it in better way.
I have also executed your code and it looks perfect. However I dont understand why does your code's applet has grey background in which button is blue and labels are bold Where as in my case applet has white background, Please tell me what did you do in your code which makes it behave like this?? I could not spot any reason

Also I am posting my modified code in which I have set ipadx,ipady etc as per your suggetion, but still there are layout problems which are:
1. You will notice there is unwanted space is there between the rows, even though I mentioned them to be on consequent rows.
2. Also I dont understand why does the total char row which is hidden initially and is supposed to be displayed on click of enter, does not get displayed on pressing enter. It get displays when I resize the window or remove focus from applet and again gives focus to the applet. It seems to me that the paint process is not happening in this case but I dont understand why? I have made a call to repaint from inside the event handler.



Please help to fix the problem.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41489
    
  53
Suvojit Chakraborty wrote:I thought my post was erased as I could not find it in its original location.

Posts get moved around if there are other forums in which they would be a better fit. You can always find all your posts through the "My Posts" link at the top of the page.


Ping & DNS - my free Android networking tools app
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Suvojit Chakraborty wrote:Thank you so much for posting reply. I thought my post was erased as I could not find it in its original location.
I have read the suns document on Gridbaglayouts and learned a lot. But I have found it difficult to implement the things practically. It will be great if you can share some sources on gridbag layout which show practical examples and explains it in better way.

You're welcome. One helpful tutorial is this one: Taming the GridBagLayout and others can be found through Google.

I have also executed your code and it looks perfect. However I dont understand why does your code's applet has grey background in which button is blue and labels are bold Where as in my case applet has white background, Please tell me what did you do in your code which makes it behave like this?? I could not spot any reason

Probably this is due to my applet being a JApplet, and thus a Swing GUI application rather than an Applet which is built with the older, less flexible and less robust AWT graphics library.

Also I am posting my modified code in which I have set ipadx,ipady etc as per your suggetion, but still there are layout problems which are:
1. You will notice there is unwanted space is there between the rows, even though I mentioned them to be on consequent rows.
2. Also I dont understand why does the total char row which is hidden initially and is supposed to be displayed on click of enter, does not get displayed on pressing enter. It get displays when I resize the window or remove focus from applet and again gives focus to the applet. It seems to me that the paint process is not happening in this case but I dont understand why? I have made a call to repaint from inside the event handler.

1. Some of your GridBagConstraint values seem randomly created. You'll want to read all tutorials you can find to help straighten you out on this.
2. I'm no expert in AWT but perhaps calling revalidate() in your action listener after you've called setVisible(true) but before you repaint you can get the layout managers to relayout components.
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Hello Pete,

Thank you for your support. I did some more researchwork on layout and I am now in a better position. I fixed my code and it is ok now. I have understood weightx,weighty now. And I also realized that weightx,weighty is aint important as long as we dont strech applet area.

revalidate() works with swing. However I found that validate() can solve my problem.

My code now


I also think layouts can be mastered with more and more practical work. I will continue to do experiments with different components og GridBagConstraints to gain better command of the subject. I hope you will help in my silly doubts( if any)

if ever my post if being viewed by someone like me, then I would like to ask them to go through sun's tutorial first throughly and then refer this: this

Thanks again
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Applet not displaying correctly