Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

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

Krantik Chavan
Answered 2d 15h ago

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

How to display the items in a JComboBox in Java

Krantik Chavan
Answered 2d 15h ago

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 display the first element in a JComboBox in Java

Nancy Den
Answered 2d 15h ago

To display the first element in a JComboBox, use the getSelectedIndex():comboBox.setSelectedIndex(0);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) ... Read More

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

Krantik Chavan
Answered 2d 15h ago

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

Java Program to disable the first item on a JComboBox

Krantik Chavan
Answered 2d 15h ago

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 set tooltip text for JCheckBox in Java?

Nancy Den
Answered 2d 15h ago

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 ... Read More

How to change JLabel background and foreground color in Java?

Nancy Den
Answered 2d 15h ago

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[]) { ... Read More

How to align JLabel text vertically top in Java?

Krantik Chavan
Answered 2d 15h ago

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 set Mnemonic key for selection of each JCheckBox in Java?

Krantik Chavan
Answered 2d 15h ago

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

Handle JCheckBox Events with an ItemListener in Java

Krantik Chavan
Answered 2d 16h ago

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 get or set the selection state of JCheckBox in Java

Krantik Chavan
Answered 2d 16h ago

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

How to disable JCheckBox if not checked in Java

Krantik Chavan
Answered 2d 16h ago

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

Java Program to create JCheckBox from text in Swing

Krantik Chavan
Answered 2d 16h ago

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 change JButton font dynamically in Java?

Krantik Chavan
Answered 2d 16h ago

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

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

Krantik Chavan
Answered 2d 16h ago

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

Advertisements
Loading...
Unanswered Questions View All

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.