Java list drop down

How to display arraylist as a drop down in Java

Developing a simple java GUI application using swing. I have a ArrayList fetched from a file. How do I display that as a drop down for the user to select. And how to obtain the object which the user has selected. Any suggestions will be greatly appreciated.

4 Answers 4

In the part where you insert items in your menu just loop throught your ArrayList items and select them one by one.

The actionlistener will take care of the action. ie to know wich element was selected.

This is an example for using a fixed string array. It is a fairly simple extrapolation from this code to an arrayList imported from a file:

// define items in a String array: String[] languages = new String[] ; // create a combo box with the fixed array: JComboBox comboLanguage = new JComboBox(languages); 

Look at JComboBox ‘s constructors. You can supply a ComboBoxModel , an array, or a Vector . If you have a List then just create a new Vector from it.

new JComboBox(new Vector(list)); 

If you want your combobox to dynamically change then you’ll need to use a ComboBoxModel .

Читайте также:  Разработка компонентов в php

If you have a collection of objects and want to display a value, you can use the Vector class as the model and then create a custom renderer using the BasicComboBoxRenderer class to display the text.

 Vector data = new Vector(); data.addElement(new Employee(1001, "John Smith")); data.addElement(new Employee(1002, "Linda Baker")); data.addElement(new Employee(1003, "Youcef Hussain")); data.addElement(new Employee(1004, "Jia Lia")); JComboBox jcb = new JComboBox(data); jcb.setRenderer(new ComboBoxRenderer()); class ComboBoxRenderer extends javax.swing.plaf.basic.BasicComboBoxRenderer < public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) < Employee emp = (Employee)value; setText(emp.getName()); return this; >> class Employee < protected int employee_id; public String employee_name; public Employee(int id, String name)< this.employee_id = id; this.employee_name = name; >public String getName() < return this.employee_name; >> 

Источник

Java drop down list in java code

You’ll be able to use that data structure for rendering checkboxes in a dropdown Solution: This is from the example on Page 131 of the PrimeFaces 2.2 Guide EDIT: For JCombobox, you can use Solution 2: This code snippet may help you.

Java, Drop-down list of integers

Use Integer instead, as it inherits from the Object class.:

