Results 1 to 4 of 4
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default Gridbag Layout Probelm

    Hi,
    I am a student and have to submit a Mail Program. I have been asked to do the program using GridBagLayout. The program is working fine but for the Layout. I just am not able to manage the layout problems I am facing in this program. Could I get some help to set this right. I don't want to use any other layout. But I just don't believe how weired Layout Problems could get.

    I know where the problem lies. Its with the weights Assigned to the components. I just can't am not able to solve this problem. I am facing this alignment issue when I try to resize my Window.

    http://www.beginner-java-tutorial.com/Mail.zip
    Attached Files Attached Files

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I know where the problem lies. Its with the weights Assigned to the components. I just can't am not able to solve this problem. I am facing this alignment issue when I try to resize my Window.
    I have run your application and see the dialog with components displayed. I can drag the edges of the dialog to resize it and I see what is happening. I do not know how you want things to behave other than what I see. Can you please describe in detail what you want for the behavior of the layout. How must it behave differently to what it does now?

  3. #3
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default

    Hi,

    See, basically I used GrdiBag since it was as per the requirement. Secondly what I am looking for is when I resize the dialog I want even the components to resize (become smaller or larger) when the dialog is resized (smaller or larger) with the relative proportional sizes maintained. What is happening now is all the components get centered and the whole layout gets disturbed. When I resize I don't want the tool bar to float down, but stay where it is.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.util.HashMap;
    import javax.swing.*;
    
    public class InvokerRx {
    	public static void main(String[] args) {
    		String windows = "com.sun.java.swing" +
                                 ".plaf.windows.WindowsLookAndFeel";
    		try {
    			UIManager.setLookAndFeel(windows);
    		} catch (Exception e) {
    			System.out.println(e.getClass().getName() + ": " +
                                         e.getMessage());
    		}
    		new MailClientRx(null, "Mail Client", true);
    	}
    }
    
    class MailClientRx extends JDialog {
    	private JPanel mailPanel = null;
    	private JTextArea jtaMessage = null;
    
    	private JButton jbtSend = null;
    	private JButton jbtCancel = null;
    	private JButton jbtAttachment = null;
    
    	private JLabel jlbTo = null;
    	private JLabel jlbCc = null;
    	private JLabel jlbBcc = null;
    	private JLabel jlbSubject = null;
    	private JList jlstAttachedFiles = null;
    	private JScrollPane jlstAttachedFilesPane = null;
    	
    	private JTextField jtfTo = null;
    	private JTextField jtfCc = null;
    	private JTextField jtfBcc = null;
    	private JTextField jtfSubject = null;
    	
    	private JFileChooser jfcOpenChooser = null;
    	private JMenuBar jmenubar = null;
    	private JMenu jmenuFile = null;
    	private JMenu jmenuHelp = null;
    	private JMenu jmenuEdit = null;
    	private JMenuItem jmenuitemNew = null;
    	private JMenuItem jmenuitemCut = null;
    	private JMenuItem jmenuitemCopy = null;
    	private JMenuItem jmenuitemPaste = null;
    	private JMenuItem jmenuitemAttachments = null;
    	private JMenuItem jmenuitemHelp = null;
    	private JMenuItem jmenuitemExit = null;
    	
    	private JButton jbtCut = null;
    	private JButton jbtCopy = null;
    	private JButton jbtPaste = null;
    	private JButton jbtHelp = null;
    	private JButton jbtAttachmentsTb = null;
    	
    	private JToolBar jtoolbar = null;
    	private ImageIcon cutIcon = null;
    	private ImageIcon copyIcon = null;
    	private ImageIcon helpIcon = null;
    	private ImageIcon pasteIcon = null;
    	private ImageIcon attachmentsIcon = null;
    	JScrollPane mailMessage;
    	
    	public MailClientRx(Frame frame, String title, boolean modal) {
    		super(frame, title, modal);
    		initComponents();
                layoutComponents();
    	}
    	
    	public MailClientRx() {
    		this((Frame)null, "", false);
    	}
    	
    	private void initComponents(){
    	 	JFrame.setDefaultLookAndFeelDecorated(true);
    		this.setSize(400, 400);
    		
    		jlbTo = new JLabel("To");
    		jlbCc = new JLabel("CC");
    		jlbBcc = new JLabel("BCC");
    		jlbSubject = new JLabel("Subject");
    		jlstAttachedFiles = new JList();
    		jlstAttachedFilesPane = new JScrollPane(jlstAttachedFiles);
    		jlstAttachedFilesPane.setPreferredSize(new Dimension(150, 75));
    		jbtAttachment = new JButton("Attachments");
    		jbtSend = new JButton("Send");
    		jbtCancel = new JButton("Cancel");
    		jtfTo = new JTextField() ;
    		jtfCc = new JTextField();
    		jtfBcc = new JTextField();
    		jtfSubject = new JTextField();
    		jtaMessage = new JTextArea();
    		jtaMessage.setPreferredSize(new java.awt.Dimension(300, 200));
    		jtaMessage.setMinimumSize(new java.awt.Dimension(100, 50));
    		/*JScrollPane*/ mailMessage = new JScrollPane(jtaMessage);
    		jtaMessage.setLineWrap(true);
    		jfcOpenChooser = new JFileChooser();
    		jtoolbar = new JToolBar();
    		cutIcon = new ImageIcon("cut.jpg");
    		copyIcon = new ImageIcon("copy.jpg");
    		helpIcon = new ImageIcon("help.gif");
    		pasteIcon = new ImageIcon("paste.jpg");
    		attachmentsIcon = new ImageIcon("attachments.jpg");
    		jmenubar = new JMenuBar();
    		jmenuFile = new JMenu("File");
    		jmenuHelp = new JMenu("Help");
    		jmenuEdit = new JMenu("Edit");
    		jmenuitemNew = new JMenuItem("New Mail");
    		jmenuitemCut = new JMenuItem("Cut", cutIcon);
    		jmenuitemCopy = new JMenuItem("Copy", copyIcon);
    		jmenuitemPaste = new JMenuItem("Paste", pasteIcon);
    		jmenuitemAttachments = new JMenuItem("Attachments", attachmentsIcon);
    		jmenuitemHelp = new JMenuItem("Help", helpIcon);
    		jmenuitemExit = new JMenuItem("Exit");
    	
    		jbtCut = new JButton(cutIcon);
    		jbtCopy = new JButton(copyIcon);
    		jbtPaste = new JButton(pasteIcon);
    		jbtHelp = new JButton(helpIcon);
    		jbtAttachmentsTb = new JButton(attachmentsIcon);
    		
    		jbtCut.setToolTipText("Cut");
    		jbtCopy.setToolTipText("Copy");
    		jbtPaste.setToolTipText("Paste");
    		jbtHelp.setToolTipText("Help");
    		jbtAttachmentsTb.setToolTipText("Attachment");
    		
    		jmenuFile.add(jmenuitemNew);
    		jmenuFile.add(jmenuitemCut);
    		jmenuEdit.add(jmenuitemCopy);
    		jmenuEdit.add(jmenuitemPaste);
    		jmenuEdit.add(jmenuitemAttachments);
    		jmenuHelp.add(jmenuitemHelp);
    		jmenuHelp.add(jmenuitemExit);
    		jmenubar.add(jmenuFile);
    		jmenubar.add(jmenuEdit);
    		jmenubar.add(jmenuHelp);
    		
    		jtoolbar.add(jbtCut);
    		jtoolbar.add(jbtCopy);
    		jtoolbar.add(jbtPaste);
    		jtoolbar.add(jbtHelp);
    		jtoolbar.add(jbtAttachmentsTb);
    
    		jbtSend.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				jbtOK_actionPerformed(e);
    			}
    		});
    		
    		jbtCancel.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				jbtCancel_actionPerformed(e);
    			}
    		});
    		
    		jbtAttachment.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				jbtAttachment_actionPerformed(e);
    			}
    		});
     	}
    
    	private void layoutComponents() {
    		this.setLayout(new GridBagLayout());
    		GridBagConstraints toolbarCons = new GridBagConstraints();
    		toolbarCons = new GridBagConstraints(0,0,1,1,0.1,0,
    								GridBagConstraints.WEST,
    								GridBagConstraints.BOTH,
    								new Insets(5,5,0,5),0,0);  
    		mailPanel = new JPanel();
    		mailPanel.setLayout(new GridBagLayout());
    		GridBagConstraints cons = new GridBagConstraints();
    		cons = new GridBagConstraints(0,0,1,1,0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0);  
    		mailPanel.add(jlbTo, cons);
    		
    		cons = new GridBagConstraints(1,0,4,1,1.0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.BOTH,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jtfTo, cons);
    		
    		cons = new GridBagConstraints(0,1,1,1,0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jlbCc, cons);
    		
    		cons = new GridBagConstraints(1,1,4,1,1.0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.BOTH,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jtfCc, cons);
    		
    		cons = new GridBagConstraints(0,2,1,1,0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jlbBcc, cons);
    		
    		cons = new GridBagConstraints(1,2,4,1,1.0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.BOTH,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jtfBcc, cons);
    		
    		cons = new GridBagConstraints(0,3,1,1,0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jlbSubject, cons);
    		
    		cons = new GridBagConstraints(1,3,4,1,1.0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.BOTH,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jtfSubject, cons);
    		
    		cons = new GridBagConstraints(0,4,1,1,0,1.0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jbtAttachment, cons);
    		
    		cons = new GridBagConstraints(1,4,1,1,1.0,1.0,  // 4 col
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jlstAttachedFilesPane, cons);
    		
    		cons = new GridBagConstraints(1,5,1,1,1.0,1.0,  // 4c 4r
    							GridBagConstraints.WEST,
    							GridBagConstraints.BOTH,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(mailMessage, cons);
    		
    		cons = new GridBagConstraints(0,9,1,1,0,0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0); 
    		mailPanel.add(jbtSend, cons);
    		
    		cons = new GridBagConstraints(1,9,1,1,0,0,
    							GridBagConstraints.WEST,
    							GridBagConstraints.NONE,
    							new Insets(5,5,0,5),0,0);
    		mailPanel.add(jbtCancel, cons);
    		
    //		this.getContentPane().add(mailPanel, toolbarCons);
    		
    		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    		
    		this.setJMenuBar(jmenubar);
    		jtoolbar.setFloatable(false);
    		this.getContentPane().add(jtoolbar, toolbarCons);
    		GridBagConstraints mainPanelCons = new GridBagConstraints();
    		mainPanelCons = new GridBagConstraints(0,1,1,1,1.0,1.0,
    								GridBagConstraints.WEST,
    								GridBagConstraints.BOTH,
    								new Insets(5,5,0,5),0,0);  
    		this.getContentPane().add(mailPanel, mainPanelCons);
    		this.pack();
    		this.setVisible(true);
    	}
    
    	private void jbtOK_actionPerformed(ActionEvent e) {
    		//	Business Logic
    		this.dispose();
    	}
    
    	private void jbtCancel_actionPerformed(ActionEvent e) {
    		this.dispose();
    	}
    
    	private void jbtAttachment_actionPerformed(ActionEvent e) {
    		jfcOpenChooser.setMultiSelectionEnabled(true);
    		jfcOpenChooser.showOpenDialog(null);
    		File[] fileArray = jfcOpenChooser.getSelectedFiles();
    		String[] fileNames = new String[fileArray.length];
    		for(int i=0; i < fileArray.length; i++){
    			File file = fileArray[i];
    			fileNames[i] = file.getPath().trim();
    //			fileNames[i] = file.getName().trim();
    		}
    		jlstAttachedFiles.setListData(fileNames);
    	}
    	
    }

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 07:23 AM
  2. Layout Managers
    By gmioannou in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 04:12 AM
  3. Dom4j probelm with clearContent method
    By aorteu in forum New To Java
    Replies: 0
    Last Post: 11-23-2007, 03:36 PM
  4. MiG Layout Manager 2.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-16-2007, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •