Change component style more efficient

asked 2015-08-11 14:43:51 +0800

Manz gravatar image Manz

Hi people,

I need to create a very complex window, with a lots of labels, textboxes and buttons filled with data from a database. Also each of these components must have a different style (background, foreground, font, border etc.) depending on the data when loading. So far so good! My problem is to change a single style property without deconstructing and reconstructing the style-string again. May be a textbox style is:

_textBox.setStyle("background-color:#123456;font-family:open sans light;font-size:123;color:#654321");

If I want to change the font-size for example, I could split the string at ";", check for "startsWith("font-family"), split again at ":" and so on. After all I have to construct the style string again and set the property. Well, I could store style-strings parallel in a hashmap, but when changing a single style has to take effect on multiple components, there is a lot of checking, looping, hashing etc. Also I'm afraid to blow up the performance and memory budget!

Is there a more efficient way to change a single style of a component? Direct access to the CSS-parser or a helper Class?

delete flag offensive retag edit

4 Answers

Sort by ยป oldest newest most voted

answered 2015-08-12 15:37:42 +0800

cyiannoulis gravatar image cyiannoulis
1191 10

I don't understand why you have to do such a thing. The first thing is to define all different styles in a separate css file. Then apply in the sclass property of each component all required css-classes depending on your business rules.


link publish delete flag offensive edit

answered 2015-09-21 08:15:12 +0800

Manz gravatar image Manz

No, I cannot define a css file for every single style state that could be. As written the colors and fonts depends on loaded data.

link publish delete flag offensive edit

answered 2015-09-21 08:37:03 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9

updated 2015-09-21 08:37:22 +0800

Why don't you create a enum?

Define in the enum all the different possibilities.
you can even save the enum value in the DB, so when you load your data, you just need to get the style from the enum.
The thing what change is when you save the data, you need to check what enum value it will be.

Like this, lesser check in the viewmodels (most of time) but you have to adapt your model and service.


link publish delete flag offensive edit

answered 2015-09-21 09:18:29 +0800

Darksu gravatar image Darksu
1991 1 4

updated 2015-09-21 09:18:46 +0800

Hello Manz,

Also you could define domain models in order to retrieve your data.


Table Name: Template (id, name, create_date, update_date)
Table Name: Template_Values (id, template_id, property_name, property_value, create_date, update_date)

Best Regards,


link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2015-08-11 14:43:51 +0800

Seen: 46 times

Last updated: Sep 21 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More