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.
Related
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.
How do i have the JPanels in the Mainframe to appear on all other GUI i've created?
Do i have to make the same JPanels in the Mainframe again or is there another way to make it as a fixed layout so i can only edit the content part?
You state in comment:
i'm creating an application with a fixed desgin. So i have everything fixed in the Mainframe (the navigaion bar), So how do i have the navigation bar to appear on other JPanels for other GUI? I only have one JFrame.
I wouldn't have the same navigation bar appear in multiple JPanes, but rather would place my navigationBar in the main JFrame's contentPane in a fixed position, say in the BorderLayout.PAGE_START spot, and then swap my JPanel "views" in the BorderLayout.CENTER position using a CardLayout. This way only one navigationBar need be made, and it would be visible all the time (unless you explicitly choose not to show it).
I have to realize this design with Java Swing (see screen shot).
Is it possible? How can an element appear off the frame?
Add a JToolBar (non-floatable) to JFrame or parent JPanel (in the BorderLayout.NORTH position).
Put an Icon inside a JButton and add to tool bar.
Set "MODE DIAL" (or whatever) as the tool tip for the button.
Repeat for each icon/button required.
Use a PLAF to tweak the look.
You need to use a JWindow to contain your component.
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.
Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/DropTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
The Component Mover can do this for you.
Use the GlassPane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.