I am making a matching game and want to only allow the user to select two cards (JButtons with images). I have 16 jButtons and was wondering on how I would restrict the user to only select 2 of those JButtons.
How the user plays:
They press the play button.
Select 2 cards and then press the guess button to check if they are the same. (This is where i want to only allow the user to select 2 cards)
You can disable buttons like this
jButton.setEnabled(false);
So disable the ones you don't want the user to select, or disable all of them and then enable the ones the user can select.
I'm a beginner with events in java, but I would try this based on the explanation you gave:
In your event listener for the JButton create a counter that starts at zero and counts up one each time the event listener is executed. As long as the counter is less than two, the rest of your event listener code should run (i.e. registering a card as selected). Once the counter reaches two, use Jim W's code jButton.setEnabled(false); on the rest of the buttons until the user clicks guess.
Related
I am writing a Java program to record some events and their status, i.e., done or not. I have implemented the recording and calculations, but I also want to have a button that will open another window that will list the events with a checkbox corresponding to each of them, so I can pick which ones' statuses I will change to done. I have only used Swing libraries so far.
I want this window to contain one event per line, with a checkbox next to the event. Each checkbox should determine whether the status of the even should be changed or not. I had trouble even making such a window open and list the items, let alone put the checkbox.
Note: The design is not too important. This won't be a published project, I will mostly use it for personal work. I can do with buttons instead of checkboxes, where any click changes the status of the event next to it.
Thank you in advance for your valuable responses.
I've spent over a day now trying to figure this out. I've tried MouseListener, ItemListener, and ActionListener examples I've found, but none seem to do what I need.
Here's what I've got: I've got a JComboBox that works well. The user can type in the text box and it autocompletes, and there's a drop-down and everything. The purpose of the box is for performing searches. The user types a search and hits enter, which causes the search results to be highlighted in the main pane. The user can also arrow through the drop-down and initiate the result highlight by hitting enter from the dropdown.
Here's the problem: The user can click an item in the dropdown under the text field and the text field fills in with that value, but the search results are not highlighted. The user still has to hit enter after having clicked what they want.
Here's what I want: I want to detect the moment the user clicks the dropdown menu item they want so that I can use that event to call my method which highlights the result in the main pane. I don't want them to have to hit the enter key after clicking an item in the dropdown.
I've got a key adapter that catches the enter-key press to call the method which highlights results and it works well. The method it calls is called seekAll().
When I tried the mouse listener, it only seemed to work on the text field and not the dropdown. When I tried the ItemListener, I could not distinguish between items being highlighted/hovered/arrowed across and the click making the drop-down menu selection. The result of the action listener was similar to my attempt with the ItemListener.
Here's an example of the ActionListener I tried:
public abstract class HeaderFinderBox {
private WideComboBox searchTermBox;
public HeaderFinderBox() {
final String[] labels = setupData();
//just returns an array of strings
this.searchTermBox = GUIFactory.createWideComboBox(labels);
searchTermBox.setEditable(true);
searchTermBox.setBorder(null);
searchTermBox.setBackground(GUIFactory.DARK_BG);
AutoCompleteDecorator.decorate(searchTermBox);
//This adds my key listener
//searchTermBox.getEditor().getEditorComponent()
// .addKeyListener(new BoxKeyListener());
searchTermBox.addActionListener(new SearchListener());
}
class SearchListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
LogBuffer.println("Action performed on item from the combo box");
//seekAll();
}
}
}
I commented the call to seekAll() because I get null pointer exceptions when the you type your first character into the text box and the field blanks. With that call commented out, I get the "Action performed on item from the combo box" message whenever these things happen:
Twice for each character typed on the keyboard
Once when you hit enter
3 times when you click an item in the dropdown
3 times after arrowing to an item in the dropdown and then hitting enter
The only one of these that I want a single hook for doing something is 1 firing upon the user clicking an item in the dropdown. That would be number 3 in the list above, but it fires 3 times for a single click. And how do I differentiate that event from all the others (1, 2, & 4)?
If ActionListener is supposed to be able to ONLY give me a single actionPerformed upon click of an item in the dropdown, then what am I doing wrong?
I am learning android programming so be cool with me.
My question is, i am having for example 5 buttons.User clicks any one button out of 5 and clicks another button.So how to keep track of previously clicked button ids.So the 2nd button clicks output is based on the previous button.
Can any one throw me pointers on that.I am new to java and android.
I will recommend you to follow (Screen State) base approach; as below:
1.) Create Screen State class and assign each state a unique value.
2.) You should mention Screen state stack and the current screen state.
3.) when you press a button, call a OnstateChange() function. Which should determine the next screen (based on current screen and whether user moved forward or backward). If user moved backward, pop the screen from stack and mark it as current screen.
If you need to remember only the very last button you clicked, use a class member variable to remember something about it (the button's ID, or a numeric index, or something). You should set this member variable in the onClick() call corresponding to the button.
If you need to remember the entire history of clicked buttons, use a List implementation (ArrayList should be fine) and add() the information about the button (the button's ID, or a numeric index, or something) in the onClick() method corresponding to the button.
If you have different OnClickHandler's for each button, this is pretty straightforward. If you share an OnClickHandler, you will need to use some identifying property of the View you get passed in as an argument to onClick().
I need to know the events occurred in my web app, from the time it was loaded. Is there any way to get the events, The reason is I need to store those events is when ever user wants to go directly to result which he got after performing few actions, I can do PostEvent on series of events to reach that particular result, or is there any other way to do the same with out storing events?
Example:
User clicks on button1 -button1 is disabled,
User Click on button2 - button2 is disabled,
User clicks on button3 -button3 is disabled,
User Click on button4 - button4 is disabled,
Now user has performed 4 actions to get all the four button disable.
If user wants to reach the state when 1st 3 buttons I will do PostEvent of clicking 3 buttons for him.
Note Example is not exactly what I need. It's just for explanation purpose.
There's no support for this in GWT, you have to do it yourself. Record every action/event you're interested in, possibly using the command pattern, and log all of them for later replay.
In ZK , you could use Event Interceptor to log all event you need.
http://books.zkoss.org/wiki/ZK_Configuration_Reference/zk.xml/The_listener_Element/The_org.zkoss.zk.ui.util.EventInterceptor_interface
I'm developing a SWT app and in one particular form there are 14 pairs of Yes-No radio buttons. Each of these pairs have a text box associated with them. So if a user selects Yes, the associated textbox should be editable else uneditable. I find writing 28 listeners for the radio buttons really daunting. Since the radio buttons have nothing much to do than just rendering the textbox editable/uneditable I was hoping if there were some generic type of listeners in SWT that would be applicable to a set of radio buttons specified in an array or like that. Are there any frameworks or shall I have to write individual listeners?
Edit
I'm trying to fire an event only when the radio button is selected
rdoExperience.addListener(SWT.CHECK, new RadioButtonSelection(
txtExperience));
but SWT.CHECK is causing the event to be fired on mouse hover over radio button too. I've tried using SWT.SELECTED too but it's not working either and I can't find other suitable SWT constants. W;hat should I use?
Good point. Sorry I don't know such thing.
However, you create one yourself: Instead of writing an anonymous listener for every button, you could write one - say MyButtonListener - and give it the button text box as an argument. Than you instantiate MyButtonListener with the appropriate text box as an argument. Than in the Listeners appropriate callback method you enable or disable the text box.
Edit: My bad. Of course I meant you could give it your text box like radioBtn.addListener(SWT.SELECTED, new MyButtonListener(textfield1));
You could create one SelectionListener and add it to each of the radio buttons. Then you can ascertain which button was pressed from the selection event and map that to a text box. For the mapping you could use an array or hashtable.