I am wanting to reference an object of type JTextField and convert it to JFormattedTextField. Is it possible to convert to a subclass? I tried coding something to the affect of

ClassA.object = new JFormattedTextField();
ClassA.object.setValue(new Integer(number));

But no success. Any suggestions?

Recommended Answers

All 3 Replies

no success

Can you explain what the problem was?

Object is declared as type JTextField in another class. The second line is giving me an error. It won't let me apply the setValue method. Even though I tried to declare it a JFormattedTextField it only wants to apply the methods of JTextField to the object. I would think this means that it is still somehow of the type JTextField??? Is it not declared correctly? I would post more code but I really don't have any.

it only wants to apply the methods of JTextField to the object

That's always been true. object is a specific type and the compiler will only recognize/find that methods that belong to that type.
Try casting object to be the type that it points/refers to.

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.