I have a component that is inserted into the main content pane, and for some reason, it is not filling the area.
getInstance().add(internalPanel, LayoutFactory.newFactory().setX(1).setY(1).
setWeightX(1).setWeightY(1).setFill(GridBagConstraints.BOTH));
I made the LayoutFactory specifically for GridBagLayout, it just sets the property and returns the object.
Basically, the problem is that when I add the component to the content pane, it does not fill the space dedicated for it. It takes the absolute minimum, as if weightX and weightY were set to 0. I have tried to setAnchor(GridBagConstraints.FIRST_LINE_START) and setAnchor(GridBagConstraints.NORTHWEST). Neither have any visible effect on the program.
Also, when I set the border of the component, it only fills part of the space, so it is not a problem with the component I am adding to it.
It is worth noting that the component shows up in the centre of the space, and does not follow the anchor. From here, I figured my problem was the weightXandweightY properties. Looking at the other components of the content pane, the weight is set on every one of them.
getInstance().add(MainFrame.buttons, LayoutFactory.newFactory().setX(0).setY(1).
setWeightY(1).setWeightX(0).setFill(GridBagConstraints.BOTH));
and
getInstance().add(panel, LayoutFactory.newFactory().setX(0).setY(0).setWidth(2)
.setWeightX(1).setWeightY(0).setAnchor(GridBagConstraints.FIRST_LINE_START)
.setFill(GridBagConstraints.NONE));
It's certainly possible that something is going wrong somewhere else, but I didn't want to swamp this post with text. If you need more information to figure this out, I'd be more than glad to provide it. Hopefully this made sense. I've been using GridBagLayout for a while now and I've never run into something like this.
Here is an image of what it looks like in the space it is supposed to be
EDIT: Found out the problem lays somewhere else, as making a small example program works fine. Since I am a little stumped, I uploaded a full version of the program I am working on. Don't worry about what it actually does, I just care about the workload panel not filling the space.
Solution to problem :
Was adding component to InternalPanel type object, which had no layout manager. (Defaulted to flowLayout)
Related
Okay, I am kind of desperate right now. I hope you guys can help.
I need to layout content panels with Java Swing. The Problem is, that every content is different. So I need a panel that resize itself for every content. Basically what LayoutManagers are invented for.
I need a left panel and a right panel. The widths of the panels should be fixed. The heights should adjust to the given content
|<---- 30% ------->|<----- 70% -------------------->|
Easy going I thought, but it just wont work. I tried different layout managers. Some of them keep the 30% rule, but doesn't wrap the content and just display them in one single line (BorderLayout).
If a LayoutManager does support line-break (even if its just for HTML text but that is fine for me) it wont support the fixed width. A combination of both didn't worked for me either.
Note that I need to stick to Swing and can not use another more advanced library because the system I am developing for is stuck to Java 1.5. Furthermore, I know the total screenwidth so I could calculate the width of the panels to work with fixed widths, but I need to be flexible with the height.
You can achieve this by using nested BorderLayouts. Start by setting your Panel's layout as BorderLayout.
After that, for each left and right panels, set layouts as BorderLayout again. At this level, you will set %30 and %70 ratio.
Within this layouts, add your contents to NORTH layouts. This will enable your panels' height to match given content.
I am working on java swing and I am stuck with a UI layout
My current output is as below.
I want to modify it a lil and add 2 text inputs in between as shown in sample below. Please help on how to achieve the text inputs side by side.
First of all: You should really give your components more meaningful names than jComboBox2.
Your example picture is not that easy to produce with GridBagLayout. You have to understand that the layout will create a n*m grid and you can put your components (like textfields, labels, comboboxes, etc.) freely anywhere inside that grid.
For example your jLabel4 is at the position 0/3 in the grid and though i'm not actually sure what a gridwidth of -1 does i'm pretty sure it's still at 0/3. If gridwidth was for example 3, your jLabel4 would span from 0/3 to 2/3.
So if you want to put something between those two rows, you'll need to put it at the right grid coordinates and give it the right width and height.
BUT: Sadly, getting it exactly as in your picture requires you to use some tricks (for example increase the grid width of the upper and lower components or add another panel containing the new row components instead of the components themselves).
Try to somehow make it work (even if it doesn't look exactly like your picture) without those tricks first as that might help you understand how GridBagLayout actually works. As soon as you really understand that, it should not be that difficult to recreate your picture.
i wanted to ask, if somebody might have a solution about a problem i face. I am working at an application, which draws an animation - for instance a map with objects moving onto. My problem is, that on top of the drawing, a Jtable, Jlist as well as other Components are also placed.
In my particular example all of those components have been added to the Panel, which holds the map. In result each component gets redrawn as often as good my fps is. Therefore making one of the tables invisible reduces the already high cpu usage of sometimes around 50% to less than 30%.
My question is, how can i avoid calling somewhat static visual contents paintComponent() method, without having the "background" - the map - whited out the menu.
Since the animation redraws permanently the menu is not shown at all, if its separated from the corresponding JPanel.
First thoughts move into following directions:
Clipping - actually not as good as i would like to, since id like to enable moving around the menus.
JLayeredPane - already tried but seemed to turn out, that the paintComponent method of menus still gets called frequently.
JWindow/Internal Frame - had that thought a couple of minutes ago. Having a complete independent container shall be able to handle my regard, or?
I am looking forward, if somebody has an elegant idea, how to fix that and reduce the cpu usage significantly.
Thanks!!
Best regards.
I would create a custom Shape for clip. Use Area class and subtract from the Area all the children components' bounds.
For painting over JComponent(s) placed into JPanel you have look at
JLayer (Java7) based on JXLayer(Java6)
GlassPane, notice all JComponents must be lightweight, otherwise is GlassPane behind heavyweight (J)Components
is possible painting to the JViewport,
EDIT
you have to use Swing Timer for moving with Icon (in the JLabel) placed into JXLayer, GlassPane or JViewport, don't use Runnable#Thread and never, not by using plain Thread.sleep(int)
I'm experiencing a very weird problem in Netbeans 7.0.1 while designing a GUI.
At first it was all good, I could design and adjust all the swing components easily and normally. But today, for any reason, it turned to be faulty in which the whole JFrame is extended vertically way way way down (~37,000 in height!), and all the components mixed up messily. However, I couldn't do anything about this, since this happened it doesn't allow me to drag the frame's bottom side upward to minimize the height.
The worst thing is that I couldn't select (click on) some of the components, but some others can be. I don't know how to thoroughly explain this weird thing, just have a look at the Screen shot.
Tell me if you need any further info.
Cheers.
Select the JFrame component from the Navigator panel, and change the height of the JFrame from the properties panel to something like 200. Once you get the size under control, you can use mouse for resizing it to desired size.
This condition may happen if something went wrong while closing the file or if the IDE was terminated abruptly and if the IDE does not get a chance to write all the metadata about the UI properly.
First Note that You can't resize the JFrame if one of the components is the same length as th JFrame. Try resizing that tabbed pane first, then also the list on the left of the tabbed pane (Ongoing Projects).
Next use the inspector to select any component you couldn't select. It might be that another transparent component is covering it, or that it is in a panel and the panel is getting selected instead. Or even the other way round when you can't select a panel because it contains another component. So use the inspector to select components instead.
Finally, if the scroll bar is too long for you to resize easily, you can use the properties window, but note like I said, resize the components in the JFrame first before resizing the JFrame.
Hope this helps
Try to change the size (Height and width) of frame in preferredSize property,and use Panels. Without panels you cannot achieve the arrangement of different objects on a Frame.
After a lot of frustration over multiple failed attempts at fixing this issue using all the tips given by the users above [thanks everyone, but it didn't work :( ], I restarted the NetBeans application in despair. It worked. Hopefully this'll help someone else with the same problem!
Is there a simply layout manager I can use in a JPanel to create something akin to a bar chart? FlowLayout almost meets this need. The added component orientation needs to be left to right (default for FlowLayout), but they need to "rest" on the bottom of the panel with excess space at the top (not available in FlowLayout). Also, the components will all the be the same height and width.
Thanks.
A BoxLayout will do the trick as demonstrated in this posting
If you are going to do something like a bar chart, you might want to consider not using Components at all. Just have a single JComponent that overrides (IIRC) paintComponent. It'll be easier to do the calculations in a manner appropriate to a bar chart rather than trying to use an inappropriate layout manager abstraction.
FWIW, I default to GridBagLayout, even if a simpler layout manager will do, on this basis that the code can be more consistent.
You can do exactly what you want in GridBagLayout. Yes, I know everyone hates GBL; yes, I know I'll get down-voted. But it really is not difficult to understand and you can use it for almost any layout goal.
The trick to get a component to "stick" to the bottom is to use the anchor and fill properties of the GridBagConstraints object properly (i.e. SOUTH and NONE)
A BoxLayout might work for you. It lets you layout components left-to-right or top-to-bottom, with the tightly coupled Box class to force spacing constraints.
I actually prefer the FormLayout, since it is very flexible but you have to write a lot of code though. And in the beginning its a little bit confusing with its percentage and pixel parameters.
But you can for example tell a control that it is 5 pixels left of another control (thats the main part...it layouts controls in relation to neighbors), then it takes 100% of the lasting space availabel including a border space of 5 pixels (you need to use -5 then).
I think it looks somewhat similar to this
FormData data = new FormData();
data.left = new FormAttachement(neighborControl, 5);
data.right = new FormAttachement(100, -5);
...
button.setLayoutData(data);
This example is for JFace, but there are Swing implementations as well.
I will look up my old code later this day to check if the code I wrote is right :)
HereĀ“s a additional link