Link to home
Start Free TrialLog in
Avatar of perdoname_
perdoname_

asked on

automatically select radiobutton [swing]

Hello Experts,

I need your assistance how can a radiobutton [in specific "Unsorted" be selected automatically after the loading of the file [loadmethod]

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

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() {
        super("List Model");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        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();
        final 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) {
            	int selectedIndices[] = list.getSelectedIndices();
                for (int i = 0; i < selectedIndices.length; i++)
                {
              	  model.removeElementAt(selectedIndices[i] - i);
                }
            }
        });
       
        JPanel pnlButtons = new JPanel() ;
        pnlButtons.add( addButton ) ;
        pnlButtons.add( removeButton ) ;
 
        add( new RadioButton1(), BorderLayout.NORTH );
        add( pane, BorderLayout.CENTER );
        add( pnlButtons, BorderLayout.SOUTH );
        pack() ;
        setVisible(true);
    }
 
    
 
	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);
        while (true)
        {
            String line = buffer.readLine();
            if (line == null)
            {
            	break;
            }
            model.addElement(line);
        }
		return model;
    }
 
    public static void main(String s[]) {
        new ListModel();
    }
 
    public ListModel getModel() {
        return null;
    }
 
    public String getElementAt(int i) {
        return null;
    }
    
    /*
     *   /**Removing elements from the list
  void jbtRemove_actionPerformed(ActionEvent e) { 
    int selectedIndices[] = jList1.getSelectedIndices(); 
 
    for (int i=0; i<selectedIndices.length; i++) { 
      listModel.removeElementAt(selectedIndices[i] - i); 
    } 
  } 
 
     */
    
    //removeElement;
    void removeButtonClicked()
    {
      int selectedIndices[] = list.getSelectedIndices();
      for (int i = 0; i < selectedIndices.length; i++)
      {
    	  model.removeElementAt(selectedIndices[i] - i);
      }
    }
 
 
}//end of class
 
 
 
class RadioButton1 extends JPanel implements ItemListener {
    private Container Panel;
    private LayoutManager Layout;
    private JRadioButton Unsorted;
    private JRadioButton Ascending;
    private JRadioButton Descending;
 
    public RadioButton1() {
        Layout = new FlowLayout();
        Unsorted = new JRadioButton("Unsorted");
        Ascending = new JRadioButton("Ascending");
        Descending = new JRadioButton("Descending");
 
        setLayout(Layout);
        add(Unsorted);
        add(Ascending);
        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 CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of perdoname_
perdoname_

ASKER

oops sorry .. [i edited the question]