Krantik Chavan has Published 278 Articles

Handle JCheckBox Events with an ItemListener in Java

Krantik Chavan

Krantik Chavan

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

2K+ 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

2K+ 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

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

Krantik Chavan

Krantik Chavan

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

603 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

How to display the items in a JComboBox in Java

Krantik Chavan

Krantik Chavan

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

784 Views

The following is an example to display the first element in a JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout()); ... Read More

How to pre-select JComboBox item by index in Java?

Krantik Chavan

Krantik Chavan

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

1K+ Views

The following is an example to pre-select JComboBox item by index in Java. Here, we have selected the 3rd item by default i.e. whenever the Swing program will run, the third item would be visible instead of the 1st.Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; ... Read More

What happens when JDialog is set with Modality type APPLICATION_MODAL

Krantik Chavan

Krantik Chavan

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

308 Views

The JDialog Modality type APPLICATION_MODAL blocks all top-level windows and it has restrictions. The following is an example to set JDialog with Modality type APPLICATION_MODAL:Exampleimport java.awt.Cursor; import java.awt.Dialog.ModalityType; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) ... Read More

How can I check if there are any selected items in Java JList

Krantik Chavan

Krantik Chavan

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

753 Views

To check if there are any selected items, use the following:boolean res = !list.isSelectionEmpty();The value of res would be TRUE IF we have a selected item in the JList.The following is an example to check if there are any selected items in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; ... Read More

Java Program to check if the second item is selected in Java JList

Krantik Chavan

Krantik Chavan

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

152 Views

To check if the second item is selected i.e. index 1, use the method isSelectedIndex():list.isSelectedIndex(1);Above, we have set the list with string values:String sports[]= { "Squash", "Fencing", "Cricket", "Football", "Hockey", "Rugby"}; JList list = new JList(sports);The following is an example to check if the second item is selected in ... Read More

Create JList and always display the scroll bar in Java?

Krantik Chavan

Krantik Chavan

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

505 Views

To always display the scroll bar in a JList, use the properties HORIZONTAL_SCROLLBAR_ALWAYS and VERTICAL_SCROLLBAR_ALWAYS:JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);The following is an example to create JList and display the scroll bar:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame { ... Read More

Advertisements