Link to home
Start Free TrialLog in
Avatar of Archiskulkarni
Archiskulkarni

asked on

Swing Application Development

Hi,
I want to develop a gui to my existing application using swing. I am new to swing. I have designed some menus in the swing. Now i need to change the screen contents to be displayed to the user based on the menu item selected. But i am not able to figure out how it is to be done. Is there any way to change the contentPane on the fly? Please suggest me some solution.. i am stuck here
Avatar of Giant2
Giant2

>on the menu item selected
you must intercept the menuitemSelected. So add a listener and all is done.
Avatar of zzynx
>> Is there any way to change the contentPane on the fly?
Sure.

JPanel mainPanel = new JPanel();
getContentPane().add(mainPanel);


// When the menu is triggered:

public void actionPerformed(ActionEvent evt) {
    // remove everything (that is in from a previous pass)
    mainPanel.removeAll();
    // Add what you need
    mainPanel.add( ... );

    // force a visual refresh
    mainPanel.revalidate();
    mainPanel.repaint();
}
Avatar of Archiskulkarni

ASKER

Hi zzynx,
Thanx for such a prompt reply.. i tried the code suggested by u.. n it's workin.. so thanx again for tht.

I have one more small query.. how do i restrict the size of the components (like button, textfield) on the contentPane.. When i maximize it gets enlarged.
>> I am new to swing.
>> how do i restrict the size of the components (like button, textfield) on the contentPane
I think you should read
http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
>> When i maximize it gets enlarged
That depends on the layout used. The above sounds as if you use a BorderLayout.
When creating a new JPanel() it uses the FlowLayout() by default.
You can change that by

    myPanel.setLayout( new XxxxLayout() );

If - after reading the links above - you have specific questions for your app,
you're welcome to ask them. But maybe then it would be good to post your code
If your going to be changing the layout several times, I think the best option would be to use a CardLayout.
This basically stacks components on top of each other however only 1 component is visible at a time.
Building several JPanels when the class is constructed could save alot of confusion.

Check out this link for some know how and examples.

Cheers :-)
Hi,
Sorry for the naive questions that i am asking.. but i am confused by the way swing works.. I went through those links and examples listed there. In some of those examples also, the size of the components changes if we maximize the window. Now is it inherent problem of swing? do we need to keep the size of the window the same if we need to view the components at the preferred size.

Another point is do we need to use different layouts on the same screen to arrange the components as we desire.. i.e i need to have 2 components in 1 line.. then below that one more component.. and at the bottom 2 components in a line and so on...

thanx in advance
>the size of the components changes if we maximize the window
This depends of the layout you use for that component.
You can use the layout null. In this manner you can specify the position and the size you want for each component.
Or create your video like a chinese box: one panel into another.

I repeat. It depends on what you want to do and how.
See here for a full official tutorial (SUN):
http://java.sun.com/docs/books/tutorial/uiswing/
>> Now is it inherent problem of swing?
That's certainly not a problem! That's just how you want it to work in some cases/layouts
here for a quick tutorial:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/

Hope these could help you.
Bye, Giant.
>> Another point is do we need to use different layouts on the same screen to arrange the components as we desire..
Indeed.
E.g. For one panel you use the BoderLayout.
For a panel in that panel you use a FlowLayout.
For anther panel in that panel you use maybe still another layout.
The combination of all those layouts give you the desired behaviour when resizing.
At first sight this looks complicated, but after some practice you'll get it.
Just start experimenting.
Ok zzynx,
u meant that in a swing application.. we group the components that require one particular layout in a panel.. and then we add these panels to topmost panel.. is it like that?
seems interesting..
you can use only one panel and use the layout (default) that permit you to specify the position and the size for each component.
>and then we add these panels to topmost panel.. is it like that

like I told before:
>You can use ...
>Or create your video like a chinese box: one panel into another.
ok guys,
i think i should now start coding and experiment the things.. if i have some queries(n have plenty of them) i will get back to you gain
thanx for your cooperation.
>>Ok zzynx,
>>u meant that in a swing application.. we group the components that require one particular layout in a panel..
>>and then we add these panels to topmost panel.. is it like that?
Indeed. (And that topmost panel also has a layout)

>> i think i should now start coding and experiment the things.
That's the only way to go indeed.

And once again: if you don't specify a layout for a JPanel it has the FlowLayout by default
Hi,
I want to display a HTML file from this java application.. i tried some freely available browsers but they down't display tables properly. So is there any simple java browser freely available for that?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
???
why accept this last post?
Giant2,
All the posts in this thread hav been very useful for me.. and all my queries regarding swing hav been solved..
As i have already stated, all the posts have equal importance in solving my queries.. the only reason tht i accepted the last post is to close the question..
>All the posts in this thread hav been very useful for me...
>...all the posts have equal importance in solving my queries...
In this situation you could make the choice to accept one answer like accepted and the other like assisted.
And why B grade of accepting?
Thanks for accepting.
But indeed, for the people reading this Q afterwards, it's better to accept the comment(s) that really helped you
ok,
i will take care of it here after..
:)