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.
//Set tooltip text
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;
class MailClient 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;
private static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
public MailClient(Frame frame, String title, boolean modal) {
super(frame, title, modal);
init();
}
public MailClient() {
this((Frame)null, "", false);
}
private void init(){
try {
UIManager.setLookAndFeel(windows);
} catch (Exception e1) {
;
}
JFrame.setDefaultLookAndFeelDecorated(true);
this.setSize(400, 400);
//Toolbar in seperate panel and the other components in the seperate panel
JPanel toolbarPanel = new JPanel();
toolbarPanel.setLayout(new GridBagLayout());
GridBagConstraints toolbarCons = new GridBagConstraints();
toolbarCons = new GridBagConstraints(0,0,1,1,0.1,0,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
GridBagConstraints mailPanelCons = new GridBagConstraints();
mailPanelCons = new GridBagConstraints(0,1,1,1,0.1,0,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
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);
mailPanel = new JPanel();
mailPanel.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
cons = new GridBagConstraints(0,0,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jlbTo, cons);
cons = new GridBagConstraints(1,0,4,1,0.8,0.1,
GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0);
mailPanel.add(jtfTo, cons);
cons = new GridBagConstraints(0,1,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jlbCc, cons);
cons = new GridBagConstraints(1,1,4,1,0.8,0.1,
GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0);
mailPanel.add(jtfCc, cons);
cons = new GridBagConstraints(0,2,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jlbBcc, cons);
cons = new GridBagConstraints(1,2,4,1,0.8,0.1,
GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0);
mailPanel.add(jtfBcc, cons);
cons = new GridBagConstraints(0,3,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jlbSubject, cons);
cons = new GridBagConstraints(1,3,4,1,0.8,0.1,
GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0);
mailPanel.add(jtfSubject, cons);
cons = new GridBagConstraints(0,4,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jbtAttachment, cons);
cons = new GridBagConstraints(1,4,4,1,0.8,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jlstAttachedFilesPane, cons);
cons = new GridBagConstraints(1,5,4,4,0.8,0.4,
GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0);
mailPanel.add(mailMessage, cons);
cons = new GridBagConstraints(0,9,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jbtSend, cons);
cons = new GridBagConstraints(1,9,1,1,0.2,0.1,
GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);
mailPanel.add(jbtCancel, cons);
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);
}
});
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setJMenuBar(jmenubar);
jtoolbar.setFloatable(false);
toolbarPanel.add(jtoolbar, toolbarCons);
this.getContentPane().setLayout(new GridBagLayout());
this.getContentPane().add(toolbarPanel, toolbarCons);
this.getContentPane().add(mailPanel, mailPanelCons);
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);
}
}
public class Invoker{
private static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
public static void main(String args[]){
try {
UIManager.setLookAndFeel(windows);
} catch (Exception e1) {
;
}
MailClient mc = new MailClient(null, "Mail Client", true);
mc.pack();
mc.setVisible(true);
}
}