For the life of me I cannot modify the model of a JList. Basically I have a jTabbedPane which holds a JPanel, which holds a JTextField, JScrollPane, JList (in the Scroll Pane) and two JButtons, one for adding values to the JList the other for removing values from the JList.
This is very similar to idea behind the ListDemo.java (Hire and Fire application) example found here: http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
Even after creating the model as a DefaultListModel and casting the model to the DefaultListModel (when I call getModel() ) it will not let me add anything to model. After checking the size before and after I try to add an element it is the same size.
The output is the following:
SIZE BEFORE: 6
THIS BLOWS 2
SIZE AFTER: 6
I don't know if I'm supposed to do anything in the ListenerHandler.java file other then simply implement the methods in the interface. From the output it seems to call the intervalAdded method and then it calls makes the call to the getSize() method again, which shows nothing has changed.
For some reason if I call the size() function on the DefaultListModel object, before I add anything, it returns 0. When I call the size() method after the call to the add() method it returns a value of 1. I am using NetBeans 5.5 GUI builder to create this application.
In any event I'm completely lost, I'm sure its an easy fix, maybe I'm supposed to invalidate() and then validate() some component but after trying that it didn't work either.
Any insight on this issue would be greatly appreciated. Thanks in advance !
Here are the relevant portions of the code:
_*NewJFrame.java*_
package pkg_reseller_application;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.event.ListDataListener;
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
profilePanel = new javax.swing.JPanel();
SONumPanel = new javax.swing.JPanel();
SONumTextField = new javax.swing.JTextField();
SONumScrollPane = new javax.swing.JScrollPane();
DefaultListModel defaultModel = new DefaultListModel();
SONumList = new javax.swing.JList();
SONumList = new JList(defaultModel);
addSONumButton = new javax.swing.JButton();
removeSONumButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(600, 600));
jTabbedPane1.setPreferredSize(new java.awt.Dimension(600, 600));
profilePanel.setPreferredSize(new java.awt.Dimension(600, 600));
SONumPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Standing Offer #'s"));
SONumPanel.setName("SONumPanel");
SONumTextField.setName("SONumTextField");
SONumScrollPane.setName("SONumScrollPane");
SONumList.setModel(fillListModel(SONumList.getName()));
SONumList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
SONumList.setName("SONumList");
SONumScrollPane.setViewportView(SONumList);
addSONumButton.setText("Add");
addSONumButton.setActionCommand("addToListButtonCommand");
addSONumButton.setName("addSONumButton");
addSONumButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionPerformedHandler(evt);
}
});
removeSONumButton.setText("Remove");
removeSONumButton.setName("removeSONumButton");
/* ....Alignment code goes here...... */
pack();
}
private void actionPerformedHandler(java.awt.event.ActionEvent evt) {
new ActionHandler().performAction(evt, this);
}
public javax.swing.DefaultListModel fillListModel(final String parentContainerName){
DefaultListModel listModel = new DefaultListModel(){
//Get appropriate values; pass in list name to know which values to get.
//example; new Profile().getListValues(panelName)
String [] listItems = new Profile().getListValues(parentContainerName);
public Object getElementAt(int i) {return listItems;}
public int getSize() {return listItems.length;}
};
listModel.addListDataListener(new ListenerHandler());
return listModel;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JPanel BDFBuilderPanel;
public javax.swing.JPanel BDFValidatorPanel;
public javax.swing.JList SONumList;
public javax.swing.JPanel SONumPanel;
public javax.swing.JScrollPane SONumScrollPane;
public javax.swing.JTextField SONumTextField;
public javax.swing.JButton addSONumButton;
// End of variables declaration
Profile.java_
package pkg_reseller_application;
import java.io.*;
public class Profile {
/** Creates a new instance of Profile */
public Profile() {
}
public String [] getListValues(String parentContainerName)
{
/*final*/ String DELIMETER = ":::";
String [] listItems = null;
String tempRecord = "";
String [] result = null;
try
{
BufferedReader profileDataReader = new BufferedReader(new FileReader(new File("C:/Sun/NetBeans_5.5/PROJECTS/ResellerApplication/src/pkg_reseller_application/PROFILE_DATA.txt")));
for(int i = 0; i == 0 && (tempRecord = profileDataReader.readLine()) != null; i++)
{
//Trims title from the Profile Date line
tempRecord = tempRecord.substring(tempRecord.indexOf(DELIMETER) + DELIMETER.length(), tempRecord.length());
result = tempRecord.split(":::");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return result;//listItems;
}
}
ActionHandler.java_
package pkg_reseller_application;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.*;
import javax.swing.event.ListDataEvent;
public class ActionHandler{
/** Creates a new instance of ActionHandler */
public ActionHandler() {
}
public void performAction(java.awt.event.ActionEvent evt, NewJFrame jFrame)
{
if(evt.getActionCommand().equals("addToListButtonCommand"))
addValueToList(evt, jFrame);
}
private void addValueToList(ActionEvent evt, NewJFrame jFrame)
{
DefaultListModel x = ((DefaultListModel)(jFrame.SONumList.getModel()));
System.out.println("SIZE BEFORE: " + x.getSize());
x.add(0, "14224335");
System.out.println("SIZE AFTER: " + x.getSize());
}
}
ListenerHandler.java_
package pkg_reseller_application;
import javax.swing.DefaultListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
public class ListenerHandler implements ListDataListener {
/** Creates a new instance of ListenerHandler */
public ListenerHandler() {
}
public void contentsChanged(ListDataEvent e) {
System.out.println("THIS BLOWS 1");
}
public void intervalAdded(ListDataEvent e) {
System.out.println("THIS BLOWS 2");
}
public void intervalRemoved(ListDataEvent e) {
System.out.println("THIS BLOWS 3");
}
}