
Check whether a JLabel is Enabled or Visible.
JLabel.setEnanbled() methods enable a JLabel or make it visible in it container.If you set false, the JLabel is not visible and if set truth, the JLabel is visible. Below code creates two JLabels, one is enabled and the other is not enabled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<textarea class="java" cols="10" name="code" rows="10">/** * * @author Eric * Email:kawi3462@gmail.com * Site:www.techoverload.net */ import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; public class JLabelEnable extends JFrame { public JLabelEnable() { super("JLabel Enabled and Disabled Example"); setSize(400,200); setLayout(new FlowLayout()); JLabel label=new JLabel("Enabled Label"); label.setEnabled(true); add(label); JLabel label2=new JLabel("Disabled Label"); label.setEnabled(false); add(label2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JLabelEnable(); } } </textarea> |

JTextField setEnabled Method
Below code shows how to enable or disable JTextField added in a JFrame window.JTextField inherits its methods from the class swing.Note the differences between the two fields
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<textarea class="java" cols="10" name="code" rows="10">/** * * @author Eric * site:www.techoverload.net */ import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; public class JTextFieldExample extends JFrame{ JTextFieldExample() { super("JFrame Example"); JTextField userField=new JTextField(); userField.setBounds(10, 10, 200, 20); //using the method to disable userField.setEnabled(false); add(userField); JTextField userField2=new JTextField(); userField2.setBounds(10, 40, 200, 20); //using the method to disable userField2.setEnabled(true); add(userField2); setLayout(null); setSize(300,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new JTextFieldExample(); } } </textarea> |
See also: How to create Read Only JTextField

Enabling and Disabling JButton using setEnabled Method
Enabling and Disabling JButton can be easily done using setEnabled Method.JButton class is used to add platform independent buttons to swing programs. Other JButton Methods are:- JButton setToolTipText JButton setText JButton getText Using JButton setEnabled Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<textarea class="java" cols="10" name="code" rows="10">/** * * @author Eric * Blog:techoverload.net * This program is simple.It implements Interface ActionListener. * When the user clicks one button,the other button becomes disabled. */ import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; public class JButtonGetTexExample extends JFrame{ JButton button,button2; JButtonGetTexExample() { super("JButtonGetTexExample "); button=new JButton("Login"); //adding actionlistener that will perform action of getting text. button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { button2.setEnabled(false); } }); button.setToolTipText("Click to get Button text"); button.setBounds(10, 10, 100, 30); add(button); button2=new JButton("Cancel"); button2.setBounds(120, 10, 100, 30); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { button.setEnabled(false); } }); add(button2); setSize(300,300); setLayout(null); setLocationRelativeTo(null); setVisible(true); } public static void main(String args[]) { new JButtonGetTexExample(); } } </textarea> |
This program is simple.When the user clicks…