Java Swing Buttons - java

So I have three buttons: add, edit and save. I am using ActionListener and getActionCommand to determine which button was pressed.
If I press add, then save, it has to add a new person in my table. If I press edit, then save, it has to edit that person in the table.
Is there any way to determine which button was pressed before save so that I know which way I go in Save button?

Yes. Store which button was pressed in an instance variable in your class (I don't mean in your listener class).

by using getSource() we can do it
and add some conditions like flag=1 in add button, flag=2 in edit button
if flag=1 then add new record to a table
if flag=2 then edit existing record.

A better way than using ActionListeners directly is to use Actions:
http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
I always felt like getSource was a big hack, don't know why you would ever need to use it.

Related

Is there any way to press ENTER by clicking jButton to pass a event to a jTextfield?

I have coded for multi Jtextfields in my project and used KeyPressed Events for each fields for Enter Key. Now I use a Touch Monitor.And now I want to Use a button and do the same thing by pressing Enter key of the key board.
(Not want grabfocus() or any other solutions. Just want to know is there any way to Press Enter Key by a code and run code each fields I coded.)
Example fields:
Here is an example frame. If my cursor is on field 1 I want to run the code I use with Event.getKeyCode()==10. If my cursor is on field 2 I want to run the code I use with Event.getKeyCode()==10. If my cursor is on field 3 I want to run the code I use with Event.getKeyCode()==10. If my cursor is on field 4 I want to run the code I use with Event.getKeyCode()==10.
The question is very unclear but perhaps you are looking for an ActionListener to be attached to a button.

How to select a JRadioButton from ButtonGroup?

I try to do an image creator program in java (with squares/circles/etc)
I have a few JRadioButtons in a ButtonGroup that symbolizes my program's "mode" (if I draw a circle, something else/if I move the objects).
When I click on different modes, the "mode" changes and I'm able to do what I want.
My problem is when I try to change the mode by double-clicking on an object. I do it in a MouseListener. I'm able to select the object, to change the "mode", but I can't change the selected JRadio Button on my ButtonGroup.
I searched for a while (since the setSelected() is not working). I know that ButtonGroup can have only a button selected at once. How could I deselect the curent one and select the one I need (the first one).
Thank you for any advices.
From the docs:
public void setSelected(boolean b)
Sets the state of the button. Note that this method does not trigger
an actionEvent. Call doClick to perform a programatic action change.
As mentioned here use:
radioBtn.doClick();
I created a small method that allow me to set any radio group button. Very convenient if you don't want to use if for any radio button.
public void setButtonGroup(int rdValue, Enumeration elements ){
while (elements.hasMoreElements()){
AbstractButton button = (AbstractButton)elements.nextElement();
if(Integer.parseInt(button.getActionCommand())==rdValue){
button.setSelected(true);
}
}
}
then
setButtonGroup(yourValue, yourButtonGroup.getElements());

JTable: double-click should keep prior selection

I have a JTable where I can select one or more cells. I also want to react on double-click for doing some extra action for the selected cells. But the problem is, when the user double-clicks, the selection changes to the clicked cell. But I want to keep the prior selection on double-click, so I can handle the double-click for all selected cells.
EDIT:
Related to this question:
Java : ignore single click on double click?
But I hope, there is a better/easier solution for my case.
The problem is, that on the first click the first event goes out. A bit later the second click might come or not. So the first click event does know nothing. As in the proposed solution a timer might do.
What also might do is on the first click to select nothing, but invoke a special selection event a bit later.
SwingUtilities.invokeLater(myRunnable);
and on handling the double click/myRunnable the true selection. Timing might be unavoidable though.
you can use setClickCountToStart() for XxxCellEditor, I don't know something about your JTable

How do I make a listener that fires when the USER selects an item in a JComboBox

I'm looking for a listener that fires ONLY when the user, the one who's using the program, selects an item in the JComboBox. I don't want to use ActionListener or ItemListener because those also fire when I select an item through the program. And I can't use MouseListener either because it only fires when I click the JComboBox, not when I select an item.
I was wondering what the easiest way to do this is? Currently, my solution is messy. When I change the selected item of the jcombobox through code, I set a flag to true. And in my action listener, it only executes if the flag is false.
A) I would recommend you to temporarily remove the listener when you perform the selection programatically.
B) If your programatic change is not an effect of another GUI event you could solve it the following ugly/non-robust/error-prone/"hacky" way: Check EventQueue.isEventDispatchThread() to find out if the click was triggered by the GUI thread (the user).
C) (Oops I just reread your question and saw that you've already discovered the method described below. Basically I would say that this (or the the method described above) is your best alternative.)
Another option is to have a boolean flag called something like nonUserSelection which you set to true before you select a value programatically and reset to false afterwards. In the action listener you simply add an
if (nonUserSelection)
return;

Embedding JTextArea into a JMenuItem

We are building our own Web Browser in Java. It doesn't have to be anything too complicated, we just want several things to do so we could distinguish our project from others.
What we want to know is this:
Is it possible to somehow embed a JTextField object into a JMenuItem?
To be more precise, we want our address bar (text field) to show when we click on a certain item within our menu.
Well, menu items are used for invoking an Action which causes the menu to close.
So you would just have your menu item Action display a popup dialog or something like that.
Or, if you really want to keep the popup open, the you could probably create a sub menu and then just add the JTextField to the sub menu. Remember, you can add any Component to JMenu.
Or, if you just want the text field to display on the main menu, then you just add the text field to the main menu. Of course you would lose functionality like being able to use the keyboard to navigate up and down the menu list.
All the suggestions are a hack at best, so I agree with the original comment to your question.
It's done in the standard OS X "help" menu, so I wouldn't call it an interface "quirk".
A quick google turned up this old link from 2004:
http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2004-12/0149.html

Categories