I am using the GridLayout to layout to layout JLabels and JTextfields in an JPanel. I put that JPanel into an JScrollPane. But the problem is i am not able to maintain consistent gaping among the compnents.Is there anyway in grid layout where we can maintain spaces uniformly?
my code i m attaching here:
package com.bofa.crme.dac.client.wizard.addoopexpense;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import com.bofa.crme.dac.app.model.AddOOPExpenseDetailsModel;
import com.bofa.crme.dac.app.model.AddOOPExpenseModel;
import com.bofa.crme.dac.app.util.DAConstants;
import com.bofa.crme.dac.client.beans.EntryField;
import com.bofa.crme.dac.client.businessobjects.Deal;
import com.bofa.crme.dac.client.businessobjects.SyndicateDeal;
import com.bofa.crme.dac.client.businessobjects.SyndicateMember;
import com.bofa.crme.dac.client.wizard.Wizard;
import com.bofa.crme.dac.client.wizard.WizardPanel;
import com.bofa.crme.dac.client.wizard.WizardSubPanel;
import com.bofa.crme.dac.client.wizard.payment.BottomBorder;
import com.bofa.crme.dac.persistence.valueobject.InvoiceFeeVO;
public class AddCoManagedOOPExpensePaymentPanel extends JPanel implements WizardSubPanel{
private JLabel lblLeadExpenseandPayment = new JLabel();
JLabel lblSelect = new JLabel("<html><u>Select</u></html>");
private JLabel lblName = new JLabel();
private JLabel lblRole = new JLabel();
//private JLabel lblSettled = new JLabel();
JLabel lblSettled = new JLabel();
private JLabel lblInvDate = new JLabel();
private JLabel lblInvAmnt = new JLabel();
private JLabel lblPayAmnt = new JLabel();
private JLabel lblGlDeatils = new JLabel();
private JLabel lblInfo = new JLabel();
private JLabel lblDetails= new JLabel();
private JLabel lblRemainingExpReserve = new JLabel();
private JTextField tfRemainingExpReserve = new JTextField();
GridBagLayout gridBagLayout1 = new GridBagLayout();
//private CoManagerPaymentDetailsTableModel detailsModel = null;
private CoManagerExpensesAndPaymentsPanel detailsPanel = null;
//private JTable pmtTable = new JTable();
JScrollPane scrollPane = new JScrollPane();
private JLabel lblTotalCoMgrOop = new JLabel();
private JLabel lblDifference = new JLabel();
private JTextField tfTotalInvAmt = new JTextField();
private JTextField tfDifference = new JTextField();
private JTextField tfTotalPaymentAmt = new JTextField();
private JPanel diffPanel = new JPanel();
private JPanel totalPanel = new JPanel();
private JButton btnZeroOut = new JButton();;
private JButton btnPayAll = new JButton();;
private JPanel btnPanel = new JPanel();
private JPanel expReservePanel = new JPanel();
FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 0, 0);
JPanel holder = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel rootPanel = new JPanel();
JPanel labelPanel = new JPanel();
JPanel oopExpensePanel = new JPanel();
List oopExpenseList = new ArrayList();
List rowList = new ArrayList();
private String name;
private String role;
private String dealID;
boolean isEditMode = false;
AddOOPExpensePanel owningPanel;
public AddCoManagedOOPExpensePaymentPanel() {
try{
jbInit();
}catch (Exception ex)
{
ex.printStackTrace();
}
}
public AddCoManagedOOPExpensePaymentPanel(AddOOPExpensePanel owningPanel, boolean isEditMode)
{
try
{
jbInit();
this.isEditMode = isEditMode;
this.owningPanel = owningPanel;
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private void jbInit() {
this.setLayout(gridBagLayout1);
tfRemainingExpReserve.setPreferredSize(new Dimension(175, 19));
lblLeadExpenseandPayment.setText("Enter Co-Manager OOP Expenses");
lblRemainingExpReserve.setText("REMAINING EXPENSE RESERVE");
btnZeroOut.setText("Zero Out");
btnPayAll.setText("Pay All");
lblTotalCoMgrOop.setText("Total Co-Manager OOP");
lblDifference.setText("Difference");
lblInfo.setText("*Co-Manager OOP expenses can NOT be negative.");
lblName.setBorder(new BottomBorder());
lblRole.setBorder(new BottomBorder());
lblSettled.setBorder(new BottomBorder());
lblInvDate.setBorder(new BottomBorder());
lblPayAmnt.setBorder(new BottomBorder());
lblInvAmnt.setBorder(new BottomBorder());
lblGlDeatils.setBorder(new BottomBorder());
//lblSelect.setText(" Select");
lblName.setText(" Name ");
lblRole.setText(" Role ");
lblSettled.setText(" Settled?");
lblInvDate.setText(" Invoice Date");
lblPayAmnt.setText(" Payment Amount");
lblInvAmnt.setText(" Invoice Amount");
lblDetails.setText(" ");
lblGlDeatils.setText("Co/CC/Account");
lblSelect.setPreferredSize(new Dimension(100,19));
lblName.setPreferredSize(new Dimension(100,19));
lblRole.setPreferredSize(new Dimension(100,19));
lblSettled.setPreferredSize(new Dimension(100,19));
lblInvDate.setPreferredSize(new Dimension(100,19));
lblPayAmnt.setPreferredSize(new Dimension(100,19));
lblInvAmnt.setPreferredSize(new Dimension(100,19));
lblDetails.setPreferredSize(new Dimension(100,19));
lblGlDeatils.setPreferredSize(new Dimension(100,19));
this.setLayout(flowLayout1);
rootPanel.setLayout(gridBagLayout1);
labelPanel.setLayout(gridBagLayout1);
labelPanel.add(lblSelect,new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,-3,10), 0, 0));
labelPanel.add(lblName,new GridBagConstraints(1,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,-60,2,10), 0, 0));
labelPanel.add(lblRole,new GridBagConstraints(2,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,10), 0, 0));
labelPanel.add(lblSettled,new GridBagConstraints(3,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
labelPanel.add(lblInvDate,new GridBagConstraints(4,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,-30,-3,2), 0, 0));
labelPanel.add(lblInvAmnt,new GridBagConstraints(5,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,-5,-3,10), 0, 0));
labelPanel.add(lblPayAmnt,new GridBagConstraints(6,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,10), 0, 0));
labelPanel.add(lblDetails,new GridBagConstraints(7,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,10), 0, 0));
labelPanel.add(lblGlDeatils,new GridBagConstraints(8,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,-40,2,2), 0, 0));
/*labelPanel.add(lblSelect,null);
labelPanel.add(lblName,null);
labelPanel.add(lblRole,null);
labelPanel.add(lblSettled,null);
labelPanel.add(lblInvDate,null);
labelPanel.add(lblInvAmnt,null);
labelPanel.add(lblPayAmnt,null);
labelPanel.add(lblDetails,null);
labelPanel.add(lblGlDeatils,null);*/
tfRemainingExpReserve.setPreferredSize(new Dimension(100, 19));
tfRemainingExpReserve.setAlignmentX(JPanel.LEFT_ALIGNMENT);
expReservePanel.add(tfRemainingExpReserve, null);
tfDifference.setPreferredSize(new Dimension(100, 19));
tfDifference.setAlignmentX(JPanel.LEFT_ALIGNMENT);
diffPanel.add(tfDifference, null);
tfTotalInvAmt.setPreferredSize(new Dimension(100, 19));
tfTotalPaymentAmt.setPreferredSize(new Dimension(100, 19));
totalPanel.add(tfTotalInvAmt, null);
tfTotalInvAmt.setAlignmentX(JPanel.LEFT_ALIGNMENT);
totalPanel.add(tfTotalPaymentAmt, null);
lblLeadExpenseandPayment.setFont(new Font("Tahoma", Font.BOLD, 14));
lblLeadExpenseandPayment.setVerticalAlignment(SwingConstants.TOP);
lblLeadExpenseandPayment.setHorizontalAlignment(SwingConstants.CENTER);
rootPanel.add(lblLeadExpenseandPayment, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(2, 300, 2, 2), 0, 0));
rootPanel.add(lblRemainingExpReserve, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0));
rootPanel.add(expReservePanel,new GridBagConstraints(1, 1, 1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(2, 2, 2, 250), 0, 0));
rootPanel.add(labelPanel,new GridBagConstraints(0, 2, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
//======== scrollPane2 ========
{
oopExpensePanel.setLayout(new GridBagLayout());
scrollPane.setPreferredSize(new Dimension(900,120));
scrollPane.setViewportView(oopExpensePanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
rootPanel.add(scrollPane,new GridBagConstraints(0, 3, 4, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
btnPanel.setLayout(gridBagLayout1);
btnPanel.add(btnPayAll,new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(2, 90, 2, 2), 0, 0));
btnPanel.add(btnZeroOut,new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(2, 27, 2, 2), 0, 0));
rootPanel.add(btnPanel,new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(2, 250, 2, 2), 0, 0));
rootPanel.add(lblTotalCoMgrOop, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 20, 2, 2), 0, 0));
rootPanel.add(totalPanel,new GridBagConstraints(1, 5, 1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(2, -170, 2, 2), 0, 0));
rootPanel.add(lblDifference, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 30, 2, 2), 0, 0));
rootPanel.add(diffPanel,new GridBagConstraints(1, 6, 1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(2, -85, 2, 2), 0, 0));
rootPanel.add(lblInfo, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0));
holder.setLayout(borderLayout1);
holder.add(rootPanel, BorderLayout.WEST);
this.add(holder);
// this.tfTotalInvAmt.setText(detailsModel.calculateTotalInvoiceAmt().toString());
// this.tfTotalPaymentAmt.setText(detailsModel.calculateTotalPaymentAmt().toString());
// this.tfDifference.setText(syndTeamModel.totalDiffAmount.toString());
btnZeroOut.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnZeroOut_actionPerformed(e);
}
});
btnPayAll.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnPayAll_actionPerformed(e);
}
});
}
void btnZeroOut_actionPerformed(ActionEvent e){
//syndTeamModel.clearData();
}
void btnPayAll_actionPerformed(ActionEvent e){
//syndTeamModel.setAmount();
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public void setRole(String newRole)
{
role = newRole;
}
public String getRole()
{
return role;
}
// public void setBusinessObject(UnderwriterTeamAssignmtVO[] syndicateMember){
// System.out.println("In business Obj"+syndicateMember.length);
// this.businessObject = syndicateMember;
// }
// public UnderwriterTeamAssignmtVO[] getBusinessObject(){
// return businessObject;
// }
/**
* Set the deal data to the panel.
* @param deal
*/
public void setDeal(Deal deal){
SyndicateMember[] syndicateMembers = ((SyndicateDeal)deal).getSyndicateMembers();
int memberLength = syndicateMembers.length;
for(int i = 0; i < memberLength; i++){
OOPExpenseModel newModel = new OOPExpenseModel();
Calendar current = GregorianCalendar.getInstance();
newModel.setSyndicateMember(syndicateMembers);
//newModel.setRole(syndicateMembers[i].getRole());
newModel.setInvoiceAmount(new BigDecimal("0.00"));
newModel.setPaymentAmount(new BigDecimal("0.00"));
newModel.setInvoiceDate(current);
//newModel.setCompCCAccount("compCCAccount");
oopExpenseList.add(newModel);
}
// Show oopExpense data
addOOPExpenseComponents(oopExpenseList);
System.out.println(" oopexpenselist size " + oopExpenseList.size());
}
/**
* Add the OOPExpense data to the UI.
* @param oopExpModelList List of OOExpenseModels
*/
public void addOOPExpenseComponents(List oopExpModelList){
int rowIndex = 0;
Iterator oopIterator = oopExpModelList.iterator();
while(oopIterator.hasNext()){
OOPExpenseModel model = (OOPExpenseModel) oopIterator.next();
Row newRow = new Row(rowIndex, model, this.owningPanel);
oopExpensePanel.add(newRow, new GridBagConstraints(0, rowIndex, 1, 1, 1.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
//add the new row to the list
rowList.add(newRow);
rowIndex ++;
}
this.validate(); // Relayout the UI.
}
public List getAddOOPExpenseModels(){
List modelList = new ArrayList();
Iterator rowIterator = rowList.iterator();
while(rowIterator.hasNext()){
Row row = (Row) rowIterator.next();
OOPExpenseModel rowModel = row.getRowModel();
if(rowModel.isEntered()){
AddOOPExpenseModel addOOPExpenseModel = new AddOOPExpenseModel();
addOOPExpenseModel.setType(DAConstants.OOP_CO_MANAGED_TYPE);
addOOPExpenseModel.setDealId(this.getDealID());
addOOPExpenseModel.setSyndicateMember(rowModel.getSyndicateMember().getId());
addOOPExpenseModel.setInvoiceDate(rowModel.getInvoiceDate());
addOOPExpenseModel.setPaymentAmount(rowModel.getPaymentAmount());
addOOPExpenseModel.setInvoiceAmount(rowModel.getInvoiceAmount());
addOOPExpenseModel.setOopExpenseDetailModel(rowModel.getOopExpEntriesList());
modelList.add(addOOPExpenseModel);
}
}
return modelList;
}
public String getDealID() {
return "100";
}
public void setDealID(String deal) {
dealID = deal;
}
public String validateData() {
// TODO Auto-generated method stub
return null;
}
/**
* This represents each row in the OOPExpensePanel.
* @author Accenture
*
*/
class Row extends JPanel{
JLabel nameLbl = new JLabel();
JLabel roleLbl = new JLabel();
JLabel settledLabel = new JLabel();
EntryField invDateField = new EntryField();
JTextField invAmtField = new JTextField();
JTextField payAmtField = new JTextField();
JButton detailsButton = new JButton("Add/Edit");
JLabel coCCAccLbl = new JLabel();
JCheckBox selectCheckBox = new JCheckBox();
OOPExpenseModel currentModel = null; // Current oopExpense data
AddOOPExpensePanel owningPanel;
public Row(int rowIndex, OOPExpenseModel model, AddOOPExpensePanel owningPanel){
/* GridLayout rowLayout = new GridLayout(0, 9,5,0);
rowLayout.setHgap(3);
this.setLayout(rowLayout);
this.setPreferredSize(new Dimension(750, 20));
currentModel = model; // Set the model for the row.
this.owningPanel = owningPanel; //set the owning panel.
//Set data for all UI Fields
nameLbl.setText(model.getSyndicateMember().getName());
roleLbl.setText(model.getSyndicateMember().getRole());
coCCAccLbl.setText("Comp/CC/Account");
//Add Checkbox
this.add(selectCheckBox);
// Add Syndicate Member name field
this.add(nameLbl);
// Add Role field
this.add(roleLbl);
// Add Settled field
this.add(settledLabel);
// Add Invoice date field
this.add(invDateField);
// Add Invoice Amount field
this.add(invAmtField);
// Add Payment Amount field
this.add(payAmtField);
// Add Details button
this.add(detailsButton);
// Add Co CC Account field
this.add(coCCAccLbl);
*/
/*GridBagLayout gridBagLayout = new GridBagLayout();
this.setLayout(gridBagLayout);
//settledLabel.setPreferredSize(new Dimension(10,19));
this.add(selectCheckBox,new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0));
this.add(nameLbl,new GridBagConstraints(1,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(roleLbl,new GridBagConstraints(2,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(settledLabel,new GridBagConstraints(3,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(invDateField,new GridBagConstraints(4,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(invAmtField,new GridBagConstraints(5,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(payAmtField,new GridBagConstraints(6,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(detailsButton,new GridBagConstraints(7,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));
this.add(coCCAccLbl,new GridBagConstraints(8,0,1,1,1.0,0.0,GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0));*/
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT,3,0);
this.setPreferredSize(new Dimension(900, 20));
currentModel = model; // Set the model for the row.
this.owningPanel = owningPanel; //set the owning panel.
//Set data for all UI Fields
nameLbl.setText(model.getSyndicateMember().getName());
roleLbl.setText(model.getSyndicateMember().getRole());
coCCAccLbl.setText("Comp/CC/Account");
//Add Checkbox
this.add(selectCheckBox);
// Add Syndicate Member name field
this.add(nameLbl);
// Add Role field
this.add(roleLbl);
// Add Settled field
this.add(settledLabel);
// Add Invoice date field
this.add(invDateField);
// Add Invoice Amount field
this.add(invAmtField);
// Add Payment Amount field
this.add(payAmtField);
// Add Details button
this.add(detailsButton);
// Add Co CC Account field
this.add(coCCAccLbl);
detailsButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button_actionPerformed(e);
}
});
}
private void button_actionPerformed(ActionEvent e){
/*CoManagerExpensesAndPaymentsPanel detailsPanel = new CoManagerExpensesAndPaymentsPanel(this.owningPanel.getOwningWizard().getOwningFrame(), currentModel );
//CoManagerExpensesAndPaymentsPanel detailsPanel = new CoManagerExpensesAndPaymentsPanel();
detailsPanel.setVisible(true);
this.setData(); //set data in summary screen after user enters data & clicks 'ok' in details panel
*/
}
//set data in summary screen after user enters data & clicks 'ok' in details panel
public void setData(){
currentModel.setEntered(true);
invDateField.setObject(currentModel.getInvoiceDate());
invAmtField.setText(currentModel.getInvoiceAmount().toString());
payAmtField.setText(currentModel.getPaymentAmount().toString());
}
//return the oop expense model
public OOPExpenseModel getRowModel(){
return this.currentModel;
}
}
}
please help me soon..Thanks in advance