Search code examples
javauser-interfacecodenameonetransparency

Can I make a component transparent without hiding it?


I need to make a component transparent, so there's nothing to see, but it still takes its place (unlike with setVisible(false)).

With CSS terminology, I need visibility:hidden rather than display:none.

Ideally, it should work for any component, including containers and their children. So I don't think, subclassing and overriding paint or alike is the way to go.

  • Replacing by other component and delegating could do, will it?
  • Or is there a simpler way?

Solution

  • setVisible() hides a component while it still occupies its space unlike setHidden() which shrinks the component away`. See:

    Form hi = new Form("Visible", BoxLayout.y());
    
    Button r1 = new Button("Regular");
    Button invisible = new Button("Invisible");
    invisible.setVisible(false);
    Button r2 = new Button("Regular");
    
    Button r3 = new Button("Regular");
    Button hidden = new Button("Hidden");
    hidden.setHidden(true);
    Button r4 = new Button("Regular");
    
    hi.add(BoxLayout.encloseX(r1, invisible, r2));
    hi.add(BoxLayout.encloseX(r3, hidden, r4));
    
    hi.show();    
    

    enter image description here