Link to home
Start Free TrialLog in
Avatar of perdoname_
perdoname_

asked on

Swing + radiobuttons

Hello Experts,

I need your assistance how can i add the 3 radiobuttons created in the radiobutton1 class in the main window above the jlist and below the menubar!


If anyone can be some sort of help it'd be grateful !

Thanks in advance!
import javax.swing.*;
import java.text.Collator;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
 
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() {
        super("List Model");
        add(new JScrollPane(list));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // The below line was recursively calling ListModel constructor  
        //setContentPane(new ListModel());
        setSize(260, 200);
        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

ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I suggest the use of a visual GUI builder, like Netbeans (www.netbeans.org)
>>I suggest the use of a visual GUI builder

it is always better to know & understand the code even if you use a tool to build UI!