In .NET a UserControl may be composed of Controls and it may be part of a Form or part of a bigger UserControl. One can design UserControls with a Visual Designer using the ToolBox Palette.
I have found out that a java BeanForm is the analouge for a .NET UserControl.
I can design a BeanForm with a visual Designer in NetBeans
In my Library I can extend a BeanForm from any other Library and I can design the extended BeanForm with a visual Designer in NetBeans.
In my main Project I can pick all my BeanForms from the Palette onto my frames.
So "BeanForm" is the java pendant for the "UserControl".
In .Net I can easily shape my UserControl to a Fish or to a Star or to whatever I like
by simply setting this.Region = new Region(graphicsPath).
I know that Java is not as easy as C# and I have found Java Samples that partly describe the necessary steps BUT
I did not find a Shaped non-rectangular BeanForm Sample.
Of course extending the MouseAdapter with a new special mouseClicked(MouseEvent e) -- as many Samples do suggest -- isnt enough - this is only a first step.
Did they all forget about the Dragged-Event and the Focus Events and the Mouse-Entered Event and Mouse-Up Event and Mouse-Leave Event and all the other typical JComponent Events ??? - Such Component will SURELY NOT BEHAVE like a real Shaped JComponent should behave.
Please give me a Sample for a OvalComponent-BeanForm that can be taken from the Palette onto my frame and that will behave like a REAL Shaped JComponent
It should cover All the necessary Mouse Events and all the necessary MouseMotion Events and all the Drag-Drop Events and all the Key-Events in case the Oval has the focus or in case the Mouse is over the Oval.
(e.g I should be enabled to use the Oval's Mouse Events and to extend the Oval Component to a MovingOval Component that can be moved around within the frame ...)
If this is too much work for you then, please, outline and describe the steps I would have to implement - like MouseAdapter, KeyAdapter, maybe DragDropAdapter or whatever is necessary.
please, point me what to do!
many thanks in advance.
Every java.awt.Shape has a contains() method that you can use for hit testing in your MouseListener or Mouseadapter.
Okay, no one answered so far.
I want to put it differently.
Let us suppose we have 2 square CirclePanels (extends JPanel), size 100x100 pixels, each shaped to a cicle and these 2 Components show a red cicle and a blue circle.
And let us suppose that the CirclePanels are transparent in the regions outside the colored circles.
And let us furthermore suppose that the hosting frame has Null-Layout - so the red circle partly overlaps the blue circle.
In order to make these circles behave like real Controls we have to fix at least these issues:
1.) a MouseClick outside the circles but very very close to the circle's border must ONLY trigger the frame's MouseListener - it MUST NOT trigger the JPanel'S MouseListeners.
2.) a MouseClick inside the red area must ONLY trigger the red JPanel's MouseListener - it MUST NOT trigger the blue JPanel'S MouseListener and it MUST NOT trigger the frame's MouseListener.
3.) a MouseClick inside the blue area must ONLY trigger the blue JPanel's MouseListener - it MUST NOT trigger the red JPanel'S MouseListener and it MUST NOT trigger the frame's MouseListener.
4.) The necessary Source Code to achieve this all MUST be within the CirclePanel.java file and the frame's java MUST NOT care for these issues. This is because these shaped Controls have to function in ANY frame.
This describes the requirement differently.
Can anybody, please, point me now, how it can be done in java.
yours, Gerald
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
In a board game we are developping in Java we would like the gui to be overshadowed when the game is finished. We have a Jframe in which there is JPanel with the board on which there are some colored pawns and boxes (JButtons) and we would like that everything becomes a sort of black and white and grey. Is there an authomatic method in Java to do this in Java Components?
There are several different kinds of panes to look at that could achieve this, or something similar, if you are using Swing (which I assume from the tag, that you are).
You could use a Glass Pane. http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
glass panes could be thought of as like a CSS overlay. They dis-allow interaction with components behind them.
Also, take a look at JXLayers and JLayers, they allow you to modify the way components are actually painted.
Good Luck!
Override paintComponent() (Or if you have some components added to the container it's better to override paintComponents() method).
Call
super.paintComponent(g);
Color semiColor=new Color(0,0,0,128);//the last param represents alpha
g.fillRect(semiColor);
For my current project I have the situation that I need an interactive element that will be drawn on a custom graphics context and allows user interaction (e.g. mouse clicks).
Since this elements will be quite complex I decided the best solution would be to use a JPanel for layouting and manually paint it and dispatch mouse and keyboard events.
Currently my plan is to:
call setSize and setLocation on the Jpanel
call paint with the graphic context
Catch desired events (in this example clicks) create a new MouseEvent and pass it on with JPanel.dispatchEvent().
I suspect there will be many traps and edge cases along the way so
a.) Is this the correct approach or is it missing something fundamental?
b.) Is there any existing library which could be uses?
This is a bad idea, you are throwing away half of what Swing does for you and then trying to re-implement it yourself.
If you really must do this then something like this: How to make canvas with Swing? is probably your best way forwards. Really I would look to see if you can instead do this just by building the screen from custom controls though.
i wanted to ask, if somebody might have a solution about a problem i face. I am working at an application, which draws an animation - for instance a map with objects moving onto. My problem is, that on top of the drawing, a Jtable, Jlist as well as other Components are also placed.
In my particular example all of those components have been added to the Panel, which holds the map. In result each component gets redrawn as often as good my fps is. Therefore making one of the tables invisible reduces the already high cpu usage of sometimes around 50% to less than 30%.
My question is, how can i avoid calling somewhat static visual contents paintComponent() method, without having the "background" - the map - whited out the menu.
Since the animation redraws permanently the menu is not shown at all, if its separated from the corresponding JPanel.
First thoughts move into following directions:
Clipping - actually not as good as i would like to, since id like to enable moving around the menus.
JLayeredPane - already tried but seemed to turn out, that the paintComponent method of menus still gets called frequently.
JWindow/Internal Frame - had that thought a couple of minutes ago. Having a complete independent container shall be able to handle my regard, or?
I am looking forward, if somebody has an elegant idea, how to fix that and reduce the cpu usage significantly.
Thanks!!
Best regards.
I would create a custom Shape for clip. Use Area class and subtract from the Area all the children components' bounds.
For painting over JComponent(s) placed into JPanel you have look at
JLayer (Java7) based on JXLayer(Java6)
GlassPane, notice all JComponents must be lightweight, otherwise is GlassPane behind heavyweight (J)Components
is possible painting to the JViewport,
EDIT
you have to use Swing Timer for moving with Icon (in the JLabel) placed into JXLayer, GlassPane or JViewport, don't use Runnable#Thread and never, not by using plain Thread.sleep(int)
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.