Hello friends . I have some problems .
I want to Implement a stack and use 2 jButoons for push and pop and a jlist for showing the pushed elements . I write this code but it dont give me good result ...!
please help me compelet this program . I spent much times to creat this ... I'm beginner in java programming :icon_rolleyes: ... help me please
this is the source code :
http://www.upload4files.tk/download.php?file=4f0a1863d5fd7195effa6415f8e98cef

thanx
neda

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.

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 );

jList1.setModel(dmod2);        
}
}

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();
    if(index>-1){
dmodd2.remove(index);
jList1.setModel(dmodd2);
        }
    }

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


Edit

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

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);
   jList1.setModel(listmodel2);
}
 else{
    JOptionPane.showMessageDialog(null, "Stack Is full !!!! ");
 }
jList1.setModel(listmodel2);
jTextField1.setText("");
    }

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.

please explain more :) ... I dont understand ... !
thanx

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.