[ControlP5] Basics - radioButton (it doesn't work nicely)

edited September 2014 in Library Questions

hi,
somebody who know how to make each button from radio to the length from the text-label? (see line 10)

import controlP5.*;
ControlP5   cp5 = new ControlP5(this);

size(300, 200);

RadioButton r;
r = cp5.addRadioButton("radio")
.setNoneSelectedAllowed(false)
.setItemsPerRow(4)
//.setItemWidth(70)  //its to short  or to long
.addItem("sdfsdf ", 1)
.addItem("asdfasdf", 2)
.addItem("sdf", 3)
.addItem("dfsasdfasdf", 4)
;

for (Toggle t : r.getItems ()) {
  t.getCaptionLabel().align(CENTER, CENTER);
    t.getCaptionLabel().setControlFont(new ControlFont(createFont("Arial", 12)));
}

Answers

  • somebody an idea? ~:>

  • edited September 2014 Answer ✓

    Changing the width of an individual item is easy but you can't set individual spacing!

    int maxValue = 0;
      for (Toggle t : r.getItems ()) {
        t.getCaptionLabel().align(CENTER, CENTER);
        t.getCaptionLabel().setControlFont(new ControlFont(createFont("Arial", 12)));
        int w = (int) textWidth(t.getLabel());
        if (w > maxValue) maxValue = w;
        t.setSize((int) (w*1.33), 15);
      }
      r.setSpacingColumn(maxValue);
    

    You will get better results if you don't create a new ControlFont for each Toggle.

  • edited September 2014

    thank you colouredmirrorball! :)
    now it works.

    You will get better results if you don't create a new ControlFont for each Toggle.

    ..ok, but is there any other/better method to change my font from the radioButtons?

  • ControlFont someFont = new ControlFont(createFont("Arial", 12));
    
    (...)
    
    t.getCaptionLabel().setControlFont(someFont);
    

    In this simple example it would not matter much, but if you were doing this for a large list you'd end up with a large amount of controlFont objects which are all just the same, littering memory.

  • edited September 2014

    alright ;)

    but is there a difference between:
    t.getCaptionLabel().setControlFont(new ControlFont(createFont("Arial", 12)));
    and
    t.captionLabel().setFont(createFont("Arial", 12));
    ?

  • I don't think so. The Javadoc doesn't even mention the setControlFont() method.

  • edited September 2014

    ok, but it's better to use the first or the second variety?

  • I don't know for sure but it shouldn't matter much.

Sign In or Register to comment.