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.
valleytechAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>setLayout(layout);

You don't need to do that - a JFrame has a BorderLayout set by default. If it had been changed, it would in any case be


getContentPane().setLayout(layout); // (You had this earlier)

Open in new window

0
 
__geof__Connect With a Mentor Commented:
A good place to start for layouts is http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html more specially flowlayout: http://java.sun.com/docs/books/tutorial/uiswing/layout/flow.html
I do not think that you can set location in a flow layout but I am not 100% sure. I recommand SpringLayout for best control. (My opinion only)
0
 
valleytechAuthor Commented:
i have problem with alignment and location in FlowLayout.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
no you cannot use setLocation() with a FlowLayout. You need to use no (null) layout manager to explicitly set the position.

0
 
__geof__Commented:
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.
0
 
CEHJCommented:
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
0
 
objectsCommented:
cominations of BorderLayout and BoxLayout will handle most situations.

0
 
valleytechAuthor Commented:
let me search around Borderlayout and boxlayout because  i have problem with alignment on flowlayout. i cant control position. Thank you for advises.
0
 
CEHJCommented:
Much easier to help if we have a drawing
0
 
valleytechAuthor Commented:
how to post a picture? So i can post my GUI picture. THanks.
0
 
CEHJCommented:
You can attach image files directly to this thread
0
 
valleytechAuthor Commented:
i just attach my GUI design. Please take a look at it. Thank you.
GUIdesign.jpg
0
 
valleytechAuthor Commented:
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.
0
 
valleytechAuthor Commented:
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.
0
 
objectsCommented:
close, move OK into East

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

Well that's good practise if you have the time ;-)
0
 
valleytechAuthor Commented:
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.
0
 
valleytechAuthor Commented:
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
0
 
objectsCommented:
> 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

0
 
valleytechAuthor Commented:
could you explain in more details?
 I have problem because the checkbox, radiobutton, and cancel button are on same line :)
0
 
objectsConnect With a Mentor Commented:
giving the the same preferred height will ensure they line up

0
 
objectsCommented:
__geof__ earlier suggestion about using SpringLaypout is also worth looking into. Its a nice flexible layout manager.
0
 
valleytechAuthor Commented:
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.
0
 
objectsCommented:
I actually never use GBL, never found a layout that couldn't be done without it

0
 
valleytechAuthor Commented:
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 :)
0
 
valleytechAuthor Commented:
hello all experts......
0
 
objectsCommented:
any progress?

0
 
valleytechAuthor Commented:
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 :).
0
 
valleytechAuthor Commented:
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);
           
     
      }
}
0
 
CEHJCommented:
Not quite sure what you see as your problem..?
0
 
valleytechAuthor Commented:
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?
0
 
objectsCommented:
use a BorderLayout

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

0
 
valleytechAuthor Commented:
Let me implement this idea immediately :)
0
 
valleytechAuthor Commented:
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);    
      }
0
 
valleytechAuthor Commented:
i see. please advise what i need to do next. Thanks.
0
 
CEHJCommented:
>>i see. please advise what i need to do next.

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

0
 
valleytechAuthor Commented:
ok. let me implement it now.
0
 
valleytechAuthor Commented:
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);
           
      }
}
0
 
objectsCommented:
why use GBL, a simple BoxLayout should suffice

0
 
valleytechAuthor Commented:
oh i thought GBL help to lay out components in appropriate location. let me change.
0
 
valleytechAuthor Commented:
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);
           
           
           
      }
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.