I'm looking for a method that I could override or some listener that is fired when a JPanel is being layouted. Checked the javadoc but couldn't find one. The situation is this:
I have a component that takes care about its contents via a Null-Layout.
The component itself is however layouted inside another JPanel (with a layout manager) and receives a certain size when layouting is done.
Now I want to use this size to calculate widths and heights of the contents of that Null-Layout JPanel.
But of course I need to know when I have to recalculate sizes. Any ideas or a good alternative approach? The required calculating in the Null-Layout is actually very simple and using a layout manager would probably require more work than the current solution - I just need to find that method or listener.
Answering myself so this can be closed.
doLayout will be called when layouting is needed, so that's the method one can override when doing layouts without a layout manager.
Related
I'm making an application in which JLabels are created and can be re-positioned using
.setLocation(x,y)
However, whenever I invoke
.setVisible(true)
the JLabels move back to their original position. I think it has something to do with
.setVisible()'s --- revalidate() function
But I am unsure of how to override or avoid revalidate() effects.
Thanks,
You don't override or change revalidate() -- rather you use the appropriate layout manager since all revalidate does is to tell the layout managers to layout the components held by this and all nested containers. One "solution" is to use a null layout, but I strongly advise against that as this will lead to hard to debug and maintain GUI's, ones that might look good on one platform and terrible on all others. Consider nesting JPanels, each using its own layout manager, and using Borders, such as an EmptyBorder, to achieve your desired GUI layout.
If your program (not the user) is setting the location of the components then don't do this. Instead use the appropriate layout manager, or combination of nested panels with different layout managers to achieve your layout.
Read the Swing tutorial on Layout Managers for more information and working examples.
JLabels are created and can be re-positioned using setLocation(x,y)
However, if you are creating an application that allows the users to drag components around the screen then you need to use a null layout so you can control the location (and the size) of each component.
However, I would recommend you take at look at Drag Layout. This is a layout manager that implements most of the layout manager functionality, but will allow you to control the location of the component.
However, whenever I invoke setVisible(true)
Also, Swing components are visible by default so there is no need to invoke that method, except on top level containers (JFrame, JDialog etc).
Trying to figure out if it's possible and difficultly level around the below problem as depending on such might consider other alternatives...
If I have a overall JFrame framework, can I construct various different JPanels with their associated components and actions then say pass these JPanels as args depending on user interaction so the inside of the overall JFrame/JPanel changes. I'm assuming there must be some implementation that achieves this, but having trouble find the answer.. For example I construct a JPanel, which has border layout, and the centre position will change different JPanels depending on what a user does etc.. I thought it would be a simple as create a JPanel, then passing it to a method which calls the overall Jpanel add(component, borderlayout.center) method which would change what is shown, but doesn't work like that and assumed that must only work for constructor when GUI is first constructed..
Sorry for the length, but if someone can point me in the right direction i'd be appreciated...
Removing and adding components does work as expected. You need to call revalidate() on the parent component once it's done, though.
If that doesn't work, post an SSCCE exhibiting the problem.
I'm building a GUI application, and within a JFrame i have 2 jcombobox's and a JPanel to view certain data. Now when i call the pack() methode in the main class it puts the two jcombobox'es next to my JPanel, which i dont want, because I want them North. Ofcourse I've tried to hard-code it in my code, but it doesn't work after I've called the pack() method.
Are there any alternatives to this method?
Only one component can be NORTH, so if you want both ComboBoxes to be NORTH you have to add them into a separate container. This separate container can then be put NORTH.
(Post the source for more exact help.)
All pack does is resize the Window (in this case JFrame) to its preferred size and the preferred sizes of its sub-components. To control the actual location of the sub-components relative to one another you need to use an appropriate LayoutManager.
You might want to check out the Using Layout Managers tutorial.
The pack() method just causes the layouting to happen, it has abolutely nothing to do with what is put where.
Most likely you're not using layout managers correctly. Show us your code and we can tell you waht exactly you're doing wrong.
You can avoid using pack by explicitly setting the frame size with setSize and setBounds. However, using pack is usually the preferred way as it leaves the frame layout manager in charge of the frame size.
That being said, the problem you are describing appears to be related to the correct use of a layout manager rather than the sizing of the frame. Have a look at the various layout managers for Swing and how to use them: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html.
I have some JButtons in a JFrame. The layout of JFrame is null.
I have given absolute position to button. But when I minimize or maximize frame, the postion of the buttons is getting disturbed how can i set it relative to JFrame dynamically?
Write your own layout. Otherwise, you'll be doing the same thing, just with a mess of listener classes.
Use a layout manager. This is the kind of issue that they are designed to handle automatically.
As others have said, writing your own absolute layout manager is the best way to handle this. Alternatively, you could look at Explicit Layout to see if it meets your needs. There are also references on the web to an AbsoluteLayout class that either does or used to come with Netbeans. Perhaps you could track that down and use it.
Another option from the Java Tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html
I know that in a project I was working on, we needed to do absolute positioning, so we actually made our own Layout (which we called an absolute layout) and passed in constraints to it (which we called the absolute constraints).
Edit: As noted by others, I would recommend using a layout manager unless there is a good reason for absolute positioning. GridBagLayout is a commonly used one, and I have found it to be the best as long as you follow the standards. The java tutorial for it is here:
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
I want to create a custom JComponent (specifically a custom JToggleButton) that has a custom appearance. What i want to do is simply override the default painting of the component and draw something of my own (an image for instance)
This is NOT a question on how to do that (I am fairly proficient with Java2D). What i want to ask is what steps must i take to ensure that my component has the size i desire it to have?
The tests i have done so far have been problematic. I draw an image of lets say 200*100 pixels and the layout managers display only a part of my component. I tried setSize, setPrefferedSize, setMinimumSize and none of them worked.
There is no way in the Swing model to outright guarantee that you will be given a set amount of space - layout managers can and do ignore minimum and maximum sizes, though normally they only ignore one or the other.
If you have a fixed size component, you should override getMinimumSize, getPreferredSize and getMaximumSize to all return a dimension of that fixed size that you need. If you can scale to some extent adjust the minimum and maximum as required. Overriding the methods avoids some third party code calling the set*Size methods and overwriting your choices (layout managers will still call setSize to tell the component what size it was actually allocated which is normal). It also makes sure the sizes are set before the layout manager starts laying out the component.
If the size of your component can change after layout has occurred, you need to make sure you invalidate the component layout properly but avoid doing this if you can.
the size is determined by the LayoutManager. if you will use a null LayoutManager you will be able to force a specific size (and location). otherwise you can override getPreferedSize() which will be respected by some layout managers.