Themes in lanterna 2.1.9

258 views
Skip to first unread message

edo...@gmail.com

unread,
Mar 10, 2015, 12:56:30 PM3/10/15
to lanterna...@googlegroups.com
Hi,
I've some difficulty in understanding how to change the graphic aspect of some components in a window.
To make things easier I'd like to show a TextBox with RED text on BLACK background and another one with default theme.

To do this I've extended TextBox (NewTextBox) and in the overridden repaint method I instantiated a overridden Theme (NewTheme).
As you can imagine It doesn't work :-) probably because I've not really understood how should things be done.
Any hint about how should things be done or pointer to some example?

Thanks



class NewTheme extends Theme
{
protected NewTheme ()
{
Theme.Definition l_def = new Theme.Definition(
Terminal.Color.RED ,
Terminal.Color.BLACK
) ;

setDefinition( Theme.Category.TEXTBOX , l_def ) ;

}

}



public class NewTextBox extends TextBox
{


[...]

public void repaint( TextGraphics graphics )
{
NewTheme l_test = new NewTheme () ;

graphics.applyTheme( l_test.getDefinition( Theme.Category.TEXTBOX ) ) ;


super.repaint( graphics ) ;


}


}

AvL42

unread,
Mar 11, 2015, 4:02:16 AM3/11/15
to lanterna...@googlegroups.com
The intended way for Themes in 2.1.* versions was to have a Theme that would provide a ThemeDefinition
for each category. A custom theme would subclass lanterna's Theme and in the constructor would add
some "setDefinition(...)" calls.  Then you'd call method setTheme on the guiscreen passing your own theme.
For the one component that you want colored differently, you'd call method .setStyle, passing the special
category that your theme provides the right themedefinition (thus: colors) for.
That was a bit limiting, because you cannot add new categories, only "abuse" existing ones.
Note: I'm only about 90% sure. I might have missed something, myself, back in the days I
used the old 2.1.* Lanterna.

I should mention, that in the 3.0.0-series (still in alpha) the Theme classes have completely changed.
Before investing too much time in the old (2.1.*) approach, you might want to have a look at the 3.0.0
themes and see if they suit you better.

AvL42

unread,
Mar 11, 2015, 4:08:32 AM3/11/15
to lanterna...@googlegroups.com
I forgot to mention, that calling setStyle on the component would usually happen right after its instantiation.
There shouldn't be a need to override its redraw() method, unless you really want to draw it on your own,
completely replacing the standard way of drawing that component.  If your special component only differs
in style, you shouldn't even need to subclass it at all.

edo...@gmail.com

unread,
Mar 11, 2015, 11:44:51 AM3/11/15
to lanterna...@googlegroups.com
Hi,
thanks for your help; it seems however that setStyle only works for Label component and not for example for TextBox :-(
I'll have to find a workaround :-)

Bye

AvL42

unread,
Mar 12, 2015, 12:47:07 PM3/12/15
to lanterna...@googlegroups.com
I apologise, you're right.   What I found in my old sources were all labels.
I'm afraid, I don't know, how to do it with other components.

Maybe subclassing and overriding *is* necessary after all, and rather than
setting the theme in repaint(), it might boil down to copying the original
TextBox's redraw implementation and adapting that for the preferred
colors...   (still not entirely sure)

edo...@gmail.com

unread,
Mar 16, 2015, 10:03:48 AM3/16/15
to lanterna...@googlegroups.com
Dont' worry, your hint has been very useful to me anyway.
Perhaps It'll be enough for me to only change style to labels waiting to adopt lanterna 3.0 as soon as possible.

Thanks

Martin Berglund

unread,
Apr 4, 2015, 12:35:11 AM4/4/15
to lanterna...@googlegroups.com
Yeah, theming wasn't properly implemented until 3.0, I wouldn't count on it too much.

Martin

--
Det här meddelandet skickas till dig eftersom du prenumererar på gruppen "lanterna-discuss" i Google Grupper.
Om du vill sluta prenumerera på den här gruppen och inte längre få någon e-post från den skickar du ett e-postmeddelande till lanterna-discu...@googlegroups.com.
Fler alternativ finns på https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages