Lazy-initialization enabled CardLayout? - java

I have a bunch of panels put in a CardLayout where nth panel depends on the state changes caused in (n - 1)th panel. Since with CardLayout, you have to initialize and add all panels beforehand. As such it makes it harder than necessary to manage state.
Does Java or some third party open source library provide a variation of CardLayout that initializes its constituent panels lazily i.e. they are initialized just before they are going to be visible?
Edit:
Perhaps I did not state the problem clearly. Let me try again.
I need to set up the panels in CardLayout beforehand, but I do not want it to initialize them until they are to be made visible. This is necessary so that the state changes from previous stages are transparently propagated to next stages.
In my current code, I have:
cardsPanel.add(ReadMePanel.create(this), ReadMePanel.ID);
cardsPanel.add(LicencePanel.create(this), LicencePanel.ID);
cardsPanel.add(InstallationPathPanel.create(this), InstallationPathPanel.ID);
cardsPanel.add(
ExtractionProgressPanel.create(
this,
new NormalizedPath(appContext.getParameter("zipFilePath")),
new NormalizedPath(appContext.getInstallationDirectory().toString())
),
ExtractionProgressPanel.ID
);
Here InstallationPathPanel allows users to select a different installation directory than the default one. ExtractionProgressPanel is supposed to extract a certain zip file to this directory. if ExtractionProgressPanel were lazily initialized, the user selected path would be propagated to it, without me doing anything extra.
Hope the problem is clear now.

You can initialize and add the panel to the CardLayout right before calling the show method, so there is no problem.
EDIT
So on the location where you now call CardLayout#show, you could first do an add and then the show
As it looks like you are trying to create a wizard, the following article might be a good place to start

Related

GUI with multiple frames

I'm looking to build out a Java GUI with a table area and an area that will display the data of a selected row of the table. I've never tried a multi-frame set up before so before I venture to do this I wanted to check with others. Is it difficult to have two frames and have them passing data back and forth? The idea would be that I could move the details frame anywhere I like on the screen or to a second monitor and allow the table to go full-screen if the user wants. Any input or examples are appreciated.
don't to create two of more JFrames use JDialog instead,
reuse this JDialog for another action(s)
create one JFrame and one JDialog for displaying details
have to determine if and which of JTables row(s) is selected
better would be to set ListSelectionMode to the SingleSelection
maybe would be better to invoke (show that already exist) JDialog from JPopupMenu Action
You should have no problem in doing what you are after. You can have public methods in each frame which expose properties and/or structures and you then pass the instance of one JFrame to the other. This should allow you to pass data back and forth.
That being said however, I think that this scenario is valid only when you have one, two, or at most three JFrames. Having a lot of frames calling each other could result a maintenance nightmare.
there are several possibilities to do so:
you can add one of the jframes as a listener to anothe, or both to each other. For this, you have to implement a listener mechanism, like in java.awt. You can pass the information contained in the event objects - this would be the most clean alternative
you can pass the instance of the detailframe directly in the constructor of the main frame and call operations from main frame on detail frame. this is the simplest way, but you will need lot of code changes if you have some new features to add

Container removes component before it made visible

I am working on a big application with lots of components in it. I am trying to add a comboBox at one place and Container is removing that component before it is visible. I read some where that Java Swing validates and removes the component if it is aded somewhere else. How does that exactly works ?
I am sorry but I guess this problem is too abrupt and I am not able to provide any code.
P.S. checked every property of component,panel and container and there is nothing which is affecting the visibility of component.
I never see that Container is removing that component before it is visible, how and what did you debug that, are you sure that JComboBox was added to the expected and visible Container, btw there are lots of threads about How to Add/Remove JComponent(s) on Runtime here is guide from last/recent posts about that,
What you've read is that you can add a component to only one visualized container. If you add the component to multiple containers it will only be visualized in one of them. Other than that, Swing will not randomly remove components from containers, so if the first thing that I mentioned is not your issue, you've got a bug in your program, and we'll need to see code to figure out what it is. Best would be if you could create and post an SSCCE

How to refresh the lwuit form?

