defaultlistmodel

Adding elements to a JList

点点圈 提交于 2019-11-28 11:36:09
I have an array of objects that contain the name of customers, like this: Customers[] How I can add those elements to an existing JList automatically after I press a button? I have tried something like this: for (int i=0;i<Customers.length;i++) { jList1.add(Customers[i].getName()); } But I always get a mistake. How I can solve that? I am working on NetBeans. The error that appears is "not suitable method found for add(String). By the way my method getName is returning the name of the customer in a String. The add method you are using is the Container#add method, so certainly not what you need.

How to clear a JList in Java?

南笙酒味 提交于 2019-11-27 23:30:35
问题 i have a jList in gui where i can add some data with Add button. what i want to add another button called Clear which will clear all elements. i tried this: private void jButtonClearActionPerfomed(java.awt.event.ActionEvent evt) { DefaultListModel listmodel=new DefaultListModel(); jList1 = new JList(listmodel); if(evt.getSource()==jButtonClear) JList.setListData(new String[0]; else listmodel.removeAllElements(); } When I click on Add button this will add elements. When I click on Clear button

Adding elements to a JList

纵饮孤独 提交于 2019-11-27 06:19:10
问题 I have an array of objects that contain the name of customers, like this: Customers[] How I can add those elements to an existing JList automatically after I press a button? I have tried something like this: for (int i=0;i<Customers.length;i++) { jList1.add(Customers[i].getName()); } But I always get a mistake. How I can solve that? I am working on NetBeans. The error that appears is "not suitable method found for add(String). By the way my method getName is returning the name of the customer

Double-click event on JList element

独自空忆成欢 提交于 2019-11-27 00:31:21
问题 I have a JList with a DefaultListModel . How I can make an item in a JList react to double-click event? 回答1: String[] items = {"A", "B", "C", "D"}; JList list = new JList(items); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { JList list = (JList)evt.getSource(); if (evt.getClickCount() == 2) { // Double-click detected int index = list.locationToIndex(evt.getPoint()); } else if (evt.getClickCount() == 3) { // Triple-click detected int index = list