How does a DefaultListModel store it's elements..?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 713 Views - Last Post: 11 April 2019 - 03:48 PM Rate Topic: -----

#16 Joshuaparsons18   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 07-May 16

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 11:45 AM

View PostNormR, on 11 April 2019 - 11:42 AM, said:

Quote

it displays that the JList is empty.

Where is there a posted display of that?
The posted display shows a list with 3 empty Sets.


I didn't want to message you back at that exact second, but I didn't want to give the impression I was ghosting away (I know sometimes I can take a while to get back to you guys.)

You are right that it is NOT displayed in the above quote messages. That I can assist with getting after work or on my next reply. Sorry, about being misleading in that message.
Was This Post Helpful? 0
  • +
  • -

#17 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3627
  • View blog
  • Posts: 16,660
  • Joined: 20-September 08

Re: How does a DefaultListModel store it's elements..?

Posted 11 April 2019 - 03:48 PM

It would probably be better to take a step back and not talk in technical terms about sets and lists and so on. What is your problem domain? What is your GOAL?

Having said that, he is how you can 'explode' a list model consisting of sets of string. Run it and it should be self-explanatory

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

public class FB extends JFrame implements ActionListener {
    private JButton b;
    private JList<Set<String>> setsList;

    public void actionPerformed(java.awt.event.ActionEvent e) {
        setsList.setModel(getSetsListExploded());
    }

    private DefaultComboBoxModel<Set<String>> getSetsListExploded() {
        ListModel<Set<String>> model = setsList.getModel();
        Vector<Set<String>> v = new Vector<>();
        for (int i = 0; i < model.getSize(); i++) {
            Set<String> s = model.getElementAt(i);
            Iterator<String> iter = s.iterator();
            while (iter.hasNext()) {
                Set<String> ss = new HashSet<>();
                ss.add(iter.next());
                v.add(ss);
            }
        }
        return new DefaultComboBoxModel<>(v);
    }

    private Set<String> getInitialSets() {
        return new HashSet<String>(Arrays.asList(new String[] { "A", "B", "C" }));
    }

    private JList<Set<String>> getSetsList() {
        Set<String> s1 = getInitialSets();
        Vector<Set<String>> v = new Vector<>();
        v.add(s1);
        setsList = new JList<>(new DefaultComboBoxModel<Set<String>>(v));
        return setsList;
    }

    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
            JPanel bPanel = new JPanel();
            b = new JButton("Explode");
            b.addActionListener(this);
            bPanel.add(B)/>/>;
            cp.add(bPanel, BorderLayout.SOUTH);
            cp.add(getSetsList(), BorderLayout.CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    FB f = new FB();
                    f.setGui();
                    f.setSize(200, 200);
                    f.setVisible(true);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


This post has been edited by g00se: 12 April 2019 - 04:56 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2