0

I'm trying to use the NetBeans GUI editor for creating a UI. I've read that when using JLists you have to define the list model manually. I've tried to do that but it still does not work. The goal is to insert a string in the list when clicking a button.

I suspect that I've maybe put something in the wrong order or messed up something with inheritance.

Please refer to the following video for the problem (watch fullscreen and 720p): http://www.youtube.com/watch?v=HSB-lDGdQPY&feature=youtu.be

Here is most of the affected code (the list is the one called JList1):

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    String post = inputField.getText();
    defaultModel.addElement(post);
}                                      

.

private void initComponents() {

titleLbl = new javax.swing.JLabel();
addLbl = new javax.swing.JLabel();
inputField = new javax.swing.JTextField();
addBtn = new javax.swing.JButton();
rememberList = new javax.swing.JScrollPane();
DefaultListModel defaultModel = new DefaultListModel();
jList1 = new javax.swing.JList();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

titleLbl.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
titleLbl.setText("Remember to:");

addLbl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
addLbl.setText("Add new:");

inputField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

addBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
addBtn.setText("Add");
addBtn.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        addBtnActionPerformed(evt);
    }
});

jList1.setModel(defaultModel);
rememberList.setViewportView(jList1);
Gray
  • 115,027
  • 24
  • 293
  • 354
jimutt
  • 667
  • 2
  • 13
  • 29
  • 1
    *"Here is most of the affected code:"* For better help sooner, post an [SSCCE](http://sscce.org/) like the one I [posted here](http://stackoverflow.com/questions/11027219/adding-multiple-images-in-single-jpanel/11027533#11027533). That code **can** refresh the content of the list model. BTW - *"Please refer to the following video for the problem"* No thanks. I can't afford to be devoting that kind of bandwidth to help people on SO. – Andrew Thompson Jun 14 '12 at 08:54
  • 1
    [this method is correct](http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultListModel.html#addElement(E)) issue must be somewhere in rest of your code , Item could be added to the end of JLists view , for better help sooner post an [SSCCE](http://sscce.org/) demonstrated your issue with `DefaultListModel` – mKorbel Jun 14 '12 at 08:56
  • Okay, I'll try to do that in a while. – jimutt Jun 14 '12 at 09:13
  • Seems like I will maybe be able to solve the problem myself. It must be something related to inheritance. Because if I add the "addElement" in the constructor of the class it works. But it's just in the actionevent it doesn't work – jimutt Jun 14 '12 at 09:45

1 Answers1

0

I just created a compleately new project and did everything from scratch again. And now I got it working. Don't really now what the original issue was though. As the inheritance still seems to be same.

jimutt
  • 667
  • 2
  • 13
  • 29