1

Thanks to the very helpful post by Bart Kiers in someone else's post, I have managed to get my input hints working. Hurray! Unfortunately I can't seem to get a small glitch to stop occurring.

During the normal course of operation, the HintTextFields are disabled and enabled to allow or disallow edits. The input hint should only show when the field is empty and not selected, but when I left click the boxes, even when disabled and containing text, the text disappears in favor of the hint. Then, when I click on something else, I lose the contents entirely.

I have added an extra bit to the if then statements regarding gaining and losing focus to prevent it from running that code if the component is disabled, but the problem still occurs. It must be some small interaction with the JTextField class (or maybe FocusListener?). I have also tried getParent() in favor of this in that clause, in case I was misunderstanding the 'this' keyword, but that didn't help. The class below:

class HintTextField extends JTextField implements FocusListener {

  private final String hint;
  private boolean showingHint;

  public HintTextField(final String hint) {
    super(hint);
    this.hint = hint;
    this.showingHint = true;
    super.addFocusListener(this);
  }

  @Override
  public void focusGained(FocusEvent e) {
    if(this.getText().isEmpty() && this.isEnabled()) {
      super.setText("");
      showingHint = false;
    }
  }
  @Override
  public void focusLost(FocusEvent e) {
    if(this.getText().isEmpty() && this.isEnabled()) {
      super.setText(hint);
      showingHint = true;
    }
  }

  @Override
  public String getText() {
    return showingHint ? "" : super.getText();
  }
}

Original post if anyone wants to go upvote the wonderful Bart Kiers. Java JTextField with input hint

Also, here is the initialization. This started as a JDeveloper gui.

    startDateField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    startDateField.setToolTipText(notes);
    startDateField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    startDateField.setMaximumSize(new java.awt.Dimension(2, 16));
    startDateField.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            startDateFieldMouseClicked(evt);
        }
    });

private void startDateFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startDateFieldMouseClicked
if (SwingUtilities.isRightMouseButton(evt)) {
    editNote();
}
}


public void editNote() {
    int selected = -1;
    for (int a = 0; a < spinner.bitem.bldg.stallArray[1].length; a ++) {
        if (spinner.bitem.bldg.stallArray[1][a].equals(stallName)) {
            selected = a;
        }
    }
    String oldNote = notes;
    JTextField xField = new JTextField(oldNote);
    xField.setPreferredSize(new Dimension(200,30));

    JPanel myPanel = new JPanel();
    myPanel.add(new JLabel("Note:"));
    myPanel.add(xField);
    int result =
        JOptionPane.showConfirmDialog(this, myPanel, "Please enter note.",
                                      JOptionPane.OK_CANCEL_OPTION);

    if (result == JOptionPane.OK_OPTION) {
        String note = xField.getText();
        if (!note.equals(oldNote)) {
            notes = note;
            spinner.bitem.bldg.stallArray[9][selected] = notes;
            spinner.bitem.master.sendNoteDataToSQL(spinner.bitem.bldg.buildingName, spinner.bitem.bldg.stallArray[1][selected], notes);
            spinner.updateScreen();
        }
    }

}
Community
  • 1
  • 1
Peter F
  • 435
  • 1
  • 8
  • 17
  • Is there anything that puts data in the field programmatically? – RealSkeptic Feb 22 '15 at 19:05
  • No. The only other methods called on them are setEnabled, setToolTipText and getText. It should be noted that this is a converted JDeveloper gui. I have added it's initialization block above as it is complicated-ish and I didn't write it. – Peter F Feb 22 '15 at 19:14
  • So what's in the `startDateFieldMouseClicekd` method? – RealSkeptic Feb 22 '15 at 19:16
  • And `editNote()`? Please include every code referred. – RealSkeptic Feb 22 '15 at 19:21
  • That's a rather big operation to put in the event dispatch thread. Perhaps there are others like it, that cause events to back up. I suggest you create an [MCVE](http://stackoverflow.com/help/mcve), as it is pretty impossible to guess what's going on at the moment. – RealSkeptic Feb 22 '15 at 20:11

0 Answers0