View Single Post
  #1 (permalink)  
Old 11-30-2007, 12:07 PM
Gajesh Tripathi Gajesh Tripathi is offline
Member
 
Join Date: Aug 2007
Posts: 19
Gajesh Tripathi is on a distinguished road
Waste Space & Text Field
I have 2 problem in my program (source code is attached):-

1. When I run my program then I have lot of space at top,bottom,left & right space which I don't want at all.
2. Text Field size is not set (I dont' want to use fill=BOTH for text fields)

Plz modify my source with solution of my problem then return me..

HERE IS SOURCE:

Code:
import javax.swing.*; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.FlowLayout; import java.awt.Insets; class PnlTestResultPreview extends JPanel { private JLabel rno,vno; private JTextField txtRNo,txtVNo; private JLabel lblPtName,lblConsultBy; private JButton btnOk; private JTextField txtPtName,txtConsultBy; private JTable gridPreview; PnlTestResultPreview() { addComponents(); } private void addComponents() { GridBagLayout gb=new GridBagLayout() { public Insets getInsets() { return new Insets(0,0,0,0); } }; GridBagConstraints gbc; gb=new GridBagLayout(); gbc=new GridBagConstraints(); setLayout(gb); //ADD First Row //Adding 1st & 2nd Col... gbc.anchor=GridBagConstraints.WEST; rno=new JLabel("Test Recpt. No.:"); add(rno,gbc); txtRNo=new JTextField(); txtRNo.setSize(40,rno.getHeight()); txtRNo.setEditable(false); add(txtRNo,gbc); //Adding 3rd Col... gbc.fill=GridBagConstraints.HORIZONTAL; gbc.gridwidth=GridBagConstraints.REMAINDER; JPanel pnlTemp=new JPanel(new FlowLayout(FlowLayout.RIGHT)); vno=new JLabel("Voucher No.:"); txtVNo=new JTextField(); txtVNo.setSize(40,vno.getHeight()); txtVNo.setEditable(false); pnlTemp.add(vno); pnlTemp.add(txtVNo); add(pnlTemp,gbc); gbc.fill=GridBagConstraints.NONE; //ADD Second Row //Adding 1st Col... gbc.gridwidth=1; lblPtName=new JLabel("Pt. Name"); txtPtName=new JTextField(); add(lblPtName,gbc); //Addding 2nd Col... gbc.fill=GridBagConstraints.HORIZONTAL; gbc.gridwidth=GridBagConstraints.REMAINDER; add(txtPtName,gbc); gbc.fill=GridBagConstraints.NONE; //ADD Third Row //Adding 1st Col... gbc.gridwidth=1; lblPtName=new JLabel("Consult By"); txtPtName=new JTextField(); add(lblPtName,gbc); //Addding 2nd Col... gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.fill=GridBagConstraints.HORIZONTAL; add(txtPtName,gbc); gbc.fill=GridBagConstraints.NONE; //ADD Fourth Row gbc.fill=GridBagConstraints.BOTH; gridPreview=new JTable(); add(new JScrollPane(gridPreview),gbc); //Add Fifth Row gbc.fill=GridBagConstraints.NONE; gbc.anchor=GridBagConstraints.EAST; btnOk=new JButton("OK"); btnOk.setMnemonic('O'); add(btnOk,gbc); btnOk.requestFocusInWindow(); } private static void showPanel() { final JFrame jf=new JFrame("GridBag Test"); PnlTestResultPreview pnlGridBag=new PnlTestResultPreview(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setContentPane(pnlGridBag); jf.setPreferredSize(new java.awt.Dimension(300,250)); jf.setLocationByPlatform(true); jf.pack(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { jf.setVisible(true); } }); } public static void main (String[] args) { showPanel(); } }
Help Me
Gajesh Tripathi
Reply With Quote
Sponsored Links