Selected Item from comboBox is always the same one [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've created a JComboBox that gets populated with an arraylist of strings. My comboBox gets populated correctly just as I want. My problem comes when getting the selected item: As I print it in a btnGo.addActionListener I realize that it is always the same one selected, even though I change the selection and click Go again. Is there a way to make the selection actually change? And can I do the same without having to click Go? Like just selecting the item from the comboBox and instantly do the action.
This is how I check what item is getting selected:
String selected = comboBox.getSelectedItem().toString();
And then I print selected in the btnGo.addActionListener

yes it's possible to execute an action when a comboBox element is selected. You just need to add an ActionListener to your comboBox ; here is a sample code :
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JComboBox comboBox = (JComboBox) event.getSource();
Object selected = comboBox.getSelectedItem();
if(selected.toString().equals("item1"))
field.setText("AA");
else if(selected.toString().equals("item2"))
field.setText("BB");
}
});
getContentPane().add(comboBox);
getContentPane().add(field);
}
Please share source code in order to help you with the selection issue.

Related

GWT: how to check the selection event for a CellTable with a SelectionModel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
We use a sortable CellTable with two columns, with a SingleSelectionModel.
Now one of the columns is filled with ButtonCells.
And the problem is: if I click the first time directly on the button of a row, only the row gets selected and it doesn't directly fires the Event for the Button. I have to click a second time.
How can I change this? How can I directly fire the event of the Button?
You can set a FieldUpdater on your Button Column:
column.setFieldUpdater(new FieldUpdater<Auction, String>() {
#Override
public void update(int index, Auction object, String value) {
// The user clicked on the button for the passed auction.
}
});
The CellSampler from GWT's docs has a direct example of clickable buttons. Cheers.
Another option is is overriding the Button's onBrowserEvent() method.

Redesigning a Java Radio button [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a student and still learning a lot about Java, but I have a pretty good grasp on the basics. Right now I am trying to figure out a way to recreate a radio button to mesh with the UI design for an application I have been commissioned to build.
The entire application is meant to be designed around the Star Trek LCARS interface (Bold bars, distinct color changes, no check boxes or radio buttons). I need to incorporate radio buttons into the design, but I have been unable to find any resource that will allow me to design a radio button without that distinctive filled/empty circle.
I already have a good idea of how I need to work the design. The selected item will change to a different display color with the mouse click or touchscreen tap. Selecting another item in the same group would deselect the original and select the new option, while trying to select the same item would unselect all choices (Select an undisplayed, "default option").
The problem is pure aesthetics, but it is important to the customer.
Is it possible to create a label (or other component) and have the ActionListener "transfer" the user interaction with the object to a radio button? In other words, the user clicks on the selection label and it fires off the same commands (change item/text color, play sound) and also fires off a command to set a designated radio button to either on or off?
well first of all... Everything is possible..its just a matter of how much time/effort you want to put in.
If I read that right..You have suggested creating a hidden radio button that gets information passed to it. This would work but is probably not the best way to design it.
I would personally just make an integer and call it mostRecentLabel or something to that effect and if you have 4 labels you would just keep track of which label was "turned on" with your mostRecentLabel variable. Then just put a function in that responds whenever a label is clicked to update your mostRecentLabel and this would update the display accordingly

How to save input taken through text field using a save button using net beans? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a beginner in programming and using net beans java for my work. can someone please tell how can I save input data that I have taken from user using text field in the gui? Here is the code for input:
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
}
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)
{
char b_no = evt.getKeyChar();
if (!(Character.isDigit(b_no)) || (b_no==KeyEvent.VK_BACKSPACE) ||b_no==KeyEvent.VK_DELETE))
{
getToolkit().beep();
evt.consume();
}// TODO add your handling code here:
}
I need help on how I should save this input
If you're trying to add a KeyListener to a JTextField, then don't. This should never be done as it will mess up the functionality of the JTextField. Instead if you're trying to limit input to text, then consider using :
a JFormattedTextField with a decent MaskFormatter. For example, formatted text field tutorial, or
add a DocumentFilter to your JTextField's Document to limit the input to numeric or
Use an InputVerfier to verify that the input is OK
Or allow any input and verify it simply on JButton press or enter press using an ActionListener. This is the route I'd go as it's the most definitely the easiest to implement. Here in the ActionListener's actionPerformed you'd get the text from the field via myField.getText(), check if it is valid by whatever test you wish, and if not valid, clear the text via myField.setText("") and send an error message to the user via JOptionPane.sendMessage(...) dialog.

Make radio button mandatory in JSP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Can somebody please help me to create a mandatory radio button in JSP?
Idea is to prompt the user to check the radio box when he parses through the page, and once he checks either of the radio button (Yes or No) we capture the value and save it to Db.
By default radio should be blank it should be blank.
Embedded a piece of Javascript in your page, upon form submission, the Javascript validates radiobutton value like this:
How can I check whether a radio button is selected with JavaScript?
if(document.getElementById('gender_Male').checked) {
//Male radio button is checked
} else if(document.getElementById('gender_Female').checked) {
//Female radio button is checked
} else {
alert('You should select a radio button!');
}

Start Editing a cell in JTable automaically

I am developing a restaurant billing system.
So here is the order panel interface
So now when i click on the menuTable the item code automatically gets added to kotTable
and when i press "Q" the focus shifts to quantity column in kotTable.
`
private void menuTableKeyTyped(java.awt.event.KeyEvent evt)
{
if(evt.getKeyChar()=='Q') {
kotTable.editCellAt(i-1,2);
}
} `
The problem is The cell doesnt start editing automatically. I need to click on that cell and then the editing starts.
i tried using DefaultCellEditor , getInputMap() and many other. But I am bit confused and the problem is not solved.. Thanx
Using a key binding, you can map the Q key to the table's "startEditing" action. More examples may be found here.
table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), "startEditing");

Categories