Jave Swing Eclipse - JPanel and components auto resize - java

I am maintaining a system which requires me to make components in the dialog resizable, the dialog box calls out a java class Panel.
What is supposed to happen:
What is currently happening:
Note: The image on the bottom layer represents the resized one. While the image at the top layer is the dialog box which is not yet resized.
As you can see, the component JPanel(the one with the black border) is not resized. I am trying to achieve what happend to the bottom layer image of the first attachment.
I tried to apply the answer in How to dynamically control auto-resize components in Java Swing and pattern it in current code but since my panel is only called in a dialog box so there are limitations. The problem is that the components and its hierarchy have been already implemented, I just have to make it auto-resize.
Here is my current outline:

If you want a simple solution you can use a layout manager as described here.
Or, if you wish to avoid a layout manager(like me) then you can have your program resize your elements every time there is a resize event.
Here is some sudo-code
frameOrPanel.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
element.setLocation(frameOrPanel.getWidth()*1/4, frameOrPanel.getHeight*1/4);
element.setSize(frameOrPanel.getWidth()*1/2, frameOrPanel.getHeight()*1/2);
}
});
You will have to add unimplemented methods.
Note: the 1/4 and 1/2 is merely a ratio you can change those to fit your application.

Related

Increase the thickness of the border of the focused area in swing

I have a problem with Java Swing and the LAF.
I am using JGoodies and I tried to increase the thickness of the border of the focused area. I tried it via the UIDefault but there is no such option. example of the Border Can you give me a hint how to set the border?
I saw this post: Change the color of the Java Swing Component Focus indicator but there is no solution to my problem.
example
This style depends on the L&F, if it supports the Table.border propriety or the Table.scrollPaneBorder.
In some cases, this property Table.scrollPaneBorder is enough but there aren't rules to have the table wrapped inside the scroll pane wrap table. In other words, you can have a table without a scroll panel.
if you have a table without scroll panel, the problem can be resolve from LookAndFeel, if your actual L&f doesn't have this support, the solutions are multiple, such as:
Use another l&f. such as Material-ui-swing, the table.border is under the developing branch, you can compile the source.
Develop a personal TableUI to set the border inside the UI such as the point one
you can wrapper your table inside a scroll pane, but it should respect the L&f rules.
1. Implement the Table UI. (from material-ui-swing)
insert a Border inside the propriety Table.border, such as:
UiManager.put("Table.border", new BorderUIResources(YOUR_BORDER));
You need to wrap the border inside the BorderUIResource because, if you implement the switch L6f inside your APP, Swing doesn't remove the propriety without the UIResource interface.
public class MaterialTableUI extends BasicTableUI {
public static ComponentUI createUI (JComponent c) {
return new MaterialTableUI();
}
#Override
public void installUI (JComponent c) {
super.installUI (c);
table.setBorder(UIManager.getBorder("Table.border"));
}
}
This answer makes an overview of all possible solutions that exist if the table is unwrapped from the Scroll Pane. In fact the propriety Table.scrollPaneBorder should be work fine
Example with material-ui-swing
set border
UIManager.put("Table.border", BorderFactory.createLineBorder(MaterialColors.COSMO_BLACK, 5));

Java button within graphics panel

I'm relatively new to developing GUI's within java so this may well be a stupid question or quite simply not possible to achieve but here we go.
I've created 1 single JPanel with no border layout set up or anything like that and I intended to paint a GUI on top of it using the graphics class. The JPanel is just plain black and then I've drawn a huge box over it leaving the black just as a border, and painted the whole GUI within this white box.
I want to add buttons within that white box GUI as well but I've no idea how. In fact they don't even have to be traditional buttons JButtons, if I could just draw a shape and have that act as a button then add an event handler to just that shape that would work also but I don't know how I'd do that either.
I have so much code for my whole program (it's a school coursework project) that I'm not sure which parts would even be worth sharing to assist with this question since there's so many GUI aspects I've already drawn so I've tried to just explain my issue in words.
Honestly I have no clue what I'm doing so any help would be appreciated.
EDIT: Here's a screenshot of my current GUI with a 'sketch' of how and where I'd like to be able to add buttons.
GUI Image
As with any suitably complex UI, you need to start by breaking it down into manageable chunks, focusing on areas of mutual interaction and functionality.
For example...
Says to me that you have two primary UI elements, the left and the right.
This could easily be established with a GridLayout, but, if the two sides are not equal in width, a GridBagLayout might be more appropriate
The right side to me says simply, JTable. You could place this within a container using a BorderLayout, allowing the table to occupy the CENTER position.
The key information would then a component laid out with either a GridLayout (top and bottom) or a GridBagLayout if the requirements are more complex. This component would then be added to the SOUTH position of the BorderLayout.
Again, this is pretty simple. The primary layout would probably be a BoderLayout, with the title in the NORTH position, the graph in the CENTER and the buttons wrapped in a component in the SOUTH.
You could use either a FlowLayout or GridBagLayout to layout the buttons depending on your how you want them to appear
Recommendations
Have a look at:
Laying Out Components Within a Container
How to Use Tables
And for the "border", I'd recommend you have a look at LineBorder. Take a look at How to use Borders more details

JFXPanel Dynamic Sizing

In the process of converting from Swing to FX. We currently have JFXPanels within JInternalFrames. When initially displaying a new screen, everything is fine because we are first setting the scene and then calling the pack() method on the JInternalFrame.
The problem lies with screens where new Nodes get added (typically based on ChoiceBox selection) to the screen that require the panel to be wider. It currently just tries to fit the Nodes in causing some of the labels to become ellipsis. What is the best way to go about having the JFXPanel/JInternalFrame resize when needed?

Can I place image outside JFrame?

Can I place an image outside JFrame?
I am developing an app, and I wanted to make the Gui good looking and some part of the buttons should go outside. Is there a way to do this?
Yes, but it's not going to be easy, as you going to constantly need to monitor the position of the parent frame in order to maintain the position of the child window.
Essentially, what you can do is create a second, undecorated and transparent window. You would need to align and size the window next to the parent window.
On to this child window, you would need to then add a transparent component which would act as your primary container.
Take a look at How to Create Translucent and Shaped Windows for more details
For example:
How to draw images on transparent window?
How to make a transparent JFrame but keep everything else the same?
Creating a JFrame you can click through
No; the Swing framework doesn't handle painting outside the root component.

Using ComponentAdapter to determine when Frame Resize is finished

I'm using a simple ComponentAdapter to do something when the main JFrame window it's added to is resized. It's picking up the events without issue however I only want to act once a user has finished the resize. The componentResized() method fires multiple events for every pixel change of the resize and I don't want that as I need to scale an image when the window is resized and when it's done for every pixel it creates a huge lag.
I tried using a MouseListener on the frame to detect mouse up and down events to set a boolean as to whether it was being currently resized or not, but the events were never being triggered.
This is the simple ComponentAdapter:
private class ResizeListener extends ComponentAdapter {
public void componentResized(ComponentEvent e) {
onResize();
}
}
And it is added to the frame in the constructor using this.addComponentListener(new ResizeListener()); The class is extending JFrame so it should be added to the frame. I tried using getContentPane().addComponentListener(new ResizeListener()); but that didn't make any difference.
Any advice on a simple or effective way of only running the componentResized() method when the window is actually finished resizing would be appreciated.
Goal
I'm implementing a PDF reader which at the moment converts the page being viewed into a BufferedImage. When the user resizes the window I need to appropriately scale the image which means I can't let layout managers look after that for me. The number of componentResized events creates a huge lag as the image is being resized for every position along the user's resize path so I need to do it once the resize is finished.
Toolkit.getDefaultToolkit().setDynamicLayout(false);
This will affect all windows in the application, so if you only need this feature for a specific frame you may also want to add a WindowListener to the frame and then handle the windowActivated/windowDeactivated events to set this property.

Categories

Resources