Solved

GUI issue

Posted on 2009-05-06
49
238 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:valleytech
  • 26
  • 12
  • 9
  • +1
49 Comments
 
LVL 6

Assisted Solution

by:__geof__
__geof__ earned 150 total points
ID: 24318724
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
 

Author Comment

by:valleytech
ID: 24319128
i have problem with alignment and location in FlowLayout.
0
 
LVL 92

Expert Comment

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

0
 
LVL 6

Expert Comment

by:__geof__
ID: 24323176
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24323330
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
 
LVL 92

Expert Comment

by:objects
ID: 24323413
cominations of BorderLayout and BoxLayout will handle most situations.

0
 

Author Comment

by:valleytech
ID: 24327640
let me search around Borderlayout and boxlayout because  i have problem with alignment on flowlayout. i cant control position. Thank you for advises.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24327657
Much easier to help if we have a drawing
0
 

Author Comment

by:valleytech
ID: 24327813
how to post a picture? So i can post my GUI picture. THanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24327859
You can attach image files directly to this thread
0
 

Author Comment

by:valleytech
ID: 24327954
i just attach my GUI design. Please take a look at it. Thank you.
GUIdesign.jpg
0
 

Author Comment

by:valleytech
ID: 24330617
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
 

Author Comment

by:valleytech
ID: 24331152
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
 
LVL 92

Expert Comment

by:objects
ID: 24331937
close, move OK into East

and break up the centre panel into two (BoxLayout) panels
0
 

Author Comment

by:valleytech
ID: 24331989
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24334461
Wouldn't it be a lot easier just to use an ide's gui designer? e.g. Netbeans?
0
 

Author Comment

by:valleytech
ID: 24337721
well, i try to learn how to manually code GUI . I have moved from netbeans to eclipse :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24337758
>>well, i try to learn how to manually code GUI .

Well that's good practise if you have the time ;-)
0
 

Author Comment

by:valleytech
ID: 24337978
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
 

Author Comment

by:valleytech
ID: 24341339
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
 
LVL 92

Expert Comment

by:objects
ID: 24341954
> 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
 

Author Comment

by:valleytech
ID: 24342166
could you explain in more details?
 I have problem because the checkbox, radiobutton, and cancel button are on same line :)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 24342207
giving the the same preferred height will ensure they line up

0
 
LVL 92

Expert Comment

by:objects
ID: 24342213
__geof__ earlier suggestion about using SpringLaypout is also worth looking into. Its a nice flexible layout manager.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:valleytech
ID: 24342251
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
 
LVL 92

Expert Comment

by:objects
ID: 24342262
I actually never use GBL, never found a layout that couldn't be done without it

0
 

Author Comment

by:valleytech
ID: 24342366
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
 

Author Comment

by:valleytech
ID: 24390250
hello all experts......
0
 
LVL 92

Expert Comment

by:objects
ID: 24390978
any progress?

0
 

Author Comment

by:valleytech
ID: 24392137
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
 

Author Comment

by:valleytech
ID: 24417056
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24417219
Not quite sure what you see as your problem..?
0
 

Author Comment

by:valleytech
ID: 24417227
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
 
LVL 92

Expert Comment

by:objects
ID: 24417248
use a BorderLayout

0
 

Author Comment

by:valleytech
ID: 24417266
so for the first row, i should use BorderLayout. Am I right?
0
 
LVL 92

Expert Comment

by:objects
ID: 24417282
yes, North panel of BorderLayout is good for puutting a row across the top

0
 

Author Comment

by:valleytech
ID: 24417297
Let me implement this idea immediately :)
0
 

Author Comment

by:valleytech
ID: 24417385
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 24417405
>>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
 

Author Comment

by:valleytech
ID: 24417435
i see. please advise what i need to do next. Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24419284
>>i see. please advise what i need to do next.

Well i don't know what you want to do
0
 

Author Comment

by:valleytech
ID: 24425669
oh i want to create same GUI that i scanned.
0
 
LVL 92

Expert Comment

by:objects
ID: 24427539
add your buttons in the EAST
and create a panel for your checkboxes and add that center

0
 

Author Comment

by:valleytech
ID: 24427722
ok. let me implement it now.
0
 

Author Comment

by:valleytech
ID: 24428018
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
 
LVL 92

Expert Comment

by:objects
ID: 24428034
why use GBL, a simple BoxLayout should suffice

0
 

Author Comment

by:valleytech
ID: 24428965
oh i thought GBL help to lay out components in appropriate location. let me change.
0
 

Author Comment

by:valleytech
ID: 24484997
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24734425
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now