SengJoonHyun
SengJoonHyun

Reputation: 73

How to get text in two layered component

Please I hope that do not hurt your eyes after see the one... so sorry.. I want to get a text as I mentioned above.

Let's have a look at my terrible drawing... enter image description here What I want to do is :

I want to get the text from the yellow highlighed box.

I designed my program that I need to get few like the above. I used getComponentCount() to check how many labels there are. It is showing correctly and then I used getComponent(int n) , n = 0, and I was looking for getText().. but there is not.

Always thank you.

Upvotes: 0

Views: 47

Answers (1)

Leet-Falcon
Leet-Falcon

Reputation: 2147

Your design is ok.
getComponent() returns a Component rather than a Label.
You just need to specifically cast it back as a Label:

String text = null;
Component c = panel.getComponent(i);
if (c instanceof Label)
    text = ((Label)c).getText();

MVC Approach:
A more OO solution would be to separate your model (data) from the view (drawing).
You could create a new model Class, let's say "DrawingModel".
Then provide get()/set() for every property in the model.
You then connect both by drawingView.setModel(drawingModel).
When you need any data component, you can access or set it from DrawingModel class rather than directly from the View.
The component who actually "drives" the application is called the Controller.

The approach is an architectural pattern called Model-View-Controller (or MVC in short).
You can learn more here and here.

Upvotes: 2

Related Questions