waltido

waltido

If the linked list program does not display the list on the GUI?

I am using the following code:

import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*;

class LinkedListExample { public static void main(String[] args) { String names[]=new String[5]; for(int i=0;i list=new LinkedList<String>();

    for(int i=0;i<names.length;i++){
        list.add(names[i]);
    }
    final JTextArea area=new JTextArea(5,20);
    JScrollPane pane=new JScrollPane(area);
    JButton b=new JButton("Reverse");

    b.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        area.setVisible(true);
        Collections.reverse(list);
        StringBuffer buffer=new StringBuffer();
        for(String names : list){
          buffer.append(names);
          buffer.append("\n");
        }
        area.append(buffer.toString());
        }
    });
    JFrame f=new JFrame();
    f.setLayout(null);
    b.setBounds(20,20,100,20);
    pane.setBounds(20,50,200,150);
    pane.setVisible(false);
    f.add(b);
    f.add(pane);
    f.setVisible(true);
    f.setSize(300,200);
}

}

View Answers

September 15, 2011 at 1:37 PM

import java.awt.*;
   import java.util.*;
   import javax.swing.*;
   import java.awt.event.*;

        class  LinkedListExample
        {
            public static void main(String[] args) 
            {
                String names[]=new String[5];
                for(int i=0;i<names.length;i++){
                 names[i]=JOptionPane.showInputDialog("Enter name "+(i+1));
                }
                final LinkedList<String> list=new LinkedList<String>();

                for(int i=0;i<names.length;i++){
                    list.add(names[i]);
                }
                        JButton b=new JButton("Reverse");
                        final DefaultListModel model = new DefaultListModel();
                JList jlist = new JList(model);
                final JScrollPane pane=new JScrollPane(jlist);                
                b.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                    pane.setVisible(true);
                    Collections.reverse(list);

                    for(String names : list){
                     model.addElement(names);

          }
                    }
                });


                JFrame f=new JFrame();
                f.setLayout(null);
                b.setBounds(20,20,100,20);
                pane.setBounds(20,50,200,150);
                pane.setVisible(false);
                f.add(b);
                f.add(pane);
                f.setVisible(true);
                f.setSize(300,200);
            }
        }









Related Tutorials/Questions & Answers:
Waltido
waltido
Advertisements
Waltido
Waltido
Waltido

Ads