0

the following codes created a box layout conviniently but the problem i have is the textfields occupy the entire rows. which is supposed to asume the parameter length in which it was specified.

 public void makeControlpanel(){
    JPanel controlpanel = new JPanel();
     //SET PANEL LAYOUT MANAGERS
    controlpanel.setLayout(new BoxLayout(controlpanel,BoxLayout.PAGE_AXIS));

    controlpanel.setBorder(BorderFactory.createTitledBorder("Create Control file")); 


    filenameC   = new JLabel("Filename");
    filenameBad   = new JLabel("Bad Filename");
    filenameDis = new JLabel("Discard Filename");

    // fields
    fileField = new JTextField(1);
    badfileField = new JTextField(7);
    discardfileField = new JTextField(7);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Abiodun
  • 959
  • 6
  • 17
  • 38
  • 1
    Have you read this: http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html ? In general, layoutmanagers have the right to override such properties as the size of the components. – zeller Sep 15 '12 at 09:22
  • either use a LayoutManager that allows the fine-tuning you want or subclass JTextField to return a maxSize. The former is recommended, you'll need a more powerful LayoutManager anyway. – kleopatra Sep 15 '12 at 09:48

1 Answers1

0

The layout manager decides the size of the components. You have options to define the bounds of a component to the layout manager using

comp.setMinimumSize(new Dimension(w, h));
comp.setPreferredSize(new Dimension(w, h));
comp.setMaximumSize(new Dimension(w, h));

When you give setPreferredSize layout manager will try to give that size. GridBagLayout is the msot flexible layout and you can prettymuch achieve any layout you need.

The parameter length by defenition only defines the character you can put in the textfield.

basiljames
  • 4,777
  • 4
  • 24
  • 41
  • no - **never-ever** use any of the setXXSize methods for reasons see http://stackoverflow.com/a/7229519/203657), instead use a suitable LayoutManager. – kleopatra Sep 15 '12 at 09:45
  • @ Kleopatra, what layout do u suggest,as i want to arrange the components i the panel in rows of different lenghts – Abiodun Sep 15 '12 at 09:47
  • @lee my personal favourite currently is MigLayout (btw: the space between the ampersand and the name prevents that I get notified of your comment :-) – kleopatra Sep 15 '12 at 10:07
  • @Kleopatra One should know that these methods exists and what it does so that he can decide whether to use it or not. @Lee Try `GridBagLayout`. – basiljames Sep 15 '12 at 10:08
  • no, best to forget (or newbies never learn) that those methods exist - applying them leads to endless pain further down the route .. – kleopatra Sep 15 '12 at 10:11