- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4336 Articles for Java 8
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
417 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(){ frame = new JFrame("ComboBox"); String Sports[]={"Select", "Tennis", "Cricket", "Football"}; JComboBox comboBox = new JComboBox(Sports); comboBox.setBounds(50, 50, 90, 20); frame.add(comboBox, BorderLayout.CENTER); JCheckBox toggleVisibility = new JCheckBox("Hide/Show"); toggleVisibility.setSelected(comboBox.isVisible()); toggleVisibility.addItemListener(e ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
722 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 have disabled the first item.The following is an example to disable the first item on a JComboBox:Exampleimport javax.swing.*; public class SwingDemo { JFrame frame; SwingDemo(){ frame = new JFrame("ComboBox"); String Sports[]={"Select", "Tennis", "Cricket", "Football"}; JComboBox comboBox = new JComboBox(Sports); ... Read More
![Nancy Den](https://www.tutorialspoint.com/assets/profiles/13557/profile/60_88439-1512714529.jpg)
257 Views
For JCheckBox, use the following to set tooltip text:checkBox1.setToolTipText("Sports Football"); checkBox2.setToolTipText("Sports Tennis");Tooltip text is visible whenever you will place cursor on that particular text.The following is an example. Here, we have set the tooltip text for both the sports:Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingDemo(){ prepareGUI(); } public static void main(String[] args){ SwingDemo swingControlDemo = new SwingDemo(); swingControlDemo.showCheckBoxDemo(); } private void prepareGUI(){ ... Read More
![Nancy Den](https://www.tutorialspoint.com/assets/profiles/13557/profile/60_88439-1512714529.jpg)
3K+ Views
To change the JLabel foreground and background color, use the following methods:JLabel label; label.setForeground(new Color(120, 90, 40)); label.setBackground(new Color(100, 20, 70));The following is an example to change JLabel background and foreground color: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 = new JFrame("Demo"); JLabel label; label = new JLabel("This is demo label!", JLabel.RIGHT); label.setVerticalAlignment(JLabel.TOP); label.setFont(new Font("Verdana", Font.PLAIN, 15)); label.setForeground(new Color(120, 90, 40)); label.setBackground(new Color(100, 20, 70)); ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
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 = new JFrame("Demo"); JLabel label; label = new JLabel("This is demo label!"); label.setFont(new Font("Verdana", Font.PLAIN, 14)); label.setVerticalAlignment(JLabel.TOP); Border border = BorderFactory.createLineBorder(Color.ORANGE); label.setBorder(border); frame.add(label); frame.setSize(600,300); frame.setVisible(true); } }Output
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
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 Mnemonic key for selection of each CheckBox:Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingDemo(){ prepareGUI(); } public static void main(String[] args){ ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
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 java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingDemo(){ prepareGUI(); } public static void main(String[] args){ SwingDemo swingControlDemo = new SwingDemo(); ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
243 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 JCheckBox("Demo"); checkBox.setSelected(true); boolean sel = checkBox.isSelected(); if (sel) System.out.println("Check box selected!"); getContentPane().add(checkBox); } public static void main(String[] args) { new SwingDemo().setVisible(true); } }OutputSince the checkbox is selected by default, the following output would be visible in EclipseIDE:
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
555 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() { public void actionPerformed(ActionEvent e) { if (checkBox.isEnabled()) checkBox.setEnabled(false); else checkBox.setEnabled(true); } }); JOptionPane.showMessageDialog(null, checkBox); } }OutputNow, when you will uncheck the above checkbox, it will get disabled:
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
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 checkBox3 = new JCheckBox("Football"); checkBox3.setSelected(true); JCheckBox checkBox4 = new JCheckBox("Hockey"); JCheckBox checkBox5 = new JCheckBox("Fencing"); JCheckBox checkBox6 = new JCheckBox("Tennis"); JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ... Read More