Jpanel Move items freely at design time - java

I am very new to Java Swing programming and I have a small problem.
When I design a user interface using Jframe and JPanels, JPanel does not allow to drag and drop an item in a place I desire (At design time ). Lets say I add a button to JPanel, then the button is stuck in the middle of the panel and I cannot drag it to a place I desire.This does not happen when I add items directly to the JFrame.
I tried using setBounds(int x, int y, int width, int height) method to move the button in JPanel but it did not work.Following image would describe my problem well. I want to drag the button in the JPanel to the left, just like the button added in the JFrame.
Button in JPanel - Top and JFrame - Bottom:
Is there a way to drag and drop items freely in the JPanel as well?
Thanks.

Posting as an answer since I do not have enough reputation to comment. I see you are using eclipse for building your GUI. However, Netbeans IDE allows you to design the interface as you have mentioned.
While creating the GUI manually is a good idea, if you want to speed up the process, you would need to use an IDE.If you are not restricted to Eclipse and want to use an IDE, I would suggest Netbeans.

Go to the properties of the Jframe or JPanel, there is a property as Layout, select the 'Abosolute Layout' from them. Then you can place any AWT Component you desire.

Related

animating components in Jpanel

I have this idea that I want to implement into my project.
I know it is doable, but I do not know where to start.
I have a JPanel,
in there will be a Jbutton , a JLabel, and a ComboBox, and a JtextArea.
Originally, there is only the JtextArea and JButton on my panel.
When I hit the button, the Combobox, and JLabel will slide in or fade in, or drop down, or event rotate in.
So how would I accomplish this, I know I have to use graphic g, repaint(). It is easy to do the Jlabel but what about the combobox.
Should I add the combobox and Jlabel into a separate Jpanel and make that panel animated in. but even that, I do not know how to make this panel in motion.
Please help me on this, and example would be appreciated
Thank you!
To get started, you'll probably need to research these terms:
Slide layout (thanks #Andrew Thompson)
setLocation
TimerTask (I think, or maybe just Timer)
clobbering graphics objects
Once you know about that stuff, you'll be in a position to make the design decisions that you're asking about.
It sounds like you'll have a child panel with a slide layout. Try not to use null layouts when possible.
You'll set its location or style per each tick in a timer task.
If you're setting a style, you'll need to clone your graphics object to avoid "clobbering" it - that is, work on a copy of the graphics object, so the original isn't changed or repainted accidentally while you're working on it.

Position of JComponents using Netbeans GUI Swing Builder

I'm developing a graphic interface using Java and Swing, and I'm having a hard time getting the JButtons to stay in their position while changing from one panel to another.
There are three buttons in a row aligned in the left bottom of each panel, all the panels the same size, but somehow they manage to change their position a little when I run the application (on the design preview they show up in the right place). It's getting a bit annoying. I'd appreciate any help
Are you trying to do tabs? If you are, a JTabbedPane will do this much better than a button.
Since you are using the Netbeans GUI Builder, look at the options in the Component panel on the left. It has Swing tabbed panes and AWT panes if you really want.

Dragging JFrame from JToolbar

I am currently attempting to move a JFrame (the same way you would by dragging the window's title bar) when the mouse is being held down over a JToolbar. The JToolbar is inside several layers of JPanels nested in the JFrame. How would I go about doing this?
EDIT: There seems to be some confusion about what I'm aiming for here. I still want the titlebar to be draggable, but I just want to extend that functionality to the JToolbar as well (while keeping JToolbar not floatable - locked in place in the frame). See screenshot:
The Component Mover should work. See the last example. Instead of a titleBar you have a toolBar.
This probably isn't the most elegant solution, but...
You could create a class that extends a JToolbar and that implements a MouseMotionListener. When the user clicks on it, you would use setBounds() on the JFrame and give it a new location relative to how much the user moves the mouse within the JToolbar.

How to Display a Panel in another Panel?

I'm coding a simple graphics program in Java.
So, I have 3 classes.
The first class is the GUI w/c extends JFrame, it load the menu bar and panel (drawing class)
The second class is the drawing class, it extends JPanel, and it has simple Graphics commands.
The third class is an animation class, it displays an animation. It also extends JPanel.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class. When I try to place it there, it displays a tiny box beside the drawing class panel. I am not very good at frames and panels. Thank you very much in advance!
All JPanels have a LayoutManager which, rather handily, manages how Components are laid out. The default layout is FlowLayout, which default mode will simply place each component to the right of the last component.
If you want to change the layout to something more useful, there are many options; BorderLayout, GridLayout, GridBag layout are popular ones. Myself, I use MigLayout, an external library which is very powerful :).
As for it appearing small, try manually enforcing the size with setSize(w, h) or setPreferredSize(w, h).
You might want to write
GUIFrame.remove(drawingPanel);
GUIFrame.add(animationPanel);
GUIFrame.pack();
However the behavior may vary if you have other components added to your GUIFrame. It is difficult to help you exactly because you have not posted an SSCCE.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class.
Use a Card Layout.

How to set JPanel size?

I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.
With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.
Or maybe check some tutorials at NetBeans site.
You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.
The trick with layout managers is that you can use not just one but several, in nested JPanels. That way, nearly any layout is possible.
This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.
Do the following:
Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)
Check, what layout is defined for the container
Read the documentation about LayoutManager's (they're in java.awt package)
???
PROFIT!
In my experience, Swing is a very picky thing. I'd try setMaximumSize and setPreferedSize. As a side note though: when ever I used GridLayout, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.
-Brett

Categories

Resources