Reputation: 1569
I had a Swing application in which many texts where formatted in the application using HTML3. Some of these texts are created in the objects toString()
method. Now I want to write a parallel JavaFX application using those text formatting in a way that let me to use the model classes for those two separated interfaces.
I have a lot of components (such as JList) that rely on the text rendered in the toString()
methods and many of the texts have HTML format.
I'm not interested to Use JList on the JavaFX application.
Is there a way to keep the model unchanged and use it for both Swing and JavaFX interfaces? Or I should fork completely my project for the JavaFX. Now I have created a new JavaFX project and use the last project contianing the model and the Swing GUI classes as a library.
It is obvious that the string generated from the toString()
method changes during the program execution. So if It makes me prefer to keep using HTML styling in JavaFX which lets me not to hardcode the differences in the model.(For example a text might turn red/blue... depending on the object state)
The following is th toString()
for an Object which is called Tank and I have some ListModel
s which are used in different JList
s. It's not the unique object with this specification in my model.
@Override
public String toString() {
String str = "<html><center>" +
(getNumber() + 1) + " " + getDescription() +"<br><b>";
if(getCurrentJobStage() == null){
str += "<font color=\"green\">Empty</font>";
} else {
str += "<font color=\"red\">Bar: " +
getCurrentJobStage().getOwnerSequence().
getOwnerProductJob().getId() +"</font>";
}
str += "</b><br>---<br>---</center></html>";
return str;
}
Upvotes: 0
Views: 483
Reputation: 209418
As a short answer, no, there is no equivalent mechanism in JavaFX.
As MadProgrammer points out in the comments, you're really paying the price now for having failed to use the proper design in your earlier application. Standard practice mandates separating the view (presentation of your data) from the model (representation of your data) and the controller (processing logic that manipulates your data).
The Tank class you mention above should be part of your model: it should represent your data but have no clue about how the data is presented to the user. The toString() you defined for it broke the general contract of the toString method; instead of providing a 'string that "textually represents" this object', you used it to provide formatting for a particular presentation of the data to the user (i.e. via Swing's JList). Now you want to switch your view technology, you're finding that this is much harder because you didn't properly separate the concerns of data representation from data presentation.
If it makes you feel better, almost every programmer does this more than once before they figure out why these design patterns are in place. It's a kind of rite of passage. Welcome to the world of design patterns.
Here's what I'd recommend as a course of action. Your boss will not agree with this, but it's the right way to go.
Refactor your current Swing application so that the entity objects are not concerned at all with the presentation. In particular, your toString() method should produce something that gives a nice, easily readable representation were you to simply send it to System.out.println(..) and you should introduce a ListCellRenderer that generates the HTML and provides it to the JList to display your objects in swing. I'm not a gambling man(*) but if I were I'd wager that there are lots of other similar enhancements you can make to your code.
Once that's done, then go ahead and proceed with the project to move this to JavaFX. It looks like you want to change the style of the renderer depending on properties in your class. The way you do this in JavaFX is using CSS. It's actually much cleaner in JavaFX 8 (and that went live today, so there's no reason not to use it) than JavaFX 2: you can define a custom css pseudoclass and update it when the relevant properties change. Then define an external css stylesheet that determines the colors (and other styling) for specific pseudoclass settings. It's a while until you'll be ready to do that, but at that point you want to look at the API docs for Pseudoclass and of course post back here if you need more help.
(*)Disclaimer: this is a lie. I really am a gambling man, this just sounded good in context.
Upvotes: 2