Link to home
Start Free TrialLog in
Avatar of valleytech
valleytechFlag for United States of America

asked on

GUI issue

hi all,
 I use a FlowLayout(). However, i also use setLocation() to place my JCheckBox, Label, Button, Textfield to where i want. I just wonder i am on right track. Thanks.
SOLUTION
Avatar of __geof__
__geof__
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of valleytech

ASKER

i have problem with alignment and location in FlowLayout.
Avatar of Mick Barry
no you cannot use setLocation() with a FlowLayout. You need to use no (null) layout manager to explicitly set the position.

FlowLayout just put the Component one after the other and center them on a line. I don't remember seeing somewhere that you could change this. You need to use another layout manager that offers control over positioning.
Why do you want to do setLocation? If you post a picture of how you want it to look we could perhaps find another approach
cominations of BorderLayout and BoxLayout will handle most situations.

let me search around Borderlayout and boxlayout because  i have problem with alignment on flowlayout. i cant control position. Thank you for advises.
Much easier to help if we have a drawing
how to post a picture? So i can post my GUI picture. THanks.
You can attach image files directly to this thread
i just attach my GUI design. Please take a look at it. Thank you.
GUIdesign.jpg
the problem i have is i can't control components' locations when using Flowlayout. please advise what kind of lay out i should use. Thanks.
I divide it into smaller panels as following:
    Printer: MyPrinter,OK as a panel on North
    Image,Text,Code, Selection, All, Applet as a panel at Center
    Cancel, Setup... as a panel on East
    Print Quality, JList, Print to File, Help as a panel on South
  I use BorderLayout to add those panels to frame.
Please suggest.
close, move OK into East

and break up the centre panel into two (BoxLayout) panels
I am coding another one GUI question first because it seems easier. Please take a look at it too :)
 let me start coding this.
Wouldn't it be a lot easier just to use an ide's gui designer? e.g. Netbeans?
well, i try to learn how to manually code GUI . I have moved from netbeans to eclipse :)
>>well, i try to learn how to manually code GUI .

Well that's good practise if you have the time ;-)
once i know how to do it manually, I can use netbeans to generate GUI code and reuse it on eclipse to make my life easier. So i must know this stuff.
so far i have completed this , but not really like i want about the space between JCheckBox, and All radiobutton location !!!!  please help

