LevyDee 2 Posting Whiz in Training

Hi guys, as the thread says, im new to java. My department is more in the c++ side of things.

My form class...

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;

public class DogForm extends JFrame
{
    private String _adminLogin = new String("admin");

    private JPanel _dogInfo         = new JPanel();
    private JPanel _otherFunctions  = new JPanel();

    private JButton _registerDog    = new JButton("Register");
    private JButton _buyDog         = new JButton("Buy");

    private JLabel _availableDogs   = new JLabel("Available Dogs");
    private JLabel _registerADog    = new JLabel("Register a new dog");

    //dog info
    private JLabel _diName          = new JLabel("Name");
    private JLabel _diPrice         = new JLabel("Price");
    private JLabel _diRegDate       = new JLabel("Registration Date");
    private JLabel _diType          = new JLabel("Type");

    private JTextField _securityField   = new JTextField(10);

    private DefaultListModel _model      = new DefaultListModel();
    private JList _listOfAvailable      = new JList(_model);

    private DogData DOGDATA = new DogData();

    private Vector<DogData> _dogList = new Vector<DogData>();
   // private ArrayList _dogList = new ArrayList();

    public DogForm()
    {
        InitWindow();

        this.pack();
        this.setVisible(true);

    }

    public void InitWindow()
    {
        this.setTitle("Levi's Dog Store");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());

        _listOfAvailable.setBorder(BorderFactory.createRaisedBevelBorder());
        _listOfAvailable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        _listOfAvailable.addListSelectionListener(new DogListSelection());

        _registerDog.addActionListener(new RegisterDogListener());

        _otherFunctions.setLayout(new GridLayout(4, 1));
        _otherFunctions.add(_registerADog);
        _otherFunctions.add(_securityField);
        _otherFunctions.add(_registerDog);
        _otherFunctions.add(_availableDogs);
        _otherFunctions.add(_listOfAvailable);

        _dogInfo.setLayout(new GridLayout(4, 1));
        _dogInfo.add(_diName);
        _dogInfo.add(_diPrice);
        _dogInfo.add(_diRegDate);
        _dogInfo.add(_diType);
        _dogInfo.add(_buyDog);

        this.setLayout(new BorderLayout());
        this.add(_otherFunctions, BorderLayout.WEST);
        this.add(_dogInfo, BorderLayout.EAST);
    }

    private class DogListSelection implements ListSelectionListener
    {
        public void valueChanged(ListSelectionEvent e)
        {
            ListSelectionModel LIST = (ListSelectionModel)e.getSource();

            int firstIndex = e.getFirstIndex();
            int lastIndex = e.getLastIndex();

            for(int i = firstIndex; i <= lastIndex; i++)
            {
                if(LIST.isSelectedIndex(i))
                {
                    DogData obj = _dogList.get(i);

                    _diName.setText(obj.GetName());
                    _diPrice.setText(obj.GetPrice());
                    _diRegDate.setText(obj.GetDate());
                    _diType.setText(obj.GetType());

                    break;
                }

            }

        }
    }

    private class RegisterDogListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            //TODO check for security input

            DOGDATA.SetName(JOptionPane.showInputDialog(null, "Name:"));
            DOGDATA.SetPrice(JOptionPane.showInputDialog(null, "Price:"));
            DOGDATA.SetDate(JOptionPane.showInputDialog(null, "Registration Date:"));
            DOGDATA.SetType(JOptionPane.showInputDialog(null, "Type:"));

            _dogList.add(DOGDATA);
            _model.addElement(DOGDATA.GetName());
        }
    }

    public static void main(String[] args)
    {
        new DogForm();
        
    }
    
}

My Data class...

public class DogData
{
    private String _name;
    private String _price;
    private String _regDate;
    private String _type;

    public DogData()
    {
        _name = "NAME";
        _price = "0.0";
        _regDate = "DATE";
        _type = "TYPE";
    }

    public void SetName(String str)
    {
        _name = str;
    }

    public void SetPrice(String dbl)
    {
        _price = dbl;
    }

    public void SetDate(String str)
    {
        _regDate = str;
    }

    public void SetType(String str)
    {
        _type = str;
    }

    public String GetName()
    {
        return _name;
    }

    public String GetPrice()
    {
        return _price;
    }

    public String GetDate()
    {
        return _regDate;
    }

    public String GetType()
    {
        return _type;
    }
}

It compiles fine and I can run it, but after a register a dog, and try to click it in the JList, it throws: Exception in thread "AWT-EventQueue-0" in my console.

I commented out ListSelectionModel LIST = (ListSelectionModel)e.getSource(); on line 82 and the associated if statement, and i didnt have any problems, but without that, then selecting a name from the list does nothing.

Any ideas? Thanks

Edit: After reading through the exception statement a little more, it says that I cannot cast a JList to a ListSelectionModel. So how can I accomplish what I am trying to do? Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.