import javax.swing.JOptionPane; public class Character_Charisma < private static String input = ""; public static int main() < Integer[] choices = ; Integer input = (Integer) JOptionPane.showInputDialog(null, "Choose now. ", "What is your Charisma modifier?", JOptionPane.QUESTION_MESSAGE, null, // Use // default // icon choices, // Array of choices choices[0]); // Initial choice return input; > > 

Selenium — How do I get the the list of options of a drop, Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build your employer brand ; Advertising Reach developers & technologists worldwide; About the company

JAVA — How to Create a Drop Down Menu in Java

Create Drop — Down Menu Using JAVASource Code : https://1bestcsharp.blogspot.com/2017/07/ java — drop — down -menu.html—— Check out my Java Projects!

Java Dropdown Checklist

If you are using JList , then its as simple as changing the ListCellRenderer to return a JCheckbox component.

EDIT: For JCombobox, you can use combobox.setRenderer(myListRenderer);

This code snippet may help you.

The basic idea is to handle actionPerformed or mouseClick events by yourself and keep states of the corresponding items (checked/unchecked) in your own data structure. You’ll be able to use that data structure for rendering checkboxes in a dropdown

Java — drop down list of hyperlinks, Here is the jsp code in Spring Expression Language for creating the drop down list that I have so far: «/> . Here are the methods in the controller for …

Adding a Dropdown listbox

This is from the example on Page 131 of the PrimeFaces 2.2 Guide

If you’d like to use a dropdown instead of an inputtext to only allow predefined filter values use filterOptions attribute and a collection/array of selectitems as value. In addition, filterMatchMode defines the built-in matcher which is startsWith by default. Following is an advanced filtering datatable with these options demonstrated.

 " /> 

So in this example, the carBean should have a method getManufacturerOptions() that returns either SelectItem[] or List containing all the values that should be in the filter dropdown list.

REFERENCE: Javadoc for SelectItem

Dependable drop-down in android using Java, Dependable drop-down in android using Java. A dependable dropdown means that your drop-down list is dependent on another factor or value, which will change the content of the dropdown. In android it is known as spinner. Spinner provides functionality to select a value from a list of multiple values. The …

Text box and Drop Down list

I think the following code may help you.

     

In this way you can solve your 2nd problem.

You can also follow the below code.

   

I think this type of solution you need.

Arrays — Java, Drop-down list of integers, I am trying to have a drop-down list in order to ask for a users charisma modifier. I am currently using the drop-list available using Java Swing. The drop-down list I am using can be found here:

Источник

Create a Dropdown Menu in Java

Create a Dropdown Menu in Java

  1. Create a Drop Down Menu Using JOptionPane in Java
  2. Create a Dropdown Menu Using JComboBox in Java

In this tutorial, we will learn how we can create a dropdown menu in Java using two methods. Both the techniques are used to create GUI components, and the dropdown menu is one of them.

Create a Drop Down Menu Using JOptionPane in Java

The JOptionPane class is a part of the javax.swing package, used mostly to create dialog boxes. In the dialog boxes, we can add multiple types of elements, and one of them is the dropdown component. In the example, we create a string array optionsToChoose containing the options that we want to show in the dropdown menu.

Then, we call the JOptionPane.showInputDialog() command that takes multiple arguments; first is the parent component, where we can attach the dialog to a frame like JFrame . The second argument is the message to display beside the dropdown. We can also set the dialog’s title, which is the third argument, then comes the message type, which can be anything like an ERROR_MESSAGE or a PLAIN_MESSAGE , but we use QUESTION_MESSAGE .

The next argument is the icon that we can display beside the dropdown, but we set it as null. The sixth argument is the array of options to choose in the dropdown, and the last argument is the value from the options to be chosen as default. At last, we get the selected value returned by the JOptionPane.showInputDialog() method as a string and show it in the output.

import javax.swing.*;  public class DropDown   public static void main(String[] args)   String[] optionsToChoose = ;   String getFavFruit = (String) JOptionPane.showInputDialog(  null,  "What fruit do you like the most?",  "Choose Fruit",  JOptionPane.QUESTION_MESSAGE,  null,  optionsToChoose,  optionsToChoose[3]);   System.out.println("Your chosen fruit: " + getFavFruit);  > > 

Java drop down menu

Create a Dropdown Menu Using JComboBox in Java

In this example, we use the JComboBox function, which is a part of the javax.swing package and is used to show a dropdown list in an interface. Below, we first create the array of options to display in the dropdown list. JComboBox is a component and needs a frame to reside, so we create a JFrame object. Then, we create the JComboBox object and pass the options array as its argument in the constructor.

We set the position and the size of the dialog box using the jComboBox.setBounds() function. Then we make a JButton object, pass the text to show on it inside the constructor, and set the bounds. Finally, to show a message when an option is chosen from the dropdown, we create a JLabel and set its bounds.

Next, we add all components in the JFrame using jFrame.add() . We set the layout of jFrame as null and fix its size and visibility. At the end of the code, we also add an ActionListener command that listens to the action performed by the button and calls its method actionPerformed() to show the message in the JLabel with the option that we chose.

import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;  public class DropDown   public static void main(String[] args)   String[] optionsToChoose = "Apple", "Orange", "Banana", "Pineapple", "None of the listed">;   JFrame jFrame = new JFrame();   JComboBoxString> jComboBox = new JComboBox<>(optionsToChoose);  jComboBox.setBounds(80, 50, 140, 20);   JButton jButton = new JButton("Done");  jButton.setBounds(100, 100, 90, 20);   JLabel jLabel = new JLabel();  jLabel.setBounds(90, 100, 400, 100);   jFrame.add(jButton);  jFrame.add(jComboBox);  jFrame.add(jLabel);   jFrame.setLayout(null);  jFrame.setSize(350, 250);  jFrame.setVisible(true);   jButton.addActionListener(new ActionListener()   @Override  public void actionPerformed(ActionEvent e)   String selectedFruit = "You selected " + jComboBox.getItemAt(jComboBox.getSelectedIndex());  jLabel.setText(selectedFruit);  >  >);   > > 

Java drop down menu 2

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article — Java GUI

Related Article — Java Swing

Copyright © 2023. All right reserved

Источник

How to create a drop-down list in java swing with multiple item selection?

I’m aware of JList and JComboBox . I need the combo box drop down functionality with multiple selection functionality that JList provides. This is because the contents of the list are too huge to be displayed using a simple list. I also need to select multiple items, otherwise I would have been content with JComboBox . Any suggestions?

4 Answers 4

When using multi-select, it’s better to use a list than a combo box. As GUI metaphors go, people expect a combo box to be single select, whereas lists can be either.

the contents of the list are too huge to be displayed using a simple list

Place the JList in a JScrollPane . You can call setVisibleRowCount(int) on the JList to specify how many rows at a time should be shown.

You can make a custom cell renderer for the combobox and add checkboxes to that components, so you can check and uncheck them. You have to make something like this:

public class MyComboBoxRenderer implements ListCellRenderer < private String[] items; private boolean[] selected; public MyComboBoxRenderer(String[] items)< this.items = items; this.selected = new boolean[items.lenght]; >public Component getListCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int index) < // Create here a JLabel with the text // Create here a JCheckBox // Add them to a layoutmanager return this; >public void setSelected(int i, boolean flag) < this.selected[i] = flag; >> 

This class must extends some subtypes of Component in order to make the return type of getListCellRendererComponent() match, if you want to return this; . If you want to return the label you created, it’s ok the original(so I don’t really get it). And where does this setSelected() come from? Best to post all code.

If your data has a hierarchical character, consider NetBeans’ Outline component, discussed in Announcing the new Swing Tree Table and in this answer. Here’s the Current Development Version of the API.

To achieve the described functionality, I finally decided to «abuse» the JMenuBar and add to it several JCheckBoxMenuItems . The GUI then perfectly fits the purpose (at least for me), it is just the handling of the ItemEvent that risks to become a bit annoying.

(finally there, I defined some bit logic over the items, and then may restrict myself to handling only one type of event)

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.24.43543

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью