Netbeans null layout causing border around background when resizable unchecked - java

I'm making a simple JFrame with the GUI editor in netbeans with a background image set as an icon in a label as suggested by the netbeans site, with a label and a button centered. I was having a very hard time centering them without using the null layout and setting the pixels to center them. I have an 800X600 image as the background, and I don't want the window to be resizeable. So I unchecked resizeable in the properties, and on the code tab I have designer size set to 800, 600, generate size is checked, and the form size automatically sets to 816, 638. This then gives me a border around the right and bottom sides of a few pixels. If I change the Form Size to 800, 600, then the background image is cut off by a few pixels. One other thing that I set that may impact that is in the properties=>bounds set to 800, 600, 800, 600.
Any advice on how to get rid of the border without allowing the window to be resizeable as well as any on whether a different layout can help with centering would be greatly appreciated. I did find some information that Grid Bag layout would help, but I wasn't quite able to get it working correctly. I suppose that writing out the code instead of using the GUI editor may also be a better alternative, but I'm pretty new so any advice on that would be great as well.

Don't use null layout when you can center components quite easily if you use the correct layout or combination of layouts. For instance if you want a JLabel next to a JButton and have them centered in a JPanel, put the JLabel and JButton into their own JPanel first (make sure to have this JPanel's opaque property set to false) and then have the containing JPanel use GridBagLayout. If you add one component (the inner JPanel) without GridBagConstraints, the component is centered automatically, even if the containing JPanel is resized. It's almost idiot-proof, whereas null layout is a recipe for difficult hard to maintain code.

Related

JTextArea wont ".paint" to the correct location on screen

Im having a problem with JTextArea that has been quite annoying.
I am trying to make a 2D game for a project, and I am currently attempting to put text on my screen using JTextArea instead of just images.
My code for configuring the JTextArea is as follows:
Insets margin = new Insets(10,30,0,0); //bottom, left, top, right
textArea = new JTextArea();
textArea.setSize(textWidth, textHeight);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setMargin(margin);
textArea.setFont(halFont);
textArea.setForeground(Color.YELLOW);
textArea.setBackground(new Color(0,0,0,0));
textArea.setLocation(0, 600);
My problem is that when I "paint" it to the screen using:
textArea.paint(g);
It just paints to the position 0,0 however it keeps the rest of the textArea formatting including color, insets, font, etc.
When I run textArea.getLocation(); after painting, it claims it is at the position 0,600 even though its clearly at 0,0.
I have tried changing the x and y position calues, but the text still stays at 0,0 on my screen. Because of this i'm fairly certain that it is something to do with the .paint call rather than .setLocation call.
I realize that I should be using a format manager of some sort, but since i'm using .paint I get the feeling it won't help.
Q: How can I use the .paint call and change the position of the text being painted to the screen.
Thanks for your time!
When you are using Layout managers, the layout manager itself is the one that calls setLocation, and setSize, so any attempt you make will be overwritten. You need to use the layout manager and allow it to place the component where it thinks it should go. You influence its position by
1) what layoutmanager you choose
2) your use of setPreferredSize(Dimension d), setMinimumSize(Dimension d), setMaximumSize(Dimension d)

Placing border in desire location inside JPanel

I am trying to put a border around the buttons inside of my JPanel. I have this:
Border lineBorder = BorderFactory.createLineBorder(Color.black);
welcomePanel.setBorder(lineBorder);
But that only puts the border around my entire application window, which makes sense. I am wanting to be able to place the border where I want.... I did this when placing my buttons in the desired location
button1.setBounds(10, 10, 60, 30);
And I looked in the API and saw a paintBorder method with parameters of int x, int y, int width, int height, which would make sense to me, but I couldn't get it to work.
Any advicewould be appreciated
Start by adding you buttons to another JPanel...
JPanel buttons = new JPanel();
buttons.setLayout(...);
// add buttons...
Set the Border of this panel...
buttons.setBorder(BorderFactory.createLineBorder(Color.black));
Add this to you main container...
welcomePanel.add(buttons);
Pixel perfect layouts are an illusion in modern UI design, you don't control factors like font choices, rendering pipelines, DPI and other factors which will change the requirements needed for each component to be positioned, that is the role of layout managers.
Swing has been designed to work with layout managers, attempting to do without will cause no end of issues and frustration as you try and find more hacks to get around the issues.
You can use things like EmptyBorders to introduce empty space between components or Insets with a GridBagLayout

Java Swing, making a gridlayout fill parent?

I just recently started using Swing to create GUIs for programs, and it's been pretty fun to mess around with so far. However, I'm having an issue with a JPanel with the layout set to gridLayout. Right now it looks like this:
The grid on the right is a JPanel set to a GridLayout, with each cell being a bordered JLabel. The options on the left are also inside a JPanel, and the left JPanel and right JPanel are nested in a GridBagLayout set on a JFrame.
Essentially, my problem is that I want to "scale" the grid on the right so that each cell is a certain height and width. The grid itself will have a variable number of rows and columns, which are set when the program first starts up. Eventually, I plan to have the right JPanel in a JScrollPane (if that's how that works...), so I'm not really concerned about whether or not all of the grid shows up onScreen.
I tried setting the fill value for the gridLayout to "BOTH" and it gave me the following result:
This is closer to my intention, but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel. Additionally, I would want the JLabels to be the same height and width. However, I don't know exactly how to do that. I've been messing around with it for a while now, and I'm not sure if I'm just too much of a noob with Swing, or if I'm missing something in the documentation.
In the end, I'd like the grid cells to be a fixed height and width, no matter the number of cells, and no matter whether it goes offscreen or doesn't fill it.
(Also, I just thought, maybe it's not the best idea to code this and then shove it in a JScrollPane later and expect it to perform the same.... I guess I'll just see what happens.)
but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel.
Check out Darryl's Stretch Icon which will allow the icon to resize to file the space available for the JLabel.

Make a panel use the exact space it needs and fill in the rest with another panel

I want to make a layout with two componenents in a vertical row.
The first component is an instance of my own class, ImagePanel, that extends Panel and shows an image, and I want it to take up the exact space that it needs to show the entire image. The remaining space should then be filled by the other component (in this case another Panel with a GridLayout). See the picture.
In Android you can do this by using the weight property, but I have not been able to find anything like that in Java, and I canĀ“t see that any of the standard layout managers in Java would be suitable for this.
I tried putting the ImagePanel in BorderLayout.NORTH and the other panel in BorderLayout.CERNTER, but the second panel was then overlapping the image, so that didn't work.
I also thougth about using a GridLayout but the grid would not care about the size of the image, so I don't think that would work either.
Any help is appreciated.
A BorderLayout should work, provided the image panel's getPreferredSize() returns the correct dimension (i.e. the dimension of the image it displays).
Using a simple JLabel containing an ImageIcon and no text instead of your custom ImagePanel would do that for you.
The most powerfull layout in java is GridBagLayout. It has weightx and weighty properties, anchor, fill, etc.
By default, with GridBagLayout each component has all the necessary space to show fully. If you want the second panel to expand, it should be enough with weighty=0.0 in your image and weighty=1.0 in your other panel.
The javax.swing.Box should be exactly what you want. There are 2 types of boxes, vertical and horizontal, you need a vertical:
Box box = Box.createVericalBox();
box.add(comp1);
box.add(Box.createVerticalStrut(5)); // add some empty space
.
.
.
add(box);
Box will only force one side to be equal, for vertical, it's the width, the length could be any size.

Label in frame(Eclipse)

I have a JInternalframe in the Eclipse Visual Editor but it does not have a JContentPane.
When I drag a JLabel onto it I can't re-size it but when I put a JContentPane in the JInternalframe and make its layout NULL the label can be re-sized.
Can someone help me how to put re-sizable label in a JInternalframe without a JContent pane?
The size of the elements in Swing is very integrated to the type of layout your using. Null layout lets specify the sizes, but you should avoid it. Border Layout lets you specify preferred, minimum and maximum values for the width and height.
I recommend to find a layout that suits your needs and use it. Some of them will resize the JLabel to fit its contents, some others will let you specify the size you want.

Categories

Resources