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
Related
I use XMonad+gnome as window Manager. I have the problem that, when I run a Java Web Start applicacion, It olways show an empty window, like the screen:
Is there something I can do to fix it?
UPDATE:
I tried simple programs from JWS Examples and it works, right. So it should be a problem of the application.
The failed aaplication is Blast2Go.
Change the name of the window manager to a known one like LG3D:
startupHook = setWMName "LG3D"
I had the exact same problem with my own Java code and using the other suggested solutions (setWMName, MToolkit, etc.) did not solve the problem with Xmonad. I must note that in other window managers (e.g. fluxbox), the app works as expected. In my case, however, I figured out the problem. If you have access to the source of the Java app, I suggest you consider the following:
If you have any JFrame or JPanel or other containers, you should explicitly define their layouts. If you set the layout to null, then the dimensions of the container must be set explicitly. Otherwise, the container won't be rendered at all. For instance, I have a JPanel that contains all my widgets. I'd rather have full control over the layout, so I set the layout to null, and then explicitly set the dimensions for the JPanel:
jContentPane.setLayout(null);
jContentPane.setPreferredSize(new Dimension(appletWidth,appletHeight+100));
Hope this helps.
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
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.
i'm new to Java programming, but I've done C++ for a while now. the whole GUI thing is new to me. i created two JPanels and added them to JFrame using FlowLayout. When I run the program, i get http://imageshack.us/photo/my-images/43/36213853.jpg/ as opposed to http://imageshack.us/photo/my-images/88/86682510.jpg/ only SOME of the times. the other times, the content appears just fine
for some reason, when i resize the window when the window is blank, the contents appear fine.
i'm not sure what the problem is that gives me this inconsistency!
any help would be appreciated. thanks!
It's hard to answer without code, but by chance are you calling setVisible(true) before packing your GUI and before adding components to the GUI? Do the components show up if you resize your GUI?
If so, you'll want to make this call only after your GUI has been fully populated with components, and usually after it has been packed (so that your layout managers can lay out all components and size the GUI correctly).
If this advice doesn't help, then you'll likely need to post some of the offending code. How much to post -- it's hard to say -- enough so that we can identify the problem ;) but not to much as to be drowned in code not related to your problem. The best code to post is an SSCCE if you have the ability to create one.
I'm trying to internationalise a Java applet and with that, support scripts which are written from right to left. I want to set up component orientations for all java components added to the view automatically.
My solution so far has to listen to all AWTEvent's using the windows mask:
c.getToolkit().addAWTEventListener(listener, AWTEvent.WINDOW_EVENT_MASK);
...and then setting the c/o on each window added, as well as adding component listeners to set c/o on any components added to the window at a later point.
My issue is that JInternalFrames are not handled by this solution, I want to be able to add another listener for these events, much like I have done for windows. Any ideas?
Or alternatively, are there any better approaches to handling script direction for all components in an applet?
Add a ContainerListener to the JDesktopPane. As a component is added to the desktop you can change its orientation.
Do you have a handle on all those JInternalFrames? If so, try the internal frame listener.
http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameListener.html
It notes that it's the analogue to the AWT WindowListener.
AWTEventListener on the current Toolkit will only give you events coming from the toolkit. Generally events generated by lightweight components will have been caused by mouse or key events.
Asking for all of something in a process is usually a very bad sign. A low-level piece of code is making policy for the whole program. A much better approach is to add listeners near to where you create the component, before it is "realised". This is repeated code, but then you probably already have repeated code. So factor out into a method. Then you have only one place to update, unless you have any cases where it doesn't apply which would have broken the global approach.