Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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
0
Archiskulkarni
Asked:
Archiskulkarni
  • 10
  • 8
  • 7
  • +1
1 Solution
 
Giant2Commented:
>on the menu item selected
you must intercept the menuitemSelected. So add a listener and all is done.
0
 
zzynxSoftware engineerCommented:
>> 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();
}
0
 
ArchiskulkarniAuthor Commented:
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
zzynxSoftware engineerCommented:
>> 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
0
 
zzynxSoftware engineerCommented:
>> 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
0
 
OBCTCommented:
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 :-)
0
 
OBCTCommented:
0
 
ArchiskulkarniAuthor Commented:
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
0
 
Giant2Commented:
>the size of the components changes if we maximize the window
This depends of the layout you use for that component.
0
 
Giant2Commented:
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/
0
 
zzynxSoftware engineerCommented:
>> 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
0
 
Giant2Commented:
here for a quick tutorial:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/

Hope these could help you.
Bye, Giant.
0
 
zzynxSoftware engineerCommented:
>> 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.
0
 
zzynxSoftware engineerCommented:
At first sight this looks complicated, but after some practice you'll get it.
Just start experimenting.
0
 
ArchiskulkarniAuthor Commented:
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..
0
 
Giant2Commented:
you can use only one panel and use the layout (default) that permit you to specify the position and the size for each component.
0
 
Giant2Commented:
>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.
0
 
ArchiskulkarniAuthor Commented:
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.
0
 
zzynxSoftware engineerCommented:
>>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
0
 
ArchiskulkarniAuthor Commented:
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?
0
 
zzynxSoftware engineerCommented:
You'd better close this one (if solved for you) and ask that in another question
0
 
Giant2Commented:
???
why accept this last post?
0
 
ArchiskulkarniAuthor Commented:
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..
0
 
Giant2Commented:
>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?
0
 
zzynxSoftware engineerCommented:
Thanks for accepting.
But indeed, for the people reading this Q afterwards, it's better to accept the comment(s) that really helped you
0
 
ArchiskulkarniAuthor Commented:
ok,
i will take care of it here after..
0
 
zzynxSoftware engineerCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now