how can one make a JComboBox dropdown when it is given focus? - java

I find that a useful way to draw attention to a jcombobox when one wants the user to select from it is to make it drop down at the point it gains focus usually when the previous item has been completed by the user.
How can this been done in Java?

You could do:
comboBox.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
comboBox.showPopup();
}
});

You want JComboBox#setPopupVisible
Add in a FocusListener to monitor for focus gained and you should be right.
Depending on if the combo box is editable or not, you may need to add a focus listener to the editor as well

rightclick on the combo box. go to events ---> mouse ----> mouseentered.
it will take you to :
private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt) {}
inside the curly braces, type: jComboBox1.showPopup();
it should look like:
private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
jComboBox1.showPopup();
}

Related

adding multiple textfields to a single focus listener

i am trying to add multiple text fields in to a single focus listener to be executed, currently i have added a text field but i cant figure out how to add multiple text fields to it,
i think its way better than creating a focus listener for each textfield.
Usually tha anser is: "it depends..."
but in your case Id say: each textfield should have its own listener (instance).
The reason is that you change the textfields content.
When you do this in only one Listener instance then you either change all the textfields any time the event occurs regarless of which textfield was affected, or you end up with a if/else cascade needing a new entry if you add another textfield.
I suggest that you create a named inner class for the Listener giving it a Textfield as parameter:
class MyTextFiledFocusListener implements FocusListener {
private final JTextFiled textField;
MyTextFiledFocusListener(JTextFiled textField){
this.textField = textField;
textField.addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
textField.setText("");
}
public void focusLost(FocusEvent e)
{
// nothing
}
}
and the bit you posted changes to:
new MyTextFiledFocusListener(JT_id);
Just for the records:
quite a lot Swing related interfaces with more than one method have default implementations with empty methods (the pre java8 solution for default methods) so does FocusListener. These default implementation are usually called *Adapter. Therefore you could shorten your code when you extend FocusAdapter insted of implementing FocusListener:
class MyTextFiledFocusListener extends FocusAdapter {
private final JTextFiled textField;
MyTextFiledFocusListener(JTextFiled textField){
this.textField = textField;
textField.addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
textField.setText("");
}
}
What about something like this?
FocusListener clearFields = new FocusListener() {
public void focusGained(FocusEvent e)
{
JT_id.setText("");
JT_name.setText("");
JT_add.setText("");
JT_cno.setText("");
JT_email.setText("");
}
public void focusLost(FocusEvent e)
{
// nothing
}
});
JT_id.addFocusListener(clearFields);
JT_name.addFocusListener(clearFields);
JT_add.addFocusListener(clearFields);
JT_cno.addFocusListener(clearFields);
JT_email.addFocusListener(clearFields)
You will still need to add a focus listener to all of the text-fields to avoid the situation where clicking/focusing on the first one (JT_id) will clear all of them while clicking/focusing on the others would do nothing.
Or, you know, the alternative - assign each textfield a focuslistener that only clears that field.

Netbeans - delete auto generated Button Parameter

how can i delete the auto generated java.awt.event.ActionEvent evt in Netbeans when i double click in Swing on a Button?
Here is a sample from a auto generated Button-Click. How can i delete the unused parameter? Sadly the Codeline is grayed out in Netbeans.
private void sampleActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
thanks in forward!
" How can i delete the unused parameter?"
The answer is, you Don't want it deleted. The parameter is necessary. Why? If you look in the uneditable auto-generated initCompoents(), you will see something like this
jButton1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
jButton1ActionPerformed(e);
}
});
And the editable method they create for you is the
private void jButton1ActionPerformed(ActionEvent e) {}
which requires the ActionEvent to be passed to it
Go to this
delete java.awt.event.ActionEvent evt
Go to Design and right click on button and select properties then click on Events and remove the unwanted one which you added wrongly.

Is there a way to disable all buttons with a method?

