
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 7442 Articles for Java

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

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 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

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 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

358 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:

782 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:

176 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

1K+ 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); add(button); // changing font size dynamically on button click button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { button.setFont(new Font("Dialog", Font.PLAIN, ++fontSize)); button.revalidate(); } }); setVisible(true); } public static void main(String[] args) { new SwingDemo(); } }OutputClick “Change” button above to change the font:

1K+ 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 javax.swing.JOptionPane; public class SwingDemo { public static void main(final String args[]) { JButton btn = new JButton("DemoButton"); btn.addActionListener(new ClickListener()); JOptionPane.showMessageDialog(null, btn); btn.doClick(); } } class ClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { ... Read More

4K+ Views
With set action command, here we are displaying a message in the console on the click of a button.Set the button first:JButton btn = new JButton("Demo Button");Now, set Action Listener to fire when the button is clicked:ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { String str = event.getActionCommand(); System.out.println("Clicked = " + str); } };The following is an example to set action command to JButton:Exampleimport java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public class SwingDemo { public static void main(final String args[]) { JButton btn ... Read More

1K+ Views
For Button border, use createLineBorder() method in Java, which allows you to set the color of the Border as well:JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE);The following is an example to change button border in Java:Exampleimport java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.border.Border; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Button Border"); Container container = frame.getContentPane(); JButton button = new JButton("Demo Button!"); Border border = BorderFactory.createLineBorder(Color.BLUE); button.setBorder(border); ... Read More