Placing border in desire location inside JPanel - java

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

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)

Can't resize JLabels in BoxLayout

I'm trying to set up a few JLabels to use as buttons inside a BoxLayout, stacked on top of each other. The layout is fine, but I'm finding that I can't resize the labels to the dimensions I want. I'm using the following code to size them:
JLabel fileAddBtn = new JLabel("Add File", SwingConstants.CENTER);
fileAddBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileAddBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileAddBtn.getMinimumSize().height));
and
JLabel fileRemBtn = new JLabel("Remove File", SwingConstants.CENTER);
fileRemBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileRemBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileRemBtn.getMinimumSize().height));
As of now I have two labels, with one being longer than the other. They are both taking the width of the longer label, which is good, but the labels are hugging the edges of the text right to the nearest pixel. Is there any way to make the labels a little bigger so that there is a bit of a border around the labels? I've tried using setSize() but it doesn't take. I've also added straight values into the above code, but it doesn't change them either. I tried adding an EmptyBorder() around them, which worked for sizing, but it hid my line border which surrounds them. Any thoughts?
Is there any way to make the labels a little bigger so that there is a bit of a border around the labels?
Sure. Add an EmptyBorder.
But since the code is already adding a border to the labels, to retain that line border, make a CompoundBorder consisting of the empty border and the line border, and set the compound border to the label.
See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

Dynamically resize images linked to JLabels in a GridLayout layoutmanager

Current implementation layout:
((EDIT: added
Code: ))
private JPanel panelCenter;
private List<BufferedImage> listCreatedImages;
public ChooseCircuitPanel(List<BufferedImage> listCreatedImages) {
this.listCreatedImages = listCreatedImages;
initiate();
}
private void initiate() {
setLayout(new BorderLayout(50, 50));
panelCenter = new JPanel();
LayoutManager theLayout = new GridLayout(0, 3, 0, 0);
panelCenter.setLayout(theLayout);
panelCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK));
for (BufferedImage bufferedImage : listCreatedImages) {
ImageIcon theImage = new ImageIcon(bufferedImage);
JLabel lblForImage = new JLabel(theImage);
lblForImage.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panelCenter.add(lblForImage);
}
this.add(panelCenter, BorderLayout.CENTER);
}
Situation:
We want to display a race circuit here. A circuit should be displayed by placing standards tiles next to each other. It should be possible to resize the window, and with that, the circuit tiles should also resize.
((EDIT: bit more info: The race circuit data is stored on a server and the desktop application has to translate the data to a visual thing, by placing some standard tiles in the right order. ))
((EDIT: we are not allowed to use any external library. It should be doable by only using Java Swing code.))
I thought about placing the images in a JLabel and placing these JLabels in a panel with GridLayout as layout manager.
Using a GridLayout - I thought - it should be rather easy to get to a solution:
the components in the GridLayout (= JLabels) already scale and do exactly what I want.
Now, it would only be a matter of finding a way to resize the images so they fill the JLabels.
As you can see: right now, the images have a fixed size and don't scale at all.
I browsed a bit and saw lots of solutions that boil down to using Graphics2D and super.paintComponent, etc.
But most of these solutions had nothing to do with a GridLayout.
So conclusive question:
Is there an easier solution aside from using Graphics2D, etc. knowing that I use a GridLayout?
If not, I will of course use Graphics2D, etc. but I'm now just exploring my options. :)
((EDIT: SOLVED The tiles now neatly fit on each other. Don't mind the misalignments, that's our fault.))
There are no Swing components that do what you want so you will need to write your own code.
The easiest approach would be to use Darryl's Stretch Icon on your JLabel.
Or another approach is to create your own custom component that dynamically scales the image as it is painted. Something like the Background Panel which has code that allows you to scale or tile an image.
Given the nature of the view, I would recommend abandoning images altogether and instead implement the rendering in an Icon. Presuming you can make an icon scale with the label.

Netbeans null layout causing border around background when resizable unchecked

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.

java border gui

I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).

Categories

Resources