1.

Solve : Redisplaying a JList in Java?

Answer»

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 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?/*
* 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();
// 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);



}

}



Discussion

No Comment Found