Can an Object type be casted to a Component type?

336 views Asked by At

Scenario :-

  1. Created a jlist, contains certain values.
  2. Need to pass the Object test_jL = jlist.getSelectedValue(); to JPanel.add(test_jL)

Not possible literally as add() accepts Component type only

Here is what I am trying to do :-

Object test_jList = jXList1.getSelectedValue();
JPanel.removeAll();
JPanel.add(test_jList);  // doesn't matter, as it won't accept an Object type
JPanel.repaint();
JPanel.revalidate();

Question is :- Can an Object type be casted to a Component type ?

I might sound like a noob, but trying to expand my knowledge in Java, so expect your suggestions and ideas to construct the scenario for solving the problem. Thanks for your time!!!

3

There are 3 answers

10
Dave Lugg On

In Java you can attempt to cast any object to any other object that shares the same inheritance. Object is a superclass of every Object in Java, so you can legally state MyObject myObj = (MyObject) someOtherObject; if someOtherObject is declared as an Object type.

If, at runtime, someOtherObject is a MyObject type, then this cast will work and flow will continue. However, if it's some other type that doesn't share an inheritance with MyObject types, then you will get a ClassCastException at runtime (which you can catch and react to).

2
Little Santi On

OK; If you want to pass an argument to a certain panel, forget about add(): That method serves to add a new visual control (a textfield, a list, a button, etc) to that panel.

I recommend you extend the JPanel to create your own class:

public class MyPanel extends JPanel
{
    public MyPanel()
    {
        super(<layout>);
    }

    public void setParameter(Object parameter)
    {
        // ...
    }
}

Then, instantiate it from the main panel and use its setParameter method to pass the parameter value:

public class MyMainPanel extends JPanel
{
    private final MyPanel panel01=new MyPanel();

    public MyMainPanel()
    {
        super(<layout>);
        add(panel01);
    }

    ... listener code...
    {
        Object value = jXList1.getSelectedValue();
        this.panel01.setParameter(value);
    }
}

(Note that setParameter is not a standard API: Actually you might give it a specific, more semantic name: setBook, setSong, setArtist...)

0
Madhan On

This way you can add JPanels to the list and based on the selectionchange on list you can dynamically update the view

 public class NewClass {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel p = new JPanel();
    JList list = new JList();
    list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {

        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            p.removeAll();
            p.add((Component) list.getSelectedValue());//dynamically adding panel from list
        }
    }
    );
    DefaultListModel m = new DefaultListModel();
    JPanel x = new JPanel() {
        public String toString() {
            return "xPanel";
        }
    };
    x.add(new JButton("@3"));

    JPanel y = new JPanel() {
        public String toString() {
            return "yPanel";
        }
    };
    y.add(new JTextField(20));

    m.addElement(x);
    m.addElement(y);//adding panel to list
    list.setModel(m);
    list.setSelectedIndex(0);

    frame.add(list, BorderLayout.NORTH);
    frame.add(p, BorderLayout.CENTER);
    frame.setSize(600, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

}

IF you select values from list you can see button and textfield swapping places