gmoranfdez / lanterna

Automatically exported from code.google.com/p/lanterna
GNU Lesser General Public License v3.0
0 stars 0 forks source link

CommonCheckBox's toString() doesn't return label (v. 2.1.6) #84

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I create a CheckBox with label "Check Box #1". I added it to a CheckBoxList. I 
add this to a window with addComponent(). When I run the program, the check box 
is rendered as such:

[ ] com.googlecode.lanterna.gui.component.CheckBox@*memory location*

I tried overriding the toString() method of CheckBox to return a hard coded 
label and that label appears next to the check selection.

Example code:
(this one shows the class name instead of label)
class CheckWin extends Window
{
    public CheckWin()
    {
        super("My Window!");

        CheckBoxList checks = new CheckBoxList();
        CheckBox check1 = new CheckBox("Check Box #1", true);
        checks.addItem(check1);
        addComponent(checks);
    }
}

(this one shows hard coded label)
class CheckWin extends Window
{
    public CheckWin()
    {
        super("My Window!");

        CheckBoxList checks = new CheckBoxList();
        CheckBox check1 = new CheckBox("you can't see this", true) {
                public String toString() 
                {
                    return "Check Box #1"; 
                }
        };

        checks.addItem(check1);
        addComponent(checks);
    }
}

this one renders as:

[ ] Check Box #1

Original issue reported on code.google.com by jasni...@gmail.com on 18 Sep 2013 at 2:39

GoogleCodeExporter commented 9 years ago
Ah, so you actually don't populate a CheckBoxList with CheckBox objects... A 
CheckBox is a separate GUI element which lives on it's own while the 
CheckBoxList is a handy component for making multiple checkboxes in one list 
with an optional scroll bar. 

The "items" you pass in by addItem(..) can be anything, CheckBoxList will call 
.toString() on them to get the label to draw. You can get the original object 
back by calling (for example) getItemAt(..) on the CheckBoxList. If you only 
want a number of labels, you can just use Strings.

Original comment by mab...@gmail.com on 30 Sep 2013 at 1:30