Krantik Chavan has Published 308 Articles

How to display the items in a JComboBox in Java

Krantik Chavan

Krantik Chavan

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

622 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

194 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

585 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

83 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

355 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

How to display row count in Java Swing JList

Krantik Chavan

Krantik Chavan

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

380 Views

Use JList getVisibleRowCount() method to display the row count in a JList:String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);The following is an example to display row count in JList:Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; ... Read More

Map to create new value from int array in Java

Krantik Chavan

Krantik Chavan

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

407 Views

Let’s say the following is our int array elements:10, 50, 100, 200, 250, 300, 400, 500Here, we are mapping and creating a new value by incrementing each int element with 1:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1)Now find the average:Arrays.stream(new int[] {10, 50, ... Read More

Java Program to select all the items in a JList

Krantik Chavan

Krantik Chavan

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

505 Views

To select all the items in a JList, use setSelectionInterval() and set a range:JList list = new JList(sports); int begn = 0; int end = list.getModel().getSize() - 1; if (end >= 0) {    list.setSelectionInterval(begn, end); }The following is an example to select all the items in a JList:Examplepackage my; ... Read More

Java Program to select the first item in JList

Krantik Chavan

Krantik Chavan

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

298 Views

To select the first item in JList, use setSelectionInterval() method:String values[]= { "One", "Two", "Three", "Four", "Five", "Six"}; JList list = new JList(values); int begn = 0; int end = 0; list.setSelectionInterval(begn, end);The following is an example to select the first item in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import ... Read More

Advertisements