1

i am trying to set the text box and label in panel but the text box is getting inappropriate size and the layout is also not setting properly

public class FrameLayout1 implements ActionListener {
 JTextField[] txtName;
    JLabel[] lblname; 
    JCheckBox keyButton;
    JCheckBox cascadeButton;
    JComboBox SetList;
    JComboBox ClassList;
    JLabel[] lblType;
    JTextField txtJoinColumn;
    JLabel[] lblAssoType;
    JTextField[] txtJoinColumnAssoc;

    FrameLayout1(){

    }
  public void setDetailsPanel() 
    {

        txtName = new JTextField[10];
        txtJoinColumnAssoc = new JTextField[10];
        lblname=new JLabel[10]; 
        lblType=new JLabel[10];
        lblAssoType=new JLabel[20]; 
        JFrame ColumnFrame=new JFrame("Enter the Column Values"); 

        int i=0;

        JPanel panel =  new JPanel(new GridLayout(0,1,5,10));

        for (i=0;i<5;i++) 
        {
            JPanel panelTxtLbl =  new JPanel(new GridLayout(0,2));
            lblname[i] = new JLabel("label"+i+":", JLabel.LEFT);
            panelTxtLbl.add(lblname[i]);

            txtName[i] = new JTextField(15);
            panelTxtLbl.add(txtName[i]);

            panel.add(panelTxtLbl);
            lblType[i] = new JLabel("labeldata", JLabel.LEFT);
            panel.add(lblType[i]);


            String[] SetStrings = { "One to Many","Many to Many" };
            SetList = new JComboBox(SetStrings);
            SetList.setSelectedIndex(1);
            panel.add(SetList);

            cascadeButton = new JCheckBox("cascade"); 
            cascadeButton.setSelected(true);
            panel.add(cascadeButton);

            JPanel panelSetClass =  new JPanel(new GridLayout(0,2));
            for(int j=0;j<3;j++)
             {
                lblAssoType[j]=new JLabel("Label Inner"+j+":", JLabel.LEFT);
                panelSetClass.add(lblAssoType[j]);

                txtJoinColumnAssoc[j] = new JTextField(15);
                panelSetClass.add(txtJoinColumnAssoc[j]);

             }
                panel.add(panelSetClass);
                panel.add(createHorizontalSeparator());
        } 


            //detailsPanel.add(panel, BorderLayout.CENTER);
            //detailsPanel.setAutoscrolls(true);
            panel.setAutoscrolls(true);
            JButton button = new JButton("Submit");
            button.addActionListener(this);
            //detailsPanel.add(button,BorderLayout.PAGE_END); 
            panel.add(button,BorderLayout.PAGE_END); 

            Color c=new Color(205, 222, 216);
            ColumnFrame.setLayout(new BorderLayout());
            ColumnFrame.add(panel,BorderLayout.CENTER);
            //ColumnFrame.setSize(700,600);
            ColumnFrame.setBackground(c);
            ColumnFrame.pack();
            ColumnFrame.setVisible(true);
            ColumnFrame.setLocation(200, 200);
    }
    /*
     * to add a horizontal line in the panel
     */
    static JComponent createHorizontalSeparator() {  
         JSeparator x = new JSeparator(SwingConstants.HORIZONTAL);  
         x.setPreferredSize(new Dimension(3,2));  
         return x;   
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    } 


}

createHorizontalSeparator function helps you add a horizontal line in the panel, as i was unable to segregate the fields .

current output

Imgur

desired output

Imgur

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 3
    1. Where's your image. Please show what is the current result, and try and convey your desired result maybe through Photoshop image or ASCII art. 2. Post an [Runnable MCVE](http://stackoverflow.com/help/mcve) we can test out and play around with. You have so much code posted and IMO unorganized, it's hard to tell what's going on just by looking at it. For future reference, just tagging JFrame by itself will not get you many views. It's usually a good idea to add java and swing as well. I'll take care of it for you this time. – Paul Samsotha Jul 08 '14 at 06:52
  • 1
    _"the text box is getting inappropriate size and the layout is also not setting properly"_ - Frankly, I don't think anyone will be able to help you if you don't help yourself first. You really need to learn how each layout manager works. Seems like you're just making guesses on how to use them. Please look at [Laying out Component within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) and [Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – Paul Samsotha Jul 08 '14 at 07:05
  • `button.setBounds(10, 10, 10,10);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 08 '14 at 07:20
  • `Shell parent` Isn't `Shell` an SWT class? Never mind answering, it will all be clear in an MCVE as suggested by @peeskillet. – Andrew Thompson Jul 08 '14 at 07:22
  • i have updated the CODE with a small runnable code. – pradnyapalan Jul 08 '14 at 08:36
  • 1
    It's happening because your using a gridlayout which stretches its entire layout to the element it's contained within and stretches the elements in each grid to its corresponding size. You need to take everything out of the grid layout and put it in something else for starters and it's ok to experiment with layouts without reading everything about them, some people are just fanatic about details it doesn't mean you have to be. – ObedMarsh Jul 08 '14 at 11:39
  • *"a small runnable code."* Runnable code must compile, so it needs the imports. To be runnable, it also needs a `main(String[])`.. – Andrew Thompson Jul 09 '14 at 10:54
  • main(String[]),i dint add to the code,as it was obvious that it needs main() . the problem was in the multiple panel added ,which resulted in incorrect layout. – pradnyapalan Jul 09 '14 at 11:16

1 Answers1

1

Today, I answered a similar question where GridLayout caused much the same confusion. Do yourself a service and use a flexible layout manager (if it is possible) like MigLayout to create your layouts. These simple built-in layout managers either have very limited application (GridLayout, FlowLayout, BorderLayout) or it can become challenging to create sofisticated layouts with them (BoxLayout).

GridLayout and BorderLayout stretch their components and do not honour the size bounds of their children. This is the reason why you have those unnecessary spaces and why the button is expanded horizontally.

The following is an example that mimics your layout. It is created with the powerful MigLayout manager.

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;


public class DetailsPanel extends JFrame {

    public DetailsPanel() {

        initUI();

        setTitle("Details");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void initUI() {

        setLayout(new MigLayout("ins 10, wrap 2", "[][grow]"));

        add(new JLabel("Label 1:"));
        add(new JTextField(), "w 200, growx");

        add(new JLabel("Label 2:"));
        add(new JTextField(), "growx");    

        add(new JLabel("Label 3:"));
        add(new JTextField(), "gaptop 30, growx");

        add(new JLabel("Label 4:"));
        add(new JTextField(), "growx");      

        add(new JLabel("Label 5:"));
        add(new JTextField(), "gaptop 30, growx");

        add(new JLabel("Label 6:"));
        add(new JTextField(), "growx");          

        add(new JButton("Submit"), "gaptop 30, skip, right");


        pack();
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DetailsPanel ex = new DetailsPanel();
                ex.setVisible(true);
            }
        });
    }
}

You need to dedicate some time to learn a more complex layout manager, but it pays off. Especially, if you often build layouts in Swing.

Details

Community
  • 1
  • 1
Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77