Solved

Swing Application Development

Posted on 2004-10-08
27
190 Views
Last Modified: 2013-11-23
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
Comment
Question by:Archiskulkarni
  • 10
  • 8
  • 7
  • +1
27 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 12257414
>on the menu item selected
you must intercept the menuitemSelected. So add a listener and all is done.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12257503
>> 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
 

Author Comment

by:Archiskulkarni
ID: 12257651
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12257722
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12257758
>> 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
 
LVL 9

Expert Comment

by:OBCT
ID: 12257928
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
 
LVL 9

Expert Comment

by:OBCT
ID: 12257932
0
 

Author Comment

by:Archiskulkarni
ID: 12258339
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12258367
>the size of the components changes if we maximize the window
This depends of the layout you use for that component.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12258393
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12258394
>> 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
 
LVL 12

Expert Comment

by:Giant2
ID: 12258399
here for a quick tutorial:
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/

Hope these could help you.
Bye, Giant.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12258425
>> 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 37

Expert Comment

by:zzynx
ID: 12258451
At first sight this looks complicated, but after some practice you'll get it.
Just start experimenting.
0
 

Author Comment

by:Archiskulkarni
ID: 12258525
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12258552
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12258582
>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
 

Author Comment

by:Archiskulkarni
ID: 12258604
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12258694
>>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
 

Author Comment

by:Archiskulkarni
ID: 12286793
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
 
LVL 37

Accepted Solution

by:
zzynx earned 90 total points
ID: 12286913
You'd better close this one (if solved for you) and ask that in another question
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12295059
???
why accept this last post?
0
 

Author Comment

by:Archiskulkarni
ID: 12295079
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12295100
>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
 
LVL 37

Expert Comment

by:zzynx
ID: 12295193
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
 

Author Comment

by:Archiskulkarni
ID: 12295217
ok,
i will take care of it here after..
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12295251
:)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now