I have made a Java 3D applet which basically displays a 3D graph with nodes(the nodes are spherical). Now, I want a side panel which contains a list of these nodes,so when I click on a node, the corresponding entry in the side panel should get highlighted/checked(if its a check box) Can anyone please help me as to how to get this done?
Try limiting the size of the Canvus for your graph and place it in your JFrame. Then put your JPanel into your JFram. Should work thought i have problems with JPanels being in there correct spot.
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 have a draggable panel (not using the java drag and drop) that has to land on another panel. The dragged panel generates the event but I want to obtain the panel it lands on. Do I have to have an array of all the "landable" panels somewhere and cycle through it to get the one I want by comparing positions or is there a faster, better way?
Thanks in advance.
The Container class has a findComponentAt(...) method.
I try to find the best way to make an image clickable with different action depending on the area you click. For now, my window display an EFTPOS (image found on google image) using :
getContentPane().add(new JPanelBackground("./img/background.png", "./img/eftposAboveBackground.png", this.getWidth(), this.getHeight()));
Now, I would like to click on every EFTPOS's buttons. I was thinking of create some transparent buttons at the buttons's positions but :
First, I don't want to use an absolute layer
Second, the Swing GUI design editor (Netbeans) doesn't display my background image since I used the code above.
I'm pretty sure there is another way than setting my eftpos on the background, maybe using the ImageIcon class..
Any help would be greatly appreciated!
You can to create an ArrayList of Rectangles (or Shapes) to represent the area of each button on the image.
You need to add a MouseListener to the image.
In the mousePressed() event you need to iterate through all the Rectangle in the ArrayList using the Rectangle.contains(mouse point) method to see if the user clicked in a Rectangle.
When you find the Rectangle that was clicked you execute your custom code.
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 am currently working on an assignment and need to create basic controls (buttons) for the Mandelbrot set that will operate as a JApplet. The graphics is initialised in the init() method and a method which draws the Mandelbrot is called in start(). The problem is, I have searched high and low and cannot figure out how to add a GUI to my applet because
I don't explicitly add the mandelbrot to a JPanel , and..
I have no room left it seems to add a GUI because the Mandelbrot takes up the entire JFrame.
I had one idea which was to set the size of the JFrame, set the size of the Mandelbrot graphic to only be say 4/5 of the whole frame, and add buttons to the remaining portion?
Does that sound like a good solution?
Does that sound like a good solution?
No. The panel in which the Mandelbrot is drawn should return a sensible preferred size. Add the rendering panel and the buttons to a layout or groups of layouts using layout padding, borders and button margins for white space.
But I am a little confused by the reference to japplet tag yet the body of the question mentions both JApplet and (twice) JFrame.
An applet has to make do with whatever size it is provided by the HTML (or JS, in the case of the deployment toolkit script) that launches it.
A frame can call pack() on a properly laid out UI and expect to be the smallest size needed to display the components.
General tips
Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start. JWS allows the user to launch a JFrame from a link in a web page.
Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.