========
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class Printer extends JFrame {

      private JLabel MyPrinterJLabel;
      
      private JCheckBox ImageJCheckBox;
      private JCheckBox TextJCheckBox;
      private JCheckBox CodeJCheckBox;
      
      private JRadioButton SelectionJRadioButton ;
      private JRadioButton AllJRadioButton;
      private JRadioButton AppletJRadioButton;
      
      private JLabel QualityJLabel;
      private JList OptionJList;
      private JCheckBox PrintJCheckBox;
      
      private JButton OKJButton;
      private JButton CancelJButton;
      private JButton SetupJButton;
      private JButton HelpJButton;
      
      
      private JPanel NorthJPanel, EastJPanel, SouthJPanel, CenterJPanel;
      
      public Printer()
      {
            super("Printer");
            
            JPanel contentPane = new JPanel(new BorderLayout());
            contentPane.setBorder(BorderFactory.createEmptyBorder(5,5,10,10));
            this.setContentPane(contentPane);
            
            MyPrinterJLabel= new JLabel("Printer:MyPrinter");

            NorthJPanel = new JPanel();
            NorthJPanel.add(MyPrinterJLabel);
            
            this.setLayout(new BorderLayout());
            this.add(NorthJPanel,BorderLayout.NORTH);
                  
            ImageJCheckBox= new JCheckBox("Image");
            TextJCheckBox = new JCheckBox("Text");
            CodeJCheckBox = new JCheckBox("Code");
            
            SelectionJRadioButton = new JRadioButton("Selection");
            AllJRadioButton = new JRadioButton("All");
            AppletJRadioButton = new JRadioButton("Applet");
      
            CenterJPanel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            
            Insets leftInsets = new Insets (10,10,10,10);
            Insets rightInsets = new Insets(10,10,10,10);
            
            gbc.gridx =0;
            gbc.gridy=0;
            gbc.insets= leftInsets;
            gbc.anchor = GridBagConstraints.WEST;
            CenterJPanel.add(ImageJCheckBox,gbc);
            
            gbc.gridx =1;
            gbc.gridy=0;
            gbc.insets= rightInsets;
            gbc.anchor = GridBagConstraints.EAST;
            CenterJPanel.add(SelectionJRadioButton,gbc);
            
            gbc.gridx =0;
            gbc.gridy=1;
            gbc.insets= leftInsets;
            gbc.anchor = GridBagConstraints.WEST;
            CenterJPanel.add(TextJCheckBox,gbc);
            
            gbc.gridx =1;
            gbc.gridy=1;
            gbc.insets= rightInsets;
            gbc.anchor = GridBagConstraints.EAST;
            CenterJPanel.add(AllJRadioButton,gbc);
            
            gbc.gridx =0;
            gbc.gridy=3;
            gbc.insets= leftInsets;
            gbc.anchor = GridBagConstraints.WEST;
            CenterJPanel.add(CodeJCheckBox,gbc);
            
            gbc.gridx =1;
            gbc.gridy=3;
            gbc.insets= rightInsets;
            gbc.anchor = GridBagConstraints.EAST;
            CenterJPanel.add(AppletJRadioButton,gbc);
            
            OKJButton = new JButton("OK");
            CancelJButton = new JButton("Cancel");
            SetupJButton  = new JButton("Setup...");
            HelpJButton   = new JButton("Help");
            
            EastJPanel = new JPanel(new GridLayout(4,1,5,5));
            EastJPanel.add(OKJButton);
            EastJPanel.add(CancelJButton);
            EastJPanel.add(SetupJButton);
            EastJPanel.add(HelpJButton);
            
            this.add(CenterJPanel, BorderLayout.CENTER);
            this.add(EastJPanel, BorderLayout.EAST);
            
            /*
      
             import javax.swing.JFrame;

public class MainClass {

      public static void main(String[] args) {
            Printer PrinterInstance = new Printer();
            PrinterInstance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            PrinterInstance.setSize(470,200);
            PrinterInstance.setLocationRelativeTo(null);
            PrinterInstance.setVisible(true);
      }

}
             */
                  
      }
}

1.jpg
> well, i try to learn how to manually code GUI

we find that to be a better approach. We find our students that have relied on gui builders really struggle.

A BoxLayout can be easier to handle alignment than a GridBagLayout
And use a GridLayout to better split the two columns

could you explain in more details?
 I have problem because the checkbox, radiobutton, and cancel button are on same line :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
__geof__ earlier suggestion about using SpringLaypout is also worth looking into. Its a nice flexible layout manager.
I have problem because the checkbox, radiobutton, and cancel button are on same line  <-- -i means at centerpanel we have 3 rows vs cancel + setup are 2 rows.
 That's why i can't just use gridbaglayout.
I actually never use GBL, never found a layout that couldn't be done without it

I actually never use GBL, never found a layout that couldn't be done without it <-- u never use it !!! then u claim no layout couldn't be done without it!!!
 I see conflict here objects :)
hello all experts......
any progress?

well i do some research recently about eclipse vs netbeans. I find out that myeclipse can handle GUI within eclipse. so I gonna restart this task again :).
i don't know why it can't display on the first row. Please show me my error. Thanks.
====
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class Printer extends JFrame {

      private JLabel MyPrinterJLabel;
     
      private JCheckBox ImageJCheckBox;
      private JCheckBox TextJCheckBox;
      private JCheckBox CodeJCheckBox;
     
      private JRadioButton SelectionJRadioButton ;
      private JRadioButton AllJRadioButton;
      private JRadioButton AppletJRadioButton;
     
      private JLabel QualityJLabel;
      private JList OptionJList;
      private JCheckBox PrintJCheckBox;
     
      private JButton OKJButton;
      private JButton CancelJButton;
      private JButton SetupJButton;
      private JButton HelpJButton;
     
     
      private JPanel NorthJPanel, EastJPanel, SouthJPanel, CenterJPanel;
     
      public Printer()
      {
            super("Printer");
           
            Container pane = this.getContentPane();
            pane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
           
            MyPrinterJLabel= new JLabel("Printer:MyPrinter");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy=0;
            pane.add(MyPrinterJLabel,gbc);
           
            OKJButton = new JButton("OK");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx =1;
            gbc.gridy =0;
            gbc.insets = new Insets(0,270,0,0);          
            pane.add(OKJButton,gbc);
           
     
      }
}
Not quite sure what you see as your problem..?
i want to have the Printer:MyPrinter and OK button should be on the first row. How can i display in the middle of the frame?
use a BorderLayout