This is a lab for school that I'm struggling with, the code is making a game of hangman, and when the "brain" program says the game is over, all of the letter buttons are supposed to be disabled.
relevant code sections:
the buttons:
class ActionButton extends JButton implements ActionListener{
private String name;
private char t;
public ActionButton(String s){
super(s);
name = s;
t = name.charAt(0);
}
#Override
public void actionPerformed(ActionEvent e) {
ido.newLetter(t);
this.setEnabled(false);
LovesMePanel.this.update();
}
}
the update method:
public void update(){
answers = ido.getAnswer();
flower.setTriesLeft(ido.getTriesLeft());
progress.setText(answers);
if(ido.gameOver()){
// This is where I need to deactivate the buttons
if(ido.hasWon()){
}
}
else if(triesLeft == 0){
}
}
the buttons are all created in a loop in the LoveMePanel that holds all of the other panels. Is there a way to reference them all or disable them all when the game is over?
If not, how should I change my code so that it would be possible to do that?
If you put your buttons in a Collection, you can iterate through them and disable them all that way. I.e.,
for (JButton b : myButtons) {
b.setEnabled(false)
}
If not, you have 26 disable statements to write.
See the setEnabled() method for JButton. You can:
Add your Buttons to an ArrayList while creating them and then iterate over it and disable one by one
Get children of a JPanel, iterate over them, check if it's a button and disable it
Put a Glass Pane on top of your Burrons to intercept the incoming events
Feel free to choose the one you like best.
how about getting the children of the root panel by calling getComponents and iterating over them recursively and finding JButtons and disabling them as you find them?

Detecting JComboBox editing

I have a JComboBox, once every second I want to retreive a set of strings from a database and set those strings to the contents of the JComboBox, and one of them as the currently selected value. But I also want the user to be able to edit the JComboBox and add a value to the database and set it as the current value.
I want to the be able to detect when characters are entered into the JComboBox, so I can reset a count down which prevents updating the JComboBox as long as it's not zero. My first instinct was to use a KeyListener but the Java tutorial on combo boxes says this,
Although JComboBox inherits methods to register listeners for
low-level events — focus, key, and mouse events, for example — we
recommend that you don't listen for low-level events on a combo box.
And they go on to say that the events fired may change depending on the look and feel.
This is a little dicey, but it should work to listen to the Document updates on the Editor component (A JTextField).
JComboBox cb = new JComboBox();
Component editor = cb.getEditor().getEditorComponent();
if (editor instanceof JTextField) {
((JTextField) editor).getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent documentEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void removeUpdate(DocumentEvent documentEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void changedUpdate(DocumentEvent documentEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
}
Those *Update(DocumentEvent documentEvent) methods should get called for every character typed/deleted from the JComboBox.
I would like to add that the changedUpdate method will not fire a notification for plain text documents. If you are using a plain text text component, you must use insertUpdate and/or removeUpdate.
I recently had to use a document listener as a way of disabling/enabling a button if the user was editing the combo box. I did something like this and worked very well:
public class MyDocumentListener implements DocumentListener
{
#Override
public void insertUpdate(DocumentEvent e)
{
setChanged();
notifyObservers(true);
}
#Override
public void removeUpdate(DocumentEvent e)
{
setChanged();
notifyObservers(false);
}
#Override
public void changedUpdate(DocumentEvent e)
{
// Not used when document is plain text
}
}
Then, I added this listener to the combo box like this:
((JTextComponent) combobox.getEditor().getEditorComponent())
.getDocument().addDocumentListener(new MyDocumentListener());
This works because the document associated with the combo box is plain text. When I used changedUpdate it did not.

How to enter data to swing.JTextField without pressing the enter key?

I'm building a Gui using net beans (Java) and my question is how to get the string in the text field to the variable without pressing the Enter key?
I wrote this code:
private void idTextBoxActionPerformed(java.awt.event.ActionEvent evt) {
this.Id = evt.getActionCommand();
}
The problem is that if I just enter the text and move to the next text field, the data doesn't go into the Id variable, if I press Enter everything OK.
Pressing Enter invokes an ActionEvent from that textfield which you listen for in your actionPerformed method and that is why your code only works in that scenario.
You could use a FocusListener to acheive what you want. You will want to listen for the focusLost event, which is when you move away from the textfield.
class foo implements FocusListener {
JTextField textField = new JTextField("A TextField");
textField.addFocusListener(this);
public void focusGained(FocusEvent e) {
// Do whatever you want
}
public void focusLost(FocusEvent e) {
// Save the text in the field to your id variable
}
}
EDIT
The following tutorial shows how to use a formatted textfield. You can ignore the formatting bit and focus on the propertyChangeListner aspect of it.
The idea is the same as my first example but using a different type of listener.
You could use the action event or focus event to track the changes at component level. But if its the text changes that you are interested in, then you should consider using a DocumentListener. Read the tutorial here

Categories