Link to home
Create AccountLog in
Avatar of perdoname_
perdoname_

asked on

swing application [ problem with code]

Hello Experts,

Can anyone have a look to the code below and tell me what's needed to change as to be workable?

Thanks in advance!
public class ListModel extends JFrame implements ActionListener {
         
          JList list;
         
          DefaultListModel model;
          
          private JMenu fileMenu = new JMenu("File");
          private JMenuBar menuBar = new JMenuBar();
          private JMenuItem saveItem = new JMenuItem("Save");
          private JMenuItem openItem = new JMenuItem("Load");
          private JMenuItem exitItem = new JMenuItem("Exit");
          private String filename = null;  
          
          int counter = 100;
         
          public ListModel() {
                    
                        add(new JScrollPane(list));
                        JFrame frame = new JFrame("List Model");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setContentPane(new ListModel());
                    frame.setSize(260, 200);
                    frame.setVisible(true);
                    
                    fileMenu.add(openItem);
                    fileMenu.add(saveItem);
                    fileMenu.add(exitItem);
                    openItem.addActionListener(this);
                    saveItem.addActionListener(this);
                    exitItem.addActionListener(this);
                    menuBar.add(fileMenu);
                    setJMenuBar(menuBar);
                    setLayout(new BorderLayout());
                    model = new DefaultListModel();
                    JList list = new JList(model);
                    JScrollPane pane = new JScrollPane(list);
                    final JButton addButton = new JButton("Add Element");
                    JButton removeButton = new JButton("Remove Element");
            
         
            addButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                          String element = JOptionPane.showInputDialog(this);
                          if (element == null)
                          {return;
                          }else{
                                  model.addElement(element);
                          }
         
              }
            });
            
            removeButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                if (model.getSize() > 0)
                  model.removeElementAt(0);
              }
            });
            
             add(pane, BorderLayout.NORTH);
            add(addButton, BorderLayout.WEST);
            add(removeButton, BorderLayout.EAST);
          }
          
          public void actionPerformed(ActionEvent e) {
              if (e.getSource() == openItem)
                        try {
                                loadFile();
                        } catch (IOException e1) {
                                e1.printStackTrace();
                        }
                else if (e.getSource() == exitItem)
                System.exit(0);
          }
          
          private DefaultListModel loadFile() throws IOException {
                    JFileChooser fc = new JFileChooser();
                    String name = null;
                    if (fc.showOpenDialog(null) != JFileChooser.CANCEL_OPTION)
                      name = fc.getSelectedFile().getAbsolutePath();
                    else
                      return model;  
                    FileInputStream stream = new FileInputStream(name);
                        InputStreamReader reader = new InputStreamReader(stream);
                        BufferedReader buffer = new BufferedReader(reader);
                        
                        String line = buffer.readLine();            
                        String[] words = line.split("\\s+"); 
                                for(int j=0;j<words.length;j++) 
                                {
                                        model.addElement(new String(words[j]));
                                         model.addElement(words[j]);
                        }
                                        return model;
                 
                    }
         
          public static void main(String s[]) {
                  new ListModel();
          }
         
        public ListModel getModel() {
                return null;
        }
         
        public String getElementAt(int i) {
                return null;
        }
          
          
          
        }//end of class
 
        class RadioButton1 extends JApplet implements ItemListener {
                  private Container Panel;
                  private LayoutManager Layout;
                  private JRadioButton Unsorted;
                  private JRadioButton Ascending;
                  private JRadioButton Descending;
         
                  public RadioButton1 () {
                    Layout = new FlowLayout ();
                    Panel = getContentPane ();
                    Unsorted = new JRadioButton ("Unsorted");
                    Ascending = new JRadioButton ("Ascending");
                    Descending = new JRadioButton ("Descending");
                    
                    Panel.setLayout (Layout);
                    Panel.add (Unsorted);
                    Panel.add (Ascending);
                    Panel.add (Descending);
                    
                    Unsorted.addItemListener (this);
                    Ascending.addItemListener (this);
                    Descending.addItemListener (this);
                  }
         
                  public void itemStateChanged (ItemEvent e) {
                    ItemSelectable Source;
                    Source = e.getItemSelectable();
                    if (Source == Unsorted) {
                                //unsorted jlist        
                    } else if (Source == Ascending) {
                                sortList(null);
                    } else if (Source == Descending) {
                        //descending jlist
                    }
                    
                  }
                  
                  void sortList(JList list)
                  {
                        javax.swing.ListModel model = list.getModel();              
                        int numItems = model.getSize();
                    String[] a = new String[numItems];
                    for (int i=0;i<numItems;i++)
                    {
                      a[i] = (String)model.getElementAt(i);
                    }
                    sortArray(a);
                    list.setListData(a);
                    list.revalidate();
                  }
                  void sortArray(String[] strArray)
                  {
                    if (strArray.length == 1)    
                      return;
                    Collator collator = Collator.getInstance();
                    String strTemp;
                    for (int i=0;i<strArray.length;i++)
                    {
                      for (int j=i+1;j<strArray.length;j++)
                      {
                        if (collator.compare(strArray[i], strArray[j]) > 0)
                        {
                          strTemp = strArray[i];
                          strArray[i] = strArray[j];
                          strArray[j] = strTemp;
                        }
                      }
                    }
                  }
         
         
         
        }

Open in new window

Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Add these imports.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.ItemSelectable;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.Collator;

import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
Avatar of perdoname_
perdoname_

ASKER

i had the imports [just i didnt paste them here]
class ListModel extends JFrame, but you actually declare:

JFrame frame = new JFrame("List Model");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setContentPane(new ListModel());
                    frame.setSize(260, 200);
                    frame.setVisible(true);


Beware! You are working on two different frames! And this is the only one which is visible, therefore all you code is ignored. You should write:

                   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setContentPane(new ListModel());
                    setSize(260, 200);
                    setVisible(true);
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account