so for the first row, i should use BorderLayout. Am I right?
yes, North panel of BorderLayout is good for puutting a row across the top

Let me implement this idea immediately :)
done first row. What should I do next :)?

====

public Printer()
      {
            super("Printer");
           
            //Container pane = this.getContentPane();
            //pane.setLayout(new GridBagLayout());
           
            layout = new BorderLayout();
            setLayout(layout);
           
            GridBagConstraints gbc = new GridBagConstraints();
            NorthJPanel = new JPanel (new GridBagLayout());
           
            MyPrinterJLabel= new JLabel("Printer:MyPrinter");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy=0;
            NorthJPanel.add(MyPrinterJLabel,gbc);
           
            OKJButton = new JButton("OK");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx =1;
            gbc.gridy =0;
            gbc.insets = new Insets(0,270,0,0);          
            NorthJPanel.add(OKJButton,gbc);
           
            add(NorthJPanel,BorderLayout.NORTH);    
      }
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i see. please advise what i need to do next. Thanks.
>>i see. please advise what i need to do next.

Well i don't know what you want to do
oh i want to create same GUI that i scanned.
add your buttons in the EAST
and create a panel for your checkboxes and add that center

ok. let me implement it now.
I lost control of Cancel and Setup buttons. Please help.
=============================================================
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class Printer extends JFrame {

      private JLabel MyPrinterJLabel;
     
      private JCheckBox ImageJCheckBox;
      private JCheckBox TextJCheckBox;
      private JCheckBox CodeJCheckBox;
     
      private JRadioButton SelectionJRadioButton ;
      private JRadioButton AllJRadioButton;
      private JRadioButton AppletJRadioButton;
     
      private JLabel QualityJLabel;
      private JList OptionJList;
      private JCheckBox PrintJCheckBox;
     
      private JButton OKJButton;
      private JButton CancelJButton;
      private JButton SetupJButton;
      private JButton HelpJButton;
     
      private JPanel NorthJPanel, EastJPanel, SouthJPanel, CenterJPanel;
      private BorderLayout layout;
      public Printer()
      {
            super("Printer");
           
            //Container pane = this.getContentPane();
            //pane.setLayout(new GridBagLayout());
           
            layout = new BorderLayout();
//            setLayout(layout);
            getContentPane().setLayout(layout);
           

           
            GridBagConstraints gbc = new GridBagConstraints();
            NorthJPanel = new JPanel (new GridBagLayout());
           
            MyPrinterJLabel= new JLabel("Printer:MyPrinter");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy=0;
            NorthJPanel.add(MyPrinterJLabel,gbc);
           
            OKJButton = new JButton("OK");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx =1;
            gbc.gridy =0;
            gbc.insets = new Insets(0,270,0,0);          
            NorthJPanel.add(OKJButton,gbc);
           
            add(NorthJPanel,BorderLayout.NORTH);
           
           
            CancelJButton = new JButton("Cancel");
            SetupJButton  = new JButton ("Setup...");
            EastJPanel = new JPanel(new GridLayout(2,1,5,5));
            EastJPanel.add(CancelJButton);
            EastJPanel.add(SetupJButton);
           
            add(EastJPanel,BorderLayout.EAST);
           
            ImageJCheckBox= new JCheckBox("Image");
            TextJCheckBox = new JCheckBox("Text");
            CodeJCheckBox = new JCheckBox("Code");
           
            SelectionJRadioButton = new JRadioButton("Selection");
            AllJRadioButton = new JRadioButton("All");
            AppletJRadioButton = new JRadioButton("Applet");
     
            CenterJPanel = new JPanel(new GridBagLayout());
           
            Insets leftInsets = new Insets (10,10,10,10);
            Insets rightInsets = new Insets(10,10,10,10);
           
            gbc.gridx =0;
            gbc.gridy=0;
            gbc.insets= leftInsets;
            gbc.anchor = GridBagConstraints.WEST;
            CenterJPanel.add(ImageJCheckBox,gbc);
           
            gbc.gridx =1;
            gbc.gridy=0;
            gbc.insets= rightInsets;
            gbc.anchor = GridBagConstraints.EAST;
            CenterJPanel.add(SelectionJRadioButton,gbc);
           
            gbc.gridx =0;
            gbc.gridy=1;
            gbc.insets= leftInsets;
            gbc.anchor = GridBagConstraints.WEST;
            CenterJPanel.add(TextJCheckBox,gbc);
           
            gbc.gridx =1;
            gbc.gridy=1;
            gbc.insets= rightInsets;
            gbc.anchor = GridBagConstraints.EAST;
            CenterJPanel.add(AllJRadioButton,gbc);
           
            gbc.gridx =0;
            gbc.gridy=3;
            gbc.insets= leftInsets;
            gbc.anchor = GridBagConstraints.WEST;
            CenterJPanel.add(CodeJCheckBox,gbc);
           
            gbc.gridx =1;
            gbc.gridy=3;
            gbc.insets= rightInsets;
            gbc.anchor = GridBagConstraints.EAST;
            CenterJPanel.add(AppletJRadioButton,gbc);

            add(CenterJPanel,BorderLayout.CENTER);
           
      }
}
why use GBL, a simple BoxLayout should suffice