I am working in a project in which user fills a questionnaire and the answers are then submitted to the server. For simplicity I have just kept two questions per screen and after clicking on the next command the user gets next two questions. I have used lwuit framework in this project.
To reduce the memory requirements I create form, questLabel1, ansCombo1,questLabel2 and ansCombo2 only once. and their properties are set as per the current frame (screen). The problem is if you are in form 2 and you scroll down to the last option and then you click the next button, since you scrolled down the form doesn't displays the upper components even on the next form tried so many thing. creating a new instance of the form may work but I don't want to use that, for obvious memory reasons,
Any other solution?
thanks in advance,
To make it scroll so that component is visible, check Component/Container API javadocs. I've seen at lwuit page these suggest some methods with semantics that fits - scrollComponentToVisible, scrollRectToVisible. "Makes sure the component is visible in the scroll if this container is scrollable..." stuff like that
// above extracted from comment to an answer to make it more visible
// for the case if some reader has similar problem
Have you tried form.revalidate()?. Because this is useful when you modify the container hierarchy and need to redo the layout.
Update: Use requestFocus(); on first component of next form. Its automatically focused on first (Upper) component.
You can use form.refreshTheme() or form.revalidate() for refreshing your form. If you have made updates on any particular container then do the same for container as well.

How do I add components at run time to a Swing UI created with Netbeans visual editor?

I am currently writing an application where the user has, at some point, to click a button which have been generated at run time. I know how to do it when writing all my swing code from scratch, but I'd like to take advantage of Netbeans' visual editor.
The generated UI code goes into an initComponents() method I can't modify since it is regenerated automatically from the visual form.
I'd like to have a panel I place at design time using the visual editor in which I could add the buttons at run time so that they fit nicely in the layout, but I don't know how to access the panel in a convenient way. Besides, there may be another method than using a panel.
So basically :
How do I locate a Swing component at run time ?
Is there a better way of integrating components created at run time in a generated Swing UI ?
Thanks for your help.
NetBeans-generated GUI classes store all the components in private variables. You can add a method into the generated class that returns the panel, and it will remain even as you do additional design.
If you're going to use a generated UI, then it's probably best to use a JPanel within that UI to "carve out" space for your own components. Otherwise, you'll have to worry about how your components affect the layout of the components placed by the UI.
Just because you are using NetBeans generated GUI classes doesn't mean that you have to use the Group layout for the panels. I find that switching it to a BorderLayout helps especially in cases where I want to add some dynamic user interface code.
It is possible to change private to protected/public by either right clicking on a component in the GUI-Designer, choosing properties and hitting the Source-tab or right clicking on a component and choosing "Modify Source" (or something like that) and setting the appropriate access modifier.
Or just export them via a getXYZComponent() method.
Locating the component should provide as being too difficult, as you built it with the designer and thus know each component.
For example, if you had a JTabbedPane and wanted to add tabs to it when the user hits a button or something like that, you would simply issue myJTabbedPane.add(myCustomComponent); et voila, a new tab appears.
It is also possible to modify the auto-generated code and/or the code used for auto-generation by using the "Modify source" dialog mentioned above, which can be really useful.

Netbeans 6.7.1 mainPanel resizing problem

I've created a small gui app in Netbeans. As I was adding in some buttons and text areas the mainPanel resized itself. Now it is really wide [probably 4x as wide as I want] but when I try to drag the edge in it won't resize back down. If I drag it out, making it bigger, it takes that change. I would just like to return the mainPanel back to a reasonable size. Not sure what I'm doing wrong here. I've tried to change the min size, max size, and preferred size settings for the mainPanel with no success. I've even tried to change the menuBar & statusPanel settings at the same time as the mainPanel [thinking that one of them was making the others too big] without success.
Any ideas?
Netbeans does do really stupid things like that sometimes, and I generally get around them using either of these two methods:
First thing to try is to change the layout used. Try the Grid Bag Layout, or any of the others and see if you get better results.
If that doesn't work, then probably the easiest thing to do is to change stuff in the code. You will notice that Netbeans automatically adds a call to initComponents(); in the constructor (you have to switch to Code view from Design view). And if you look at initComponents, it will have a whole heap of auto-generated code to create the GUI. Do NOT edit this, because it's just a matter of time before Netbeans overwrites your changes. What I do is to create a new method initComponentsFix, and call that immeidtaely after initComponents in the constructor. In initComponentsFix, I would add the code to resize the component to the preferred size, and any other things you you want to fix.
BTW I empathise with you - Netbeans' GUI editor is still in need of much work. However, it's code auto-generation is still very useful, so I wouldn't recommend coding the GUI the good ol' fashioned way. That's why I'm advocating using it up until you start felling its limitations, after which you "take control".
There is also a third way, which I would not recommend, is to edit the file that Netbeans stores the Design view in, which is basically shares the same file name as your frame's class' source code, except with a .form extension.
This file is XML, and is pretty easy to edit. I don't recommend this because it is sorta going around the back door, but as a last resort, you can still try it.

Categories