Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Redisplaying a JList in Java  (Read 3893 times)

0 Members and 1 Guest are viewing this topic.

zscipio

    Topic Starter


    Rookie

    Redisplaying a JList in Java
    « on: April 25, 2009, 10:13:43 PM »
    I have JList filled with names and a JButton which when clicked deletes the selected item in the JList.  The item is now longer in the array that fills the JList because the array is written out and the item is missing but the missing item is still on the screen.  How do I get the item to disappear from the screen?

    Thank you

    fffreak



      Adviser

    • That's right I am a final fantasy freak.
    • Thanked: 3
      • Yes
      • JSPCRepair
    • Certifications: List
    • Experience: Guru
    • OS: Windows 7
    Re: Redisplaying a JList in Java
    « Reply #1 on: April 26, 2009, 10:48:27 AM »
    Could you paste the source code of what you have now, so that we may take a look?
    Also, now this isn't for a homework assignment now is it?
    Computers are the future, not us. Learn everything you can about them while you still can, soon they will be learning about us... Every bit of advice that I give you is best guess, it is your choice whether or not you listen to it.

    zscipio

      Topic Starter


      Rookie

      Re: Redisplaying a JList in Java
      « Reply #2 on: April 26, 2009, 03:17:07 PM »
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */

      package updateillegalnames;

      /**
       *
       * @author mittlemanmini
       */
      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.DefaultListModel;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JList;
      import javax.swing.JScrollPane;
      import javax.swing.JTextField;
      import javax.swing.ListSelectionModel;
      import javax.swing.event.ListSelectionEvent;
      import javax.swing.event.ListSelectionListener;
      import java.awt.*;
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.ArrayList;

      public class UpdateIllegalNames extends JFrame implements ActionListener
      {
          private JList illegalNameJList;
      //    private ArrayList< String> illegalNames = new ArrayList<String>();
      //    private final String illegalNames[] = {"SYSTEMS", "PROGRAMS"};
          private String [] illegalNames = new String[2000];
          public static int  illegalNameIndex;
          private static int numberIllegalNames = 0;
          private    JTextField illegalNameTextBox = new JTextField("Enter new illegal nane", 20);
          private    JButton buttonAdd = new JButton("Add Illegal Name");
          private    JButton buttonDelete = new JButton("Delete Illegal Name");
          public UpdateIllegalNames()
          {
              super("Add a Name");
              setLayout ( new FlowLayout());
              illegalNames[0] = "SYSTEMS";
              illegalNames[1] = "PROGRAMS";
              numberIllegalNames = 2;
              illegalNameJList = new JList(illegalNames);
              illegalNameJList.setVisibleRowCount(10);
              illegalNameJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
             buttonAdd.addActionListener(this);
             buttonDelete.addActionListener(this);
              add(new JScrollPane(illegalNameJList));
              add(new JScrollPane(illegalNameTextBox));
              add(new JScrollPane(buttonAdd));
              add(new JScrollPane(buttonDelete));
              illegalNameJList.addListSelectionListen er
              (
                      new ListSelectionListener()
                      {
                          public void valueChanged(ListSelectionEvent event)
                          {
                             illegalNameIndex = illegalNameJList.getSelectedIndex() ;
                              System.out.println(numberIllegalNames);
                          }
                      }
              );
          }
              public void actionPerformed(ActionEvent e)
              {

                  Object source = e.getSource();
                  if (source == buttonAdd)
                  {
                      illegalNames[numberIllegalNames] = "BLA";
                      numberIllegalNames++;
                      repaint();
                  }
                  else
                  {
                     if (source == buttonDelete)
                     {               // Go rerun the New Hire
                       System.out.println("EOJ Rerun of state " );
      //                  System.exit(0);

                     }
                  }
              }

         
          public static void main(String[] args)
          {
            UpdateIllegalNames updateIllegalNamesJList = new UpdateIllegalNames();
              updateIllegalNamesJList.setDefaultClose Operation(JFrame.EXIT_ON_CLOSE);
            updateIllegalNamesJList.setSize(300,350);
              updateIllegalNamesJList.setVisible(true);



          }

      }