oh i thought GBL help to lay out components in appropriate location. let me change.
after i changed to Boxlayout, i can't make it work. please help.
=================
 public Printer()
      {
            super("Printer");
           
            //Container pane = this.getContentPane();
            //pane.setLayout(new GridBagLayout());
           
            layout = new BorderLayout();
            setLayout(layout);
           
            GridBagConstraints gbc = new GridBagConstraints();
            NorthJPanel = new JPanel (new GridBagLayout());
           
            MyPrinterJLabel= new JLabel("Printer:MyPrinter");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy=0;
            NorthJPanel.add(MyPrinterJLabel,gbc);
           
            OKJButton = new JButton("OK");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx =1;
            gbc.gridy =0;
            gbc.insets = new Insets(0,270,0,0);          
            NorthJPanel.add(OKJButton,gbc);
           
            add(NorthJPanel,BorderLayout.NORTH);  
           
            /*
            CancelJButton = new JButton("Cancel");
            SetupJButton  = new JButton ("Setup...");
            EastJPanel = new JPanel(new GridLayout(2,1,5,5));
            EastJPanel.add(CancelJButton);
            EastJPanel.add(SetupJButton);
            add(EastJPanel,BorderLayout.EAST);
            */
            ImageJCheckBox= new JCheckBox("Image");
            TextJCheckBox = new JCheckBox("Text");
            CodeJCheckBox = new JCheckBox("Code");
           
            SelectionJRadioButton = new JRadioButton("Selection");
            AllJRadioButton = new JRadioButton("All");
            AppletJRadioButton = new JRadioButton("Applet");
           
            Box horizontalBox = Box.createHorizontalBox();
            horizontalBox.add(ImageJCheckBox );
            horizontalBox.add(TextJCheckBox);
            horizontalBox.add(CodeJCheckBox);
            horizontalBox.add(SelectionJRadioButton);
            horizontalBox.add(AllJRadioButton);
            horizontalBox.add(AppletJRadioButton);
           
            add(horizontalBox,BorderLayout.CENTER);
           
           
           
      }
:-)