7 Years
Discussion Span
Last Post by JamesCherrill

Two things: post your code, not a link to it. Use the code-tag to format it as code. If you don't know what that means, read the intro threads on this forum, it should be in there somewhere, along with lots of other useful information.

Second thing, "it doesn't give useful result" is useless as a question. I have no idea what it is you're asking for, or what I'd be looking for in your code. Describe what is happening, and what you expected. If there are error messages, include them. Tell us what you've tried, and what's happened.

Read this article for more tips on getting useful answers to your questions.

Edited by mike_2000_17: Fixed formatting


i can add element to jlist
BuT i cant remove element from jlist
i use this code for add element

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;

public class NewJFrame extends javax.swing.JFrame {
private DefaultListModel dmod2=new DefaultListModel();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

for(int i=0;i<5;i++){
        dmod2.addElement( i );


and for remove element used this code but not Work
please Help me

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
DefaultListModel dmodd2=new DefaultListModel();
        int index=jList1.getSelectedIndex();

Problem is: You declare DefaultListModel dmodd2=new DefaultListModel(); inside your ActionPeformed. This creates a new dmodd2 that exists only for the duration of your method, and "hides" the dmodd2 that you defined earlier. So your dmodd2.remove(index); applies to the new temporary dmodd2, not the "real" one.
Just remove the incorrect declaration.


why again add DefaultListModel to JList


there must be another problem becuse remove Elment from JList is correct, including test for selectedItem

Edited by mKorbel: n/a


Hi again ... Isolved my last problem and now I have an other probland
and my new code is .... :

private void pushActionPerformed(java.awt.event.ActionEvent evt) {                                     

String str=jTextField1.getText();
int l2=jList2.getLastVisibleIndex();
int l1=jList1.getLastVisibleIndex();
 if(l1!=l2 ){
   listmodel2.insertElementAt(str, 0);
    JOptionPane.showMessageDialog(null, "Stack Is full !!!! ");

but it has a little problem .. if I dont write any Item in TextField and click "push" it add a space in the jlist that I dont like do that ... which code I must add to this to work better !!!


After line 3 you can test to see if there is anything in str. If not, just return from the method without doing anything else.


String str=jTextField1.getText();

If the user hasn't entered anything then the getText() will return a zero-length String ( "" ). You can test str to see what it contains, and if it's "" then the user didn't enter anything, so you don't want to continue with your method.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.