I'm working on a project. I want to add a toolbar to the software so I put some buttons in a panel . However the default button style doesn't meet my need. I want the button to have the following effects:
When the mouse doesn't hover over the button, the button should looks like a JLabel. The icon in the button just looks like an image on the panel, i.e. all we can see is the icon in the button and other things are transparent.
When the mouse hovers over the button, the button's border appears. It looks like a real button.
Example: Just like the buttons on the eclipse's toolbar.
Why not use a JToolbar instead of a JPanel?
I got it. The answer to my question is the setContentAreaFilled() method. When the mouse hovers over the button, call the setContentAreaFilled(true). Otherwise call the setContentAreaFilled(false). Here is a relative code: link text
So, you want to customize your JButton renderings ?
First, for an all-inclusive soluition, you can take a look at existing LnF like Substance (obviously, it's a far too powerful solution for your need, however it may give you some inspiration).
Then, if you want to solve that by yourself, you'll have to override the paintComponent method.
For that, the first move is to subclass JButton.
Then, in your subclass, start by redefining the paintComponent(Graphics) method.
Notice that if all that is overcomplicated to you, you can also take a look at setBorderPainted(boolean) method.
Extend JButton and:
Just add an Icon instead of Text to
the button.
Add MouseMotionListener to capture
hovering to show/hide border.
Related
I extended JButton so it would fit my needs. I have one JTextArea and two JLabel components inside of my new class. The problem is, that I cannot click through JTextArea. So, when mouse is in JTextArea bounds, the button is not responding. There is no problem with labels.
As on screen. There are four buttons. Each one is a separate yellowish rectangle. When mouse is over JTextArea's gray rectangle, I cannot press the button. I need JTextArea because it supports multiple lines. Is there any option to make it not intercept the mouse?
It would be ok if I could attach ActionListener to JTextArea, but I can't. It cannot have this kind of listener.
You look to be trying to use a JButton in a very non-button way, including having it hold a JTextArea, and not looking at all like a button. If you want a clickable area that is not an identifiable JButton, then consider using a MouseListener instead. You would likely have to add the same MouseListener to the container JPanel and the JTextArea.
Take a look at Concepts: Editors and Renderers to understand the difference between a renderer and an editor.
A renderer is a "rubber stamp" of the component, it is simply "painted" onto the surface of the JTable and is not a real life component
You would need to implement a custom editor, which could translate the trigger event (in your case the MouseEvent) into a local context.
Take a look at TableCellEditor#isCellEdtiable which is probably the closest you will get to the source of the event which might trigger the cell to become editable.
JButton could be seeded with a HTML String (`"This is some text"), which would be capable of supporting multiple lines and line wrapping as well
Having said all that, you might want to seriously reconsider your design...
I'm experimenting with JRadioButton's to put them on a JToolbar and select the last one clicked. If i'd use JButtons they wouldn't keep the Selection.
Since JRadioButton always have that Dot, I need to draw them myself by overriding the paint-methods.
The Button's will be circles with an Icon in it. That works if I draw Images, but looks aweful. The problem I have is that I would like to draw the circle so that these Buttons always look like the JButtons with the current LookAndFeel.
How can i do that? I searched for a while now, but I didn't find methods to read some default-colors of the LookAndFeel which I could use.
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
See UIManager Defaults.
I need to draw them myself by overriding the paint-methods
Don't do custom painting in the component. If you don't like the default Icons, then create your own Icon and do the custom painting there or create an Image and use an ImageIcon. The you can use the setXXXIcon() methods.
How to disable iconified button in JFrame Window ?
something like setResizable, but for minimize button
At First, you can use the method setUndecorated(boolean). It may disable the title bar and the border.
In the end, you will create the icon label and close button at your frame top or the others position.
But this way will lose the border look and feel for the frame. If you choose this way, you must create a lot of code.
In fact, If you could not use JNI, this way may be the only.
You could use a JDialog, which natively does not have a minimize button.
In fact, the minimize, close and maximize/un-maximize buttons are drawn by the Operating System itself. This means you can't really disable them within Java.
That's why my suggestion is to use a JDialog.
I am adding a button to a panel using Miglayout, and try what I might, I cannot get it to go to the right end of the panel. It insists on going flush left. Oddly, the demo is kind of short on such on a example (it only shows it in the context of other buttons on the same panel.
I have a panel like this:
dialog
->complex display retrieved from another class
OK Button here.
Except that it always insists on putting it like this:
dialog
->complex display retrieved from another class
OK Button here.
OK, I got the answer to this (finally). When adding the panel that contains the button add the component constraint of "gapbefore push." I had been trying that, but only on the button itself, not the panel.
panel.add(button, "skip(the amount of cells you have), al 100%")
I have a JTabPane, and i have added a MoustListener to it(for tab's title).
When i press right click, a popup menu is created.
i need to make it invisible when i press the mouse button in any place of window. how can i do this??
(the MouseListener is applied only for tab's title.)
i need to make it invisible when i
press the mouse button in any place of
window. how can i do this??
This is the default behavour of a JPopupMenu so you don't have to do anything special.
Read the JPopupMenu API and you will find a link to the Swing tutorial on "How to Use Menus". The tutorial contains a working example of using a popup menu. Compare your code with the tutorial to see whats different. We can't help you because we don't know what your code is like.
If you need more help post your SSCCE.
in the good old days what I did to solve this problem was to register a mouse listener with ALL the components.
you can write a fairly simple function that recursively traverses a top level container and do it.
this was with Java 1.1, so maybe there is a better option today.
One way off the top of my head would be to grab the coordinates of the clicks, and then have another method to determine if the clicks are on the tab, or inside of the content area of the tabs.