Obtain a JPanel's display area dimensions, Windows vs Mac - java

I have code that generates a JPanel that needs to run on both Mac OSX and Windows environments. I would like to draw a series of rectangles that frame the JPanel's display area. However the display area of the JPanels differs in the two environments. In the diagram below, the Mac JPanel is on the left and a Windows JPanel is on the right.
Is there a property that makes reference to the display area of a JPanel as opposed to its size?
The two JPanels in the diagram were made with the same code. In both cases I have the following command:
setSize(400, 400);
And in the override of paintComponent, the outermost rectangle (cut off in both in the vertical but only for Windows in the horizontal) is drawn as follows:
g2.drawRect(0, 0, 399, 399);
(The inner rectangles are displaced by an increment of 4 pixels and alternate in color. This isn't exactly what I hope to draw, but it illustrates the problem.)
I could manually determing the adjustments needed, and create separate constants for the two environments, but I'm wondering if there is something I've missed in Swing where it is possible to obtain the display size of a given JPanel. If such a function or property exists, making use of it would be cleaner and more flexible, as well as hedging against JPanels being given different title-bar sizes or padding settings.

In the paintComponent() method of your panel you use:
int width = getWidth();
int height = getHeight();
This will give you the current size of the panel whenever the component is repainted.

Related

How can I determine the height and width of a JPanel?

I have been making a program where I have created a JFrame with a BorderLayout distribution. On the center I have a class extended from a JPanel called AreaDibujo where I use a BufferedImage. In order to use this buffered image properly, I need to pass two int parameters on its constructor, the height and width it will cover.
How can I make it cover all the panel? AKA how do I determinate my panel size (the program runs on fullscreen)?
Here is an image of how does the program look so far:
How can I determine the height and width of a JPanel?
.. I have a class extended from a JPanel called AreaDibujo
Since it seems the code is using custom painting, the height and width of the panel can be determined when the paintComponent(Graphics) method is called, using this.getWidth() and this.getHeight()..

JMenuBar makes JFrame "jump"

I am creating a Java application of a sea map, where I use a JFrame as Canvas to draw the sea itself. I have a GUI on top of this as a JPanel. I recently added a JMenuBar to the JPanel with different functions. However, I noticed now that the the newly added MenuBar 'pushes the application' downwards ~10-15 pixels — and when I zoom/pan the map, the map makes some weird 'jumps' – likely trying to readjust.
Does anyone know why this happens?
Nine out of ten, you need to be looking at insets.
It's been a while since I used Swing directly, but all containers have a method called getInsets(). This returns an Insets object, describing the size of the border of the container--including your JFrame. JMenuBars tend to nudge those insets a bit, leaving you two two main options.
What I recommend is doing your drawing to a JPanel, placed in the JFrame; so that you don't have to worry about the JMenuBar. Use the panel as a canvas instead.
The other option is to poll for the insets at the time of drawing, and alter your coordinates accordingly; but I have to recommend against this if you can still avoid it, as it runs against just about all of my modular programming instincts.

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

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 (Swing): finding a component's *screen* size when the window is resized

I need to find a component's exact screen size in pixels when the main JFrame is resized.
I've tried several things and couldn't find an easy way to do it: it probably have missed something obvious.
BBBBB JFRAME BORDER BBB
BZZZZZZZZZZZZZZZZZZZZZB
BAAAAAAAAAAAAAAAAAAAAAB
BCC1................DDB
BCCC................DDB
BCCC................DDB
BCCC................DDB
BCCC................2DB
BEEEEEEEEEEEEEEEEEEEEEB
BBBBBBBBBBBBBBBBBBBBBBB
Resized, it could become this if, say, the user made the main JFrame shorter (vertically) and wider (horizontally):
BBBBBB JFRAME BORDER BBBBBB
BZZZZZZZZZZZZZZZZZZZZZZZZZB
BAAAAAAAAAAAAAAAAAAAAAAAAAB
BCC1....................DDB
BCCC....................2DB
BEEEEEEEEEEEEEEEEEEEEEEEEEB
BBBBBBBBBBBBBBBBBBBBBBBBBBB
What I want is to find the exact size in pixels, on screen, of the rectangle area indicated by dots.
I'm adding a ComponentListener to get the resizing events, which works fine.
The issue I'm having is that calling getWidth()/getHeight() on a Component does apparently not return the component's screen size but the component's actual size (and you can have, for example, a JPanel wider than the component it is into).
Any hint most welcome.
You're looking for JComponent.getVisibleRect().
getWidth() and getHeight() returns the width and height of the component. I've never heard of any difference between "actual" width/height and "screen" width/height.
Some layout managers however, does not fill the panel with a component it contains, and some space between the component and the panels edge may not be occupied by the component. In that case I usually setBackground(Color.BLUE) on the underlying panel to see what's going on.
This is a very rubbish way but you can use a wrapper Panel, where you put your main panel into. Than you can call getWidth() and getHeight() from the wrapper panel.

Categories

Resources