How do I "combine" JButton with JComboBox? - java

How do I "combine" JButton with JComboBox and place it on a JToolbar, as shown in this image:
?

You'll want to use a custom renderer I suspect, and have it display clickable buttons (with the appropriate actions attached, etc).

Just create regular combo box and put image as an item. I did it once. Unfortunately I do not have a source code here but as far as I remember it was not a problem. You have to implement your custom 1ListCellRenderer. Its methodgetListCellRendererComponent()` should return for example Label with your image.

Related

How to create an element

I want to create this element in swing:
As you can see the element is a small grid of buttons which appears when i click on another button. I've tried to use JComboBox to create this element. But as far as i know, JComboBox can just render an image of the some button, but it will not behave itself as a button. Also I couldn't set a GridLayout to JComboBox.
I also tried to create some JDialog, but I suppose that's bad idea.
So the question is: Which swing's component should I use to create mentioned element?
You could use dialog in the best way to achieve this.
JDialog dialog = new JDialog(owner);
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.setUndecorated(true);
You could set Modality type to Modeless to avoid parent frame lock and set undecorated true to make jdialog without close option. But note that you need to close the dialog from program.

Custom JButton with JTextArea component inside

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...

Swing Custom GUI Component

I need to create a custom GUI Component about same like shown in the following image.
it has some buttons and labels on this.
How can i create like this
You could use a JWindow, with an Image for the background picture. For the buttons, use a JButton with an ImageIcon.
You can have a JFrame with nullLayout then use a JLabel with the image.
Now for buttons use setContentAreaFilled(false) and setBorderPainted(false) to remove the default button style and it will look exactly like the image that you have passed while creating the button.
To position the Frame at the center use setLocationRelativeTo(null) .
I think that should solve your problem.

how to make java run time sizable image box

I'm making a Unicode translator in Java. I did all hard parts, but now I want to add a resizable, relocatable image to the textpane. The user must be able to resize image with its corners and drag & drop the image within the textpane where he likes. (like Microsoft Word or Photoshop)
Something like this:
I tried the Styled Document properties. But I couldn't find way except inserting only an ImageIcon.
May be a better choice would be not to use textPane.
If you have an custom editable label, that can be edited by double clicking on it to show a text box to edit the contents, and change the text of the label when enter key is pressed.
Also give a shot of JDesktoppane, JLayeredPane, and check what components can be added to it.
try this
http://mgeisler.net/downloads/browser/src/ImageBox.java.html
try loading the image inside a JLabel component in JFC Swing.
Otherwise try some other non editable components in the same technology with resizable property.
try this http://sourceforge.net/projects/ird/
the iRD is a component for Resize and move(drag&drop) compoennts on runtime in java.

Is there a "Group Box" equivalent in Java Swing?

Trying to build a GUI application in Java/Swing. I'm mainly used to "painting" GUIs on the Windows side with tools like VB (or to be more precise, Gupta SQLWindows... wonder how many people know what that is ;-)).
I can't find an equivalent of a Group Box in Swing...
With a group box, you have a square box (usually with a title) around a couple of related widgets. One example is a group box around a few radio buttons (with the title explaining what the radio buttons are about, e.g. Group Box entitled "Sex" with "Male" and "Female" radio buttons).
I've searched around a bit... the only way I found was to add a sub-pane, set the border on the sub-pane and then add all the widgets in the "group" to the sub-pane. Is there a more elegant way to do that?
Create a JPanel, and add your radiobuttons to it. Don't forget to set the layout of the JPanel to something appropriate.
Then call panel.setBorder(BorderFactory.createTitledBorder(name));
Others have already commetned about JPanel and using a TitledBorder, that's fine.
However, when playing with Swing LayoutManagers, you may find it annoying that components in different JPanels cannot align correctly (each panel has its own LayoutManager).
For this reason, it is a good practice (check "JGoodies" on the web for more details) in Swing GUIs to NOT use TitledBorders but rather separate groups of components in a JPanel by a JLabel followed by a horizontal JSeparator.
Ref. "First Aid for Swing"
A Group box is just a set of 'logically grouped widgets'.
This in the swing world is a JPanel.
Add your widgets to a JPanel.
Set its border type to 'Titled Border' and give the title, same as the name of the VB6 'frame'.
Voila. You have your group box.
Here's a quote from the JRadioButton javadocs since you brought up radio buttons.
An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)
Note: The ButtonGroup object is a logical grouping -- not a physical grouping. To create a button panel, you should still create a JPanel or similar container-object and add a Border to it to set it off from surrounding components.
Not AFAIK, at least not with standard swing widgets.
In VB you have a group widget, which is essentially a panel + border.
In Swing you have a JPanel which is the container widget, and you create and set a border object on it only if you need one. One can argue that in a way that is more elegant since you don't pay for something you don't use (e.g., border)
As David Koelle mentioned about setting up border through java code, you can also achieve similar result in designer mode.
I'm responding based on the Uri's comment which explaind what the OP meant by Group Box:
Uri: I think he means the control group you see in many dialog boxes, where you have a square around a bunch of widgets such as radio buttons, for example.
As far as I know, every JComponent can set a border for itself, so you don't need a second panel.

Categories