Krantik Chavan has Published 308 Articles

I want to call JButton doClick() method to simulate a click action in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

931 Views

Let us first set a JButton:JButton btn = new JButton("DemoButton");Now, attach action listener:btn.addActionListener(new ClickListener());If you have an ActionListener attached to your button it'll fire when you call the method doClick():btn.doClick();The following is an example to call JButton doClick() method to simulate a click action:Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import ... Read More

How to change JButton font dynamically in Java?

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

840 Views

The following is an example to change JButton font dynamically:Exampleimport java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo extends JFrame {    JButton button = new JButton("Change");    int fontSize = 10;    public SwingDemo() {       setSize(500, 400);       setDefaultCloseOperation(EXIT_ON_CLOSE);   ... Read More

Java Program to create JCheckBox from text in Swing

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

114 Views

The following is an example to create JCheckBox from text in Swing:Exampleimport java.awt.FlowLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(String[] args) {       JCheckBox checkBox1 = new JCheckBox("Cricket");       JCheckBox checkBox2 = new JCheckBox("Squash");       JCheckBox ... Read More

How to disable JCheckBox if not checked in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

550 Views

The following is an example to disable JCheckBox if not checked in Java:Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JCheckBox; import javax.swing.JOptionPane; public class SwingDemo {    public static void main(String[] args) {       JCheckBox checkBox = new JCheckBox("Demo", true);       checkBox.addActionListener(new ActionListener() {         ... Read More

How to get or set the selection state of JCheckBox in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

240 Views

The following is an example to get or set the selection state of JCheckBox:Exampleimport java.awt.FlowLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; public class SwingDemo extends JFrame {    public SwingDemo() {       setSize(500, 500);       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       setLayout(new FlowLayout(FlowLayout.CENTER));       JCheckBox checkBox = new ... Read More

Handle JCheckBox Events with an ItemListener in Java

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

1K+ Views

Here, we have used ItemListener to handle JCheckBox events i.e. whenever any of the CheckBox is selected.For example; When any of the sports like Football CheckBox is checked, event is fired and a message is visible in the botton.The following is an example to handle JCheckBox events with an ItemListener:Exampleimport ... Read More

How to set Mnemonic key for selection of each JCheckBox in Java?

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

1K+ Views

Mnemonic key is set so that a user can use Keyboard keys to check a CheckBox. For example, a key can be set with ALT:checkBox1.setMnemonic(KeyEvent.VK_F); checkBox2.setMnemonic(KeyEvent.VK_T); checkBox3.setMnemonic(KeyEvent.VK_R); checkBox4.setMnemonic(KeyEvent.VK_C); checkBox5.setMnemonic(KeyEvent.VK_A);Above, we have set key ALT+F for checkbox 1, key ALT+T for checkBox2, etc.The following is an example. Here, we have set ... Read More

How to align JLabel text vertically top in Java?

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

2K+ Views

Use the setVerticalAlignment() method to align JLabel text vertically to the top:JLabel label = label.setVerticalAlignment(JLabel.TOP);The following is an example to align JLabel text vertically to the top:Exampleimport java.awt.Color; import java.awt.Font; import javax.swing.*; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame ... Read More

Java Program to disable the first item on a JComboBox

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

720 Views

To disable the first index, on the click of a button, let us implement for index > 0 i.e. item 2:System.out.println("Index 0 (First Item) is disabled... "); comboBox.addItemListener(e -> {    if (comboBox.getSelectedIndex() > 0) {       System.out.println("Index = " + comboBox.getSelectedIndex());    } });By implementing above, we ... Read More

How to hide and display JCombobox with a JCheckBox in Java?

Krantik Chavan

Krantik Chavan

Updated on 30-Jul-2019 22:30:26

412 Views

To toggle visibility with JCheckBox, use isVisible() method:JCheckBox toggleVisibility = new JCheckBox("Hide/Show"); toggleVisibility.setSelected(comboBox.isVisible()); toggleVisibility.addItemListener(e -> {    comboBox.setVisible(e.getStateChange() == ItemEvent.SELECTED); });The following is an example to hide and display JCombobox with a JCheckBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ItemEvent; import javax.swing.*; public class SwingDemo {    JFrame frame;    SwingDemo(){   ... Read More

Advertisements