Scenario :-
- Created a
jlist
, contains certain values. - Need to pass the
Object test_jL = jlist.getSelectedValue();
toJPanel.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!!!
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;
ifsomeOtherObject
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 withMyObject
types, then you will get a ClassCastException at runtime (which you can catch and react to).