Problem adding/removing items to already visible components

S

steve_marjoribanks

I am having trouble figuring out the best solution to a problem. Seeing
as I am a beginner and this is my first application, I'm not yet quite
in the mindset of a programmer and so I'm struggling to visualize the
best solution so I was hoping you could help me :)

The overall purpose of the application is to load data from an XML file
and then represent it graphically on one JPanel, and display a JTree
representation of the XML file in another JPanel. All the most
important sections of code appear to work, ie. it can open a file,
validate the XML, display the XML tree and draw the required pictorial
representation, but I am having some trouble working out how to remove
the minor 'display bugs'.

When run, the application opens up a 'main' window (JFrame) which
should have no initial visible content other than the menubar and
toolbar. This has a menu and toolbar etc. from which a File Chooser is
loaded when the user selects 'open'. On opening the XML file it is
validated and then a JTabbedPane with 2 tabs should be displayed
filling all the empty area in the main JFrame. One of these tabs
displays the pictorial representation of the data and the other the
JTree.

What I am having trouble working out is how to sructure my code so that
the JTabbedPane only appears on opening a file and is then disposed of
upon closing the file (ie the file is closed but the application remins
running so another file may be opened).

I considered changing the code to use a JDesktopPane instead of the
main JFrame and then opening up the file in an internal frame (the
ability to open multiple files like this within the same application
was a possible extension to my project) but I read in Sun's Swing
tutorial that this can be a non-trivial task.
I also though, would it be possible just to add the JTabbedPane on
loading the application and just use the setVisible method to make it
visible when a file is opened but then I thought that this would create
problems when closing the file as you can't just remove a component and
then add one again when the application is still running.

Any thoughts from people more experienced than me (thats's most of you,
lol) would be greatly appreciated!

Many thanks,
Steve
 
T

Thomas Weidenfeller

What I am having trouble working out is how to sructure my code so that
the JTabbedPane only appears on opening a file and is then disposed of
upon closing the file (ie the file is closed but the application remins
running so another file may be opened).

This is not what one typically does. It would be more typical to clean
out the contents in the different panes of the tabbed pane, and then
disable (grey-out) things by using setEnabled(false).

/Thomas
 
S

steve_marjoribanks

This is not what one typically does. It would be more typical to clean
out the contents in the different panes of the tabbed pane, and then
disable (grey-out) things by using setEnabled(false).

This is what I had planned to do orginally, but I had a look at the API
and couldn't find a method available for use on a JPanel which clears
out content other than remove(). I didn't think I could use 'remove'
because if I then want to open another file after the original one has
been closed, how can I get the new data to be displayed? I thought that
components could only be added to containers before the container is
displayed? What I am saying is that if I remove the scroll pane with
the JTree in it (as mentioned in the post above) upon closing the first
file, I didn't think it was then possible to add a new scroll pane and
JTree upon opening a new file if the aplpication is still running and
the GUI is visible?
Have I misunderstood the rules for adding components?

Steve
 
S

steve_marjoribanks

After much searching I have come to the conclusion that I must use the
validate() method followed by a repaint() after adding/removing
components from the tabbed panes!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top