1 / 13

JLists

Steven Jeffries. JLists. What is a JList?. A Jlist is a swing object that lets you store multiple objects in a list, and display them. How does it work?.

raheem
Télécharger la présentation

JLists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Steven Jeffries JLists

  2. What is a JList? • A Jlist is a swing object that lets you store multiple objects in a list, and display them.

  3. How does it work? • JLists take in an array of objects, or a model, and simply lists those things out graphically. To change or add elements in a JList, you don't actually access the JList, but rather its model of whats in the list.

  4. Some Code Like anything else in swing, if you want it to be able to scroll, add the JList to a JscrollPane. jScrollPane1.setBounds(new Rectangle(0, 30, 275, 150)); //declares new JScrollPane this.getContentPane().add(jScrollPane1); //adds it to the content pane jScrollPane1.getViewport().add(jList1); //this adds the jlist to the scrollpane

  5. More Code To add things to a Jlist, you need to change its model. This code will successfully add a new element to the list. All in all, they kind of work like ArrayLists, except instead of add(someObject) its addElement(someObject), but it's still add(index, someObject) DefaultListModel d = (DefaultListModel) jList1.getModel(); //this creates a new model from the existing one d.addElement("A String is an object."); //adds an object to the list jList1.setModel(d); //replaces the old model with the new one

  6. ListSelectionListener • A ListSelectionListener will activate the method valueChanged(ListSelectionEvent e) when something is selected. • e.getFirstIndex() will return the first (top) index of the selection, and e.getLastIndex() will return its last index. • e.getSource() will still return the source of what made the method happen.

  7. Accessing JList Information • By adding a ListSelectionListener, you can set up a GUI to edit your JLists. This takes out the values from the class and puts it in the JTextFields when selection is changed.

  8. How I Did That This is my valueChanged method, it just looks at the first index (because I’m assuming they are only selecting one class), and pulls values out of that class. Don’t forget to typecast just like with ArrayLists. someThing s = (someThing) dlm.get(e.getFirstIndex()); //this creates a class pulled from the model //works just like getting from an ArrayList luckyNumTXT.setText(s.luckyNum + ""); //sets the luckyNum text field to the lucky number jTextField1.setText(s.someNum + ""); //sets the other text field to someNum from s

  9. Changing Information • To change information, we create a new object, and set the new object in place. We use the set(int, object) method to do this. It puts the new object in and removes the old one.

  10. What It Looks Like Before, I have certain information there.

  11. What It Looks Like Then, I change some info…

  12. What It Looks Like And then….

  13. The Code The code for this is actually pretty simple. int i1 = Integer.parseInt(luckyNumTXT.getText()); int i2 = Integer.parseInt(jTextField1.getText()); ///gets the ints out of the text fields someThing s = new someThing(i2, "Number " + location, i1); //creates a new class based on those ints dlm.set(location, s); //sets the item at the location to the new class jList1.setModel(dlm); //then resets the model

More Related