How to refresh the lwuit form? - java

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.

Related

Retrieve Values from multiple JFrame to a single JFrame

I have 5 JFrames in my application and I want the values from all 5 JFrames to be sent to a single JFrame. And it is a process where I have to go one frame to another and the value entered previously should not be lost and must be visible at the end of the process.
Easy example is,
I key in my name in the first frame,
then I key in my Address in the second frame,
then my mobile number in the third frame
and so on till the last frame where I want my keyed in details in the previous forms to be in the final frame to display my data in JTextfields. Is this possible? Because if it is a single form, I know how to do it. But when it is multiple forms in this situation I am lost. Please help.
This has nothing to do with Swing or JFrames and all to do with the general issue of getting information from one object into another. Yes it's possible -- give the classes that you wish to extract information from "getter" methods, and then call them when you want the information. If you want to gather this information in an event-dependent fashion, then you will need to have one class listening for state changes brought on by events in the other classes. A PropertyChangeListener can work well for this.
Or if you use modal JDialog windows instead of JFrames, you will always be notified when the dialog has returned and is no longer visible, since the calling code's program flow resumes from right after where it told the dialog to become visible.
Next we can discuss whether having 5 separate JFrames is a good idea or not. I'm guessing you know my opinion on this, else I wouldn't have mentioned the subject.

Java Swing: flyweight vs new windows

I'm developing a new application where I'll have some windows opened at the same time.
I'm currently trying to design the GUI and I'm struggling with two choices:
I could use a side navigation panel and using the center of the page to display the content of each panel. These panels would be stored according flyweight pattern and I would just hide/show them when navigation buttons are clicked (in order to save the content as is was when hidden, for example a user registration form).
I could use a front page displaying the menu all over it and use popups/new windows to show the content. These could be closed/minimized etc).
My problem is: What if all the panels are stored in my flyweight pattern? will it have a huge performance hit or will it still run smoothly with like 15 JPanels stored? (of course those JPanels will have sometimes lots of content in it such as forms etc).
What do you think would be the best easy-to-use/performance choice ?
Thank you :)
JTable rendering already uses the flyweight pattern, so a one column table is ideal for selection. A custom renderer can display an arbitrary thumbnail representation, while a ListSelectionListener can display arbitrary detail in an adjacent container. In the TableModel, consider an LRU cache if the individual data records are consuming too much memory.
As always with performance questions, prototype and profile.
As long as you don't attempt to hold more data than fits the heap reasonably, performance will be a non-issue nowadays (unless you do something exceptionally bad you will not notice any performance difference from the user perspective).
That said, unless you have pressing reason to hold on to GUI's you currently don't need - just let them get GC'd and recreate them as needed. The create-throw-away after one use approach is more flexible when the application needs to be modified and bears less opportunities for memory leaks.
As for the GUI design aspect, many people absolutely hate popups. They can also interfere with focus management/keyboard usage. But it still depends on which kind of control flow you need. Side menu bar is fine for many purposes.
I'd like to point out that the side menu is just a fancy reinvention of Tabbed Pane (which is a standard component you wouldn't need to implement yourself). Also, if things need to be done in a specific order - a Wizard like approach can also be a good choice (one Window that changes contents with each step completed).

Lazy-initialization enabled CardLayout?

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

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

In wicket, how can you refresh a feedbackpanel from an onSubmit of an ajax component within a panel?

I have a feedback panel at the top of my page.
I have a number of panels, each have sub panels with AjaxFallback links that, when clicked I want to set info() messages to be displayed in the feedback panel.
Do I have to pass my feedback panel down through the layers of panels in order that I can re-add it to the Ajax target when the link is clicked? Or is there another way?
Thanks, Tom
The following works in Wicket 6
target.addChildren(getPage(), FeedbackPanel.class);
Something has to add it to the target, but there are ways of using indirection so that things don't need to be passed around as much. See this blog article for one tactic.
Something similar is being added in Wicket 1.5. See WICKET-1312 for bug-tracking about this addition.

Categories