Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 14th, 2014, 01:29 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question GridBagLayout Help

I am having trouble trying to position labels etc using the gridbaglayout. From the pic above the textfield and the label are too far apart and i'm unable to position the text field closer to the label vision and the same for the checkbox and the cop label.

I've tried all the techniques through research and i'm still unable to position object where i want to

I would be grateful if I could get some assistance please.

Cheers.


Code:
private GridBagConstraints con = new GridBagConstraints();
 
public void setUpActor(){
mainFrame = new JFrame("Config Actor");
copLabel1 = new JLabel("Cop");
visionLabel = new JLabel("Vision");
title = new JLabel("Set up Actors");
title.setFont(new Font("Arial", Font.PLAIN, 20));
copChkBox1 = new JCheckBox();
topPanel = new JPanel();
 
 
topPanel.setLayout(new GridBagLayout());
		con.gridx = 0;
		con.gridy = 0;
		con.gridwidth = 3;
		con.weighty = 1.0;
		topPanel.add(title, con);
 
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.gridx = 0;
		con.gridy = 1;
 
		topPanel.add(visionLabel, con);
 
		con.fill = GridBagConstraints.NONE;
		con.weightx = 0.5;
		con.gridx = 1;
		con.gridy = 1;
		topPanel.add(vTxtField, con);
 
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.gridx = 0;
		con.gridy = 2;
		topPanel.add(copLabel1, con);
 
		con.fill = GridBagConstraints.NONE;
		con.weightx = 0.5;
		con.gridx = 1;
		con.gridy = 2;
		topPanel.add(copChkBox1, con);
 
 
                mainFrame.getContentPane().add(BorderLayout.NORTH, topPanel);
		mainFrame.pack();
		mainFrame.setSize(400, 300);
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Reply With Quote
  #2 (permalink)  
Old December 3rd, 2018, 08:56 AM
Authorized User
Points: 209, Level: 4
Points: 209, Level: 4 Points: 209, Level: 4 Points: 209, Level: 4
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Mar 2012
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Manny123 View Post
I am having trouble trying to position labels etc using the gridbaglayout. From the pic above the textfield and the label are too far apart and i'm unable to position the text field closer to the label vision and the same for the checkbox and the cop label.

I've tried all the techniques through research and i'm still unable to position object where i want to

I would be grateful if I could get some assistance please.

Cheers.


Code:
private GridBagConstraints con = new GridBagConstraints();
 
public void setUpActor(){
mainFrame = new JFrame("Config Actor");
copLabel1 = new JLabel("Cop");
visionLabel = new JLabel("Vision");
title = new JLabel("Set up Actors");
title.setFont(new Font("Arial", Font.PLAIN, 20));
copChkBox1 = new JCheckBox();
topPanel = new JPanel();
 
 
topPanel.setLayout(new GridBagLayout());
		con.gridx = 0;
		con.gridy = 0;
		con.gridwidth = 3;
		con.weighty = 1.0;
		topPanel.add(title, con);
 
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.gridx = 0;
		con.gridy = 1;
 
		topPanel.add(visionLabel, con);
 
		con.fill = GridBagConstraints.NONE;
		con.weightx = 0.5;
		con.gridx = 1;
		con.gridy = 1;
		topPanel.add(vTxtField, con);
 
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.gridx = 0;
		con.gridy = 2;
		topPanel.add(copLabel1, con);
 
		con.fill = GridBagConstraints.NONE;
		con.weightx = 0.5;
		con.gridx = 1;
		con.gridy = 2;
		topPanel.add(copChkBox1, con);
 
 
                mainFrame.getContentPane().add(BorderLayout.NORTH, topPanel);
		mainFrame.pack();
		mainFrame.setSize(400, 300);
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Hi
Practice makes one perfect.
You may try this program if pleased reply

Thanks
As
muthukutta1
Code:
package gridbaglayout;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.*;
import java.util.Random;
import javax.swing.border.*;

public class GridBagLayoutPanel extends JPanel implements ActionListener 
{	  
      private JLabel messageText;
      private JLabel label;
      private JPanel topPanel = new JPanel();
      private JPanel results = new JPanel();
      private JComboItems cbSupermarketItem;
      private JRadioButton buttonMasalaDosa = new JRadioButton("MasalaDosa");
      private JRadioButton buttonRavaMuruval = new JRadioButton("RavaMuruval");
      private JRadioButton buttonSpecialOnionOothappam = new JRadioButton("SpecialOnionOothappam");
      private String msg = "";
      private ButtonGroup group = new ButtonGroup();
      
      private static final Font messageFont = new Font("Sans Serif", Font.BOLD, 20);
      private static final Font listFont = new Font("Sans Serif", Font.BOLD, 14);
      
      private static Logger logger = Logger.getLogger("GridbagLayout");
      private String[] data = {"MasalaDosa", 
                               "RavaMuruval", 
                               "SpecialOnionOothappam"};
      
      public GridBagLayoutPanel() {
		
            setSize(200, 150);
		        
            cbSupermarketItem = new JComboItems();
            cbSupermarketItem.addActionListener(this);

            label = new JLabel("Food item: ");
            label.setFont(messageFont);

            RadioListener radioListener = new RadioListener();
            buttonMasalaDosa.addActionListener(radioListener);
            buttonRavaMuruval.addActionListener(radioListener);
            buttonSpecialOnionOothappam.addActionListener(radioListener);
            buttonMasalaDosa.setMnemonic('1');
            buttonRavaMuruval.setMnemonic('2');
            buttonSpecialOnionOothappam.setMnemonic('3');

            group.add(buttonMasalaDosa);
            group.add(buttonRavaMuruval);
            group.add(buttonSpecialOnionOothappam);
            
            JPanel radioPanel = new JPanel();
            radioPanel.setLayout(new GridLayout(0, 1));
            radioPanel.add(buttonMasalaDosa);
            radioPanel.add(buttonRavaMuruval);
            radioPanel.add(buttonSpecialOnionOothappam);
            
            final JList list = new JList(data);
            MouseListener mouseListener = new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        logger.info("Double clicked: " + list.locationToIndex(e.getPoint()));
                        msg = data[list.locationToIndex(e.getPoint())];
                        displayMessage();
                    }
                }
            };
            list.setFont(listFont);
            list.addMouseListener(mouseListener);
            JScrollPane listScroller = new JScrollPane(list);
            listScroller.setPreferredSize(new Dimension(100, 125));
            listScroller.setBorder(new TitledBorder("Double-click query for item value"));
            
            topPanel.add(label);            
            topPanel.add(cbSupermarketItem);
            topPanel.add(radioPanel);
            topPanel.setBorder(new TitledBorder("Supermarket"));  
					
            messageText = new JLabel("Please pick a food item...");
            messageText.setFont(messageFont);
            results.add(messageText);	
            results.setPreferredSize(new Dimension(400, 50));
            results.setBorder(BorderFactory.createLineBorder (Color.yellow, 2));
            results.setBackground(Color.orange);
            
            setLayout(new GridBagLayout());
            
            GridBagConstraints c = new GridBagConstraints(); 
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 0.5;
            c.insets = new Insets( 2, 2, 2, 2 );   
            c.fill = GridBagConstraints.BOTH; 
            add(topPanel, c);
            
            c.gridy = 1;
            c.weightx = 0.5;
            c.gridwidth = 1;  
            c.fill = GridBagConstraints.HORIZONTAL; 
            add(listScroller, c);
            
            c.gridx = 0;
            c.gridy =  2;
            c.weightx = 0.0;
            c.insets = new Insets( 50, 50, 0, 0 );
            c.fill = GridBagConstraints.NONE;
            add(results, c);
	   }
	   
       public void actionPerformed(ActionEvent e) {	          
            JComboItems cb = (JComboItems)e.getSource();
            Command obj = (Command)e.getSource();
            msg = (String)cb.getSelectedItem();
            logger.info("[actionPerformed:] " + msg);      

            if (!msg.equals("Pick a food item?")) {
                obj.execute();   
                logger.info("Execute being performed...");           
            }
       }

       class JComboItems extends JComboBox implements Command {
        
          public JComboItems() {
             this.addItem("Pick a food item?");
             this.addItem("MasalaDosa");
             this.addItem("RavaMuruval");
             this.addItem("SpecialOnionOothappam");
             setFont(messageFont);
          }

          public void execute() {
             displayMessage();
          }
       }
       
       public void displayMessage() {
          FoodItemText text = new FoodItemText();
          
          if (msg.equals("MasalaDosa"))
              ((SuperMarket)new MasalaDosa()).accept(text);              
          else if (msg.equals("RavaMuruval"))
              ((SuperMarket)new RavaMuruval()).accept(text); 
          else if (msg.equals("SpecialOnionOothappam"))
              ((SuperMarket)new SpecialOnionOothappam()).accept(text); 
         
          messageText.setFont(messageFont);
          messageText.setText(text.getCost());
          results.add(messageText);
       }
       
       public interface Command {
       	  public void execute();
       }
       
       class RadioListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             logger.info("actionCommand = " + e.getActionCommand());
             msg = e.getActionCommand();
             displayMessage();
          }
       }
       
       static public void main(String argv[]) {
       	  JFrame frame = new JFrame("GridBagLayout");

       	  frame.addWindowListener(new WindowAdapter() {
       		 public void windowClosing(WindowEvent e) {System.exit(0);}
       	  });

       	  frame.getContentPane().add(new GridBagLayoutPanel(), BorderLayout.CENTER);
       	  frame.pack();
       	  frame.setVisible(true);
       }
	   
}


class FoodItemText implements Visitor 
{
    String cost;

    public String getCost() { return cost; }
    public void visit(MasalaDosa masaladosa) 
    {
        cost = "Rs:70";
    }
    public void visit(RavaMuruval ravamuruval) 
    {
        cost = "Rs:50";
    }
    public void visit(SpecialOnionOothappam specialonionoothappam) 
    {
        cost = "Rs:60";
    }

}
class MasalaDosa implements SuperMarket {
   public void accept(Visitor v) { v.visit(this); }
}


class RavaMuruval implements SuperMarket 
{
   public void accept(Visitor v) { v.visit(this); }
}
class SpecialOnionOothappam implements SuperMarket {
   public void accept(Visitor v) { v.visit(this); }
}
interface SuperMarket 
{
  void accept(Visitor v);
}
interface Visitor 
{
    void visit(MasalaDosa masadosa);
    void visit(RavaMuruval ravamuruval);
    void visit(SpecialOnionOothappam specialonionoothappam);
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
gridbaglayout line separators cEz Java GUI 0 September 26th, 2005 09:33 PM



All times are GMT -4. The time now is 04:41 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.