[java] Newbie question about addElement
You must call fireContentsChanged(...) from the DefaultListModel after you call addElement(...).
Actually, I guess that call is not necessary:
Hope it helps you get ideas =D
Son Of Cain
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.DefaultListModel;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JTextField;public class ListTest { public static void main(String args[]) { DefaultListModel model = new DefaultListModel(); final JList list = new JList(model); ((DefaultListModel) list.getModel()).addElement("Something 1"); ((DefaultListModel) list.getModel()).addElement("Something 2"); ((DefaultListModel) list.getModel()).addElement("Something 3"); ((DefaultListModel) list.getModel()).addElement("Something 4"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(list, BorderLayout.CENTER); final JTextField field = new JTextField(); field.addKeyListener( new KeyListener() { public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_ENTER: ((DefaultListModel) list.getModel()).addElement(field.getText()); break; } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} }); frame.getContentPane().add(field, BorderLayout.SOUTH); frame.setSize(new Dimension(400, 300)); frame.setVisible(true); } }
Hope it helps you get ideas =D
Son Of Cain
Thanks for help, but still I couldnt figure out.
NetBean has a special structure for its GUI class (Auto generating).
we have:
and
and this is my click event:
but I have a feeling that I dont need to do :
//DefaultListModel model = new DefaultListModel();
//final JList jList1 = new JList(model);
because I already have it before? :(
I am confused. NetBean is free. if you try you will see the generated code and what I exactly mean.
Any Ideas ?
NetBean has a special structure for its GUI class (Auto generating).
we have:
public class NewJFrame extends javax.swing.JFrame { /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); }private void initComponents() {jList1 = new javax.swing.JList();jScrollPane2.setViewportView(jList1);}
and
public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JList jList1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTable jTable1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextPane jTextPane1; // End of variables declaration }
and this is my click event:
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) { String a1;Vector list = new Vector ();a1 = jTextField1.getText(); //DefaultListModel model = new DefaultListModel(); //final JList jList1 = new JList(model); //((DefaultListModel) jList1.getModel()).addElement("Something 1"); //((DefaultListModel) jList1.getModel()).addElement("Something 2"); //((DefaultListModel) jList1.getModel()).addElement(a1); //jList1.add( "Something 1" );//jList1.addElement(list(0));jTextField2.setText(a1);jTextPane1.setText(a1); // TODO add your handling code here: }
but I have a feeling that I dont need to do :
//DefaultListModel model = new DefaultListModel();
//final JList jList1 = new JList(model);
because I already have it before? :(
I am confused. NetBean is free. if you try you will see the generated code and what I exactly mean.
Any Ideas ?
I know what you mean, I use netbeans too.
Netbeans instantiates your list, but it does not sets a model for it. You can use the same list instance to call setModel() passing an instance of DefaultListModel (or your custom list model) for it. That will work, guaranteed :D
Get away from generated code until you get the grasp of how the API works; Also, always stick to the API, never to generated code - it helps you be productive, but you should understand what is actually happening if you want to use such a benefit properly ;)
Son Of Cain
[Edit: fixed typos]
Netbeans instantiates your list, but it does not sets a model for it. You can use the same list instance to call setModel() passing an instance of DefaultListModel (or your custom list model) for it. That will work, guaranteed :D
Get away from generated code until you get the grasp of how the API works; Also, always stick to the API, never to generated code - it helps you be productive, but you should understand what is actually happening if you want to use such a benefit properly ;)
Son Of Cain
[Edit: fixed typos]
Do you have any idea that how I can do it in Netbean ? I did Java during my degree but I always loved C++ I only need to read some tutorials to refresh my memory.
Ok, I did this and it works but there is a problem:
DefaultListModel model = new DefaultListModel();
final JList jList1 = new JList(model);
((DefaultListModel) jList1.getModel()).addElement(a1);
jScrollPane2.setViewportView(jList1);
When I type something new in my jtextfield and press the button the Jlist change too, I mean the old value disapear and the new one show up in Jlist. What would be the solution ?
DefaultListModel model = new DefaultListModel();
final JList jList1 = new JList(model);
((DefaultListModel) jList1.getModel()).addElement(a1);
jScrollPane2.setViewportView(jList1);
When I type something new in my jtextfield and press the button the Jlist change too, I mean the old value disapear and the new one show up in Jlist. What would be the solution ?
What's so hard about writing code?
If there's no wizard, write it yourself...
In fact, don't use wizards and other helpers until you know how to do it yourself. In other words, ditch netbeans for a simple text editor (with maybe syntax highlighting).
If there's no wizard, write it yourself...
In fact, don't use wizards and other helpers until you know how to do it yourself. In other words, ditch netbeans for a simple text editor (with maybe syntax highlighting).
bargasteh, from what I can guess from your post, you are creating a new list model and list box each time in your action listener. You should create and set and DefaultListModel once for the list box after creating it. In the action listener, you should then add the element to the previously created list model. Either by having stored it in a variable or through the appropriate accessor of JList.
I would also suggest you have a long look through the very well made Java API documentation and especially Swing tutorials/code samples like here.
While I personally don't advocate working completely IDE-less, I do agree with not relying on GUI builders like the one in Netbeans (I suggest Eclipse, but that's my opinion).
I would also suggest you have a long look through the very well made Java API documentation and especially Swing tutorials/code samples like here.
While I personally don't advocate working completely IDE-less, I do agree with not relying on GUI builders like the one in Netbeans (I suggest Eclipse, but that's my opinion).
Dude, your problem is not with the IDE, but with basic programming skills. When netbeans creates the JList instance for you, you have a REFERENCE to it (the variable!); use this reference to access the JList methods, including setModel() to add a ListModel implementor class to your list.
Refresh your Java knowledge before abusing IDE features... Trust me, that will save you a lot of pain =D
As the say goes, "you're trying to kill a fly using a Bazooka".
Son Of Cain
Refresh your Java knowledge before abusing IDE features... Trust me, that will save you a lot of pain =D
As the say goes, "you're trying to kill a fly using a Bazooka".
Son Of Cain
nothing (fundamentally) wrong with IDEs, but you should never rely on them because you're not capable of doing something yourself.
That's hard (maybe even impossible) with some languages that store parts of the IDE generated code in binary (non-human readable) form, but Java isn't one of them.
So LEARN to do something by hand, then use the IDE to reduce your workload.
That way you can work where there is no IDE (or there is another one than the one you're used to).
That's hard (maybe even impossible) with some languages that store parts of the IDE generated code in binary (non-human readable) form, but Java isn't one of them.
So LEARN to do something by hand, then use the IDE to reduce your workload.
That way you can work where there is no IDE (or there is another one than the one you're used to).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement