Java – Check if JTextField is empty or not

For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not. Below is what I found from this thread. yourJTextField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent … Read more

JTable – Selected Row click event

Here’s how I did it: table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event) { // do some actions here, for example // print first column value from selected row System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString()); } }); This code reacts on mouse click and item selection from keyboard.

Java JTextField with input hint

You could create your own: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); final JTextField textFieldA = new HintTextField(“A hint here”); final JTextField textFieldB = new HintTextField(“Another hint here”); frame.add(textFieldA, BorderLayout.NORTH); frame.add(textFieldB, BorderLayout.CENTER); … Read more

Detect enter press in JTextField

A JTextField was designed to use an ActionListener just like a JButton is. See the addActionListener() method of JTextField. For example: Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.out.println(“some action”); } }; JTextField textField = new JTextField(10); textField.addActionListener( action ); Now the event is fired when the Enter key is … Read more

Value Change Listener to JTextField

Add a listener to the underlying Document, which is automatically created for you. // Listen for changes in the text textField.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(); } public void removeUpdate(DocumentEvent e) { warn(); } public void insertUpdate(DocumentEvent e) { warn(); } public void warn() { if (Integer.parseInt(textField.getText())<=0){ JOptionPane.showMessageDialog(null, “Error: Please enter number … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)