I have java swing aplication where I use Borderlayout as layout. I put elements in EAST,CENTER and WEST. I want to change size of elements with mouse just like in NetBeans.
For example when I move mouse on the edge of the CENTER I need to show mouse resize cursor. And when I drag mouse on the edge, the CENTER will change size accordingly.
What is the best approach to do it?
You would need to use a JSplitPane. Simply nest them in the border layout. This is a good resource for examples.
Related
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
I used the drag and drop interface to put the components where I want them to be, but when the window is resized they lose their relative position. I have attached a screen shot of my hierarchy and of two windows to show how the components lose their position.
Hierarchy
Fullscreen
If you want to use the AnchorPane to layout your components, you can set the Anchor Pane Constraints, like in the image below:
This way it doesn't matter if you resize the screen, the button will always stay 10px far from the AnchorPane's right border.
When you use the AnchorPane to place components on screen you are not going to have a relative positioning, You should use others containers to layout your application. Read more about how to use Layout Panes here: Using Layout Panes
You need to study how to use Layout Panes. Dragging and dropping components results in absolute positioning, which really bad practice.
Here's what I need to do:
Make a grid where I can drag and drop elements. (already done)
My problem is I need to give the option to enable magnetic grid, such as the Windows desktop when you allign icons.
What type of item should I use with Swing ? (Jpanel, GridLayout)
Is there an easy way to make a magnetic grid without coding everything?
*I need to be able to place items wherever I want when the magnetic option is not checked.
Thank you guys
Consider using a JLayeredPane, and adding as one layer a JPanel holding a grid of JPanels. When you want the grid functioning, then when a component is released over the GUI, the JPanel cell that contains your mouse pointer can accept the component and can perhaps center it using a GridBagLayout. When you want to place items in a free-hand way, then add them to a JPanel layer of the JLayeredPane that uses a null layout.
I have jLabel with an icon wanna put in right top corner , in Netbeans design view I place it to a desired position , After running program it is stay there fine ! But when I maximize the window it doesn't horizontally move to the corner and keep its position. It becomes like in the middle.
Thanks
Don't use a design tool to create the GUI. Its sounds like Netbeans is using a absolute layout which is not what your want.
Use a proper layout manager. Maybe a FlowLayout that is right aligned.
Read the section from the Swing tutorial on How to Use Flow Layout for more information and working examples.
I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.