X

Geertjan's Blog

  • September 3, 2006

Swing Components and Visual Library 2.0

Geertjan Wielenga
Product Manager
I wasn't entirely correct a few days ago when I said that the Visual Library 2.0 doesn't let you use Swing components. Even though the widgets that you create (such as icons and labels) are not Swing components, you can still use Swing components in various ways. They're never the central focus of an implementation of the Visual Library 2.0, but they can play good supportive roles. Here, for example, is a JComboBox in action, letting you select a different map:

The only difference between this JComboBox and standard JComboBoxes is that this one can be moved (with the mouse) around the map. Here's the code that makes that posible:

private Widget createMoveableComponent(Component component) {
Widget widget = new Widget(this);
widget.setLayout(LayoutFactory.createVerticalLayout());
widget.setBorder(BorderFactory.createLineBorder(Color.BLACK));
widget.getActions().addAction(normalMoveAction);
LabelWidget label = new LabelWidget(this, "Map Choice:");
label.setOpaque(true);
label.setForeground(Color.WHITE);
label.setBackground(Color.BLACK);
widget.addChild(label);
widget.addChild(new ComponentWidget(this, component));
pos += 100;
widget.setPreferredLocation(new Point(pos, pos));
return widget;
}

The only thing is that you can't see the label, because of the map. I think that's a bug. However, I'm okay with that because I can still use the invisible label to drag the menu around the map.

So, the method above receives a component. Here is that component:

mapChoices = new JComboBox(new String[] { "Land Map", "Satellite Map"});
mapChoices.addActionListener(this);
mainLayer.addChild(createMoveableComponent(mapChoices));

So, a standard ActionListener is attached to the JComboBox, for handling the change in the maps. Then, the JComboBox is added to the layer (which is like a glass pane over the 'scene', which is the top root of a Visual Library implementation) and the call is made to the createMoveableComponent() method that is shown above.

In this case, when the map is changed, this is what the user sees:

Pretty cool. So, there's a pretty strong role that Swing components can play in Visual Library 2.0 implementations.

Join the discussion

Comments ( 14 )
  • Pavan Sunday, September 3, 2006
    I tried out the visual library , one thing i found out that none of the swing components appeared in the satelite view , even in this above diagram u cant see the jcombo in the satelite view.. only widgets are visible.. Is this a bug or have i got to do something else to making swing components also visible in the Satellite View.
  • Geertjan Sunday, September 3, 2006
    I'm pretty sure that's intentional. What do you want Swing components to do in the Satellite view??? That view is just for moving around and getting an overall picture of the main view...
  • pavan Sunday, September 3, 2006
    I have a huge data grid(jtable) of abt 100 cols and 500 rows ,i would like to zoom in and zoom out ... and also use the satellite view to navigate thru the grid more easily.. these are all pat of the usability requirements ....
  • Geertjan Sunday, September 3, 2006
    So what's stopping you from doing that? You don't need any Swing components for what you describe.
  • pavan Sunday, September 3, 2006
    My main component is a jtable which is heavily customized for rendering and editing.. now since the table is huge i also need to navigate using the satellite view apart from the normal scrolllbars ..i would like to know if i can use the visual library for this purpose or else do it myself using graphics transformation
  • Geertjan Sunday, September 3, 2006
    Yes, that's the purpose of the satellite view. If you have a satellite view, you automatically can move around in it. The movement within the satellite view is provided out of the box with the satellite view. Does this answer your question?
  • pavan Sunday, September 3, 2006
    nopes,
    i want to know if the visual library can help me create a satellite view for swing components like the one descibed above ..
  • Geertjan Sunday, September 3, 2006
    Probably not, because that is not the purpose of the Satellite view.
  • Pavan Sunday, September 3, 2006
    anyways thanks for ur comments ,i will try and incorporate the satellite view for jtable in my next blog and maybe things will be more clear after that
  • Augusto Saturday, September 9, 2006
    Is visual library supposed to be a high level vector graphics package for Java?
    If so, why are all these examples using bitmapped images (just easier?).
    When you include Swing controls, do they scale? (text and control, and you can still click on them?)
  • Geertjan Saturday, September 9, 2006
    SBG is currently not supported, but will be at some point.
  • Geertjan Saturday, September 9, 2006
    Sorry, I meant "SVG".
  • Swing Components Library Friday, September 28, 2007

    very interesting post

    thanx!


  • ags Tuesday, April 29, 2008

    hi Geertjan

    can i use visual library for Swing components instead of images as expl in http://platform.netbeans.org/tutorials/nbm-visual_library.html


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.