Skip to main content

Help with FormLayout

No replies
scidocks
Offline
Joined: 2003-06-12
Points: 0

/*

I'm trying out the FormLayout tool - a very good one,
BTW - to design a GUI, and I'm running into the
following issue:

What I have is a JPanel consisting of a bunch of text
boxes and sliders. When I launch my app., there appears
to be a fairly large empty space, which I can't seem to
get rid off, to the right of the rightmost column. Now,
if I make my JFrame decorated, then I can't shrink the
width of the JFrame beyond this empty space. If I make
the JFrame undecorated, then I can shrink its width;
however, the empty space which shows up initially does
seem to become an issue when, for example, I add the
panel containing the widgets to a CardPanel - the
extra space seems to affect the rest of the leaves of the
CardPanel. The CardPanel, BTW, is available from TSC, Sun,
and is similar to the CardLayout.

Below is the code, and any help will be very much appreciated :)

*/
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
import java.beans.*;

import com.jgoodies.forms.layout.*;
import com.jgoodies.forms.factories.Borders;

public class RangeCase {

private JPanel m_mainPanel;

public RangeCase(
) {

m_mainPanel = createRangePanel();
}

public JPanel getMainPanel() {

return m_mainPanel;
}

private JPanel createRangePanel() {

FormLayout layout =
new FormLayout(
// 1 2 3 4 5 6 7
"pref, 4dlu, 30dlu, 30dlu, 4dlu, 30dlu, 30dlu",
/* 1 */ " pref, " // x
/* 2 */ + " 2dlu, "
/* 3 */ + " pref, "
/* 4 */ + " 2dlu, "
/* 5 */ + " pref, " // y
/* 6 */ + " 2dlu, "
/* 7 */ + " pref, "
/* 8 */ + " 2dlu, "
/* 9 */ + " pref, " // z
/* 10 */ + " 2dlu, "
/* 11 */ + " pref, "
/* 12 */ + " 2dlu "
);

JPanel pp = new JPanel();
CellConstraints cc = new CellConstraints();
pp.setLayout( layout );
pp.setBorder( Borders.DIALOG_BORDER );

// 1 2 3 4 5 6 7
// "6dlu, 4dlu, 30dlu, 30dlu, 4dlu, 30dlu, 30dlu",

pp.add( new JLabel( "x" ) , cc.xy( 1, 1 ) );
pp.add( new JLabel( "y" ) , cc.xy( 1, 5 ) );
pp.add( new JLabel( "z" ) , cc.xy( 1, 9 ) );

pp.add( new JTextField( 5 ) , cc.xy( 3, 1 ) );
pp.add( new JTextField( 5 ) , cc.xy( 7, 1 ) );

pp.add( new JSlider() , cc.xywh( 3, 3, 2, 1 ) );
pp.add( new JSlider() , cc.xywh( 6, 3, 2, 1 ) );

pp.add( new JTextField( 5 ) , cc.xy( 3, 5 ) );
pp.add( new JTextField( 5 ) , cc.xy( 7, 5 ) );

pp.add( new JSlider() , cc.xywh( 3, 7, 2, 1 ) );
pp.add( new JSlider() , cc.xywh( 6, 7, 2, 1 ) );

pp.add( new JTextField( 5 ) , cc.xy( 3, 9 ) );
pp.add( new JTextField( 5 ) , cc.xy( 7, 9 ) );

pp.add( new JSlider() , cc.xywh( 3, 11, 2, 1 ) );
pp.add( new JSlider() , cc.xywh( 6, 11, 2, 1 ) );

JPanel conPanel = new JPanel();
conPanel.setLayout( new BorderLayout() );
conPanel.add( pp, BorderLayout.EAST );

return conPanel;
}

public static void main( String[] args ) {

JFrame.setDefaultLookAndFeelDecorated( true );

JFrame frame = new JFrame( "FormsTest" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

RangeCase rcase = new RangeCase();
frame.setContentPane( rcase.getMainPanel() );

frame.pack();
frame.setVisible( true );
}
}