Results 1 to 3 of 3
Thread: Waste Space & Text Field
- 11-30-2007, 11:07 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 40
- Rep Power
- 0
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:
Help MeJava 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(); } }
Gajesh Tripathi
- 11-30-2007, 10:58 PM #2
Java Code:import java.awt.*; import javax.swing.*; class PTRP extends JPanel { // private JLabel rno,vno; private JTextField txtRNo,txtVNo; // private JLabel lblPtName,lblConsultBy; private JButton btnOk; private JTextField txtPtName,txtConsultBy; private JTable gridPreview; PTRP() { addComponents(); } private void addComponents() { GridBagLayout gb=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); gbc.insets = new Insets(0,0,0,0); gbc.weightx = 1.0; gbc.weighty = 1.0; setLayout(gb); //ADD First Row //Adding 1st & 2nd Col... JPanel row1 = new JPanel(gb); gbc.insets = new Insets(2,2,2,2); gbc.anchor=GridBagConstraints.WEST; JLabel rno=new JLabel("Test Recpt. No.:"); row1.add(rno, gbc); txtRNo=new JTextField(8); // txtRNo.setSize(40,rno.getHeight()); txtRNo.setEditable(false); row1.add(txtRNo, gbc); //Adding 3rd Col... // gbc.fill=GridBagConstraints.HORIZONTAL; // gbc.gridwidth=GridBagConstraints.REMAINDER; // JPanel pnlTemp=new JPanel(new FlowLayout(FlowLayout.RIGHT)); JLabel vno=new JLabel("Voucher No.:"); txtVNo=new JTextField(8); // txtVNo.setSize(40,vno.getHeight()); txtVNo.setEditable(false); row1.add(vno, gbc); row1.add(txtVNo, gbc); gbc.insets = new Insets(0,0,0,0); gbc.gridwidth=2; gbc.fill=GridBagConstraints.HORIZONTAL; add(row1, gbc); gbc.fill=GridBagConstraints.NONE; gbc.gridy = 1; //ADD Second Row //Adding 1st Col... gbc.gridwidth=1; gbc.weightx = 0; JLabel lblPtName=new JLabel("Pt. Name"); txtPtName=new JTextField(); add(lblPtName, gbc); //Addding 2nd Col... // gbc.gridy = GridBagConstraints.RELATIVE; gbc.weightx = 1.0; gbc.fill=GridBagConstraints.HORIZONTAL; // gbc.gridwidth=GridBagConstraints.REMAINDER; add(txtPtName, gbc); gbc.fill=GridBagConstraints.NONE; gbc.gridy++; //ADD Third Row //Adding 1st Col... // gbc.gridwidth=1; gbc.weightx = 0; lblPtName=new JLabel("Consult By"); txtPtName=new JTextField(); add(lblPtName, gbc); //Addding 2nd Col... // gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.weightx = 1.0; gbc.fill=GridBagConstraints.HORIZONTAL; add(txtPtName, gbc); gbc.fill=GridBagConstraints.NONE; gbc.gridy++; //ADD Fourth Row // gbc.fill=GridBagConstraints.BOTH; gbc.gridwidth = 2; gridPreview=new JTable(2,2); Dimension d = gridPreview.getPreferredSize(); System.out.printf("d = [%d, %d]%n", d.width, d.height); d.width = 300; gridPreview.setPreferredScrollableViewportSize(d); gbc.anchor=GridBagConstraints.CENTER; gbc.fill=GridBagConstraints.HORIZONTAL; add(new JScrollPane(gridPreview), gbc); //Add Fifth Row gbc.fill=GridBagConstraints.NONE; gbc.anchor=GridBagConstraints.EAST; gbc.gridy++; btnOk=new JButton("OK"); btnOk.setMnemonic('O'); add(btnOk,gbc); btnOk.requestFocusInWindow(); } private static void showPanel() { final JFrame jf=new JFrame("GridBag Test"); PTRP pnlGridBag=new PTRP(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setContentPane(pnlGridBag); // jf.setPreferredSize(new Dimension(300,250)); jf.setLocationByPlatform(true); jf.pack(); SwingUtilities.invokeLater(new Runnable() { public void run() { jf.setVisible(true); } }); Dimension d = pnlGridBag.getPreferredSize(); Dimension s = pnlGridBag.getSize(); System.out.printf("pnlGridBag d = [%d, %d] s = [%d, %d]%n", d.width, d.height, s.width, s.height); } public static void main (String[] args) { showPanel(); } }
- 12-01-2007, 07:44 AM #3
Member
- Join Date
- Aug 2007
- Posts
- 40
- Rep Power
- 0
Thanx, Prob. Solved
Thanx problem solved
I hope, You use weightx=1.0 at your begining of code so that all waste spaces are removed. (I read JavaDoc for weightx property: "If all the weights are zero, all the extra space appears between the grids of the cell and the left and right edges.")
And My code set Textfield size to zero where as it is better that first add Textfield into panel then add it to content pane (as per your code)
If something is wrong with my thought then plz reply me to correct my actual concepts that why I got waste/extra space on top/right/left/bottom? and why my code didn't set Textfield width?
I modified your source again, (actually I want that when I increase height of frame then only table should be increased, all other should be remaining same)
In current situation, I set weighty=10.0 for table.
Here is modified source:-
thanx againJava Code:import java.awt.*; import javax.swing.*; public class PTRP extends JPanel { // private JLabel rno,vno; private JTextField txtRNo,txtVNo; // private JLabel lblPtName,lblConsultBy; private JButton btnOk; private JTextField txtPtName,txtConsultBy; private JTable gridPreview; PTRP() { addComponents(); } private void addComponents() { GridBagLayout gb=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; gbc.weighty = 1.0; setLayout(gb); /* //ADD First Row //Adding 1st & 2nd Col... gbc.anchor=GridBagConstraints.WEST; JLabel 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)); JLabel 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 First Row JPanel row1 = new JPanel(new GridLayout(1,2)); /* gbc.insets = new Insets(2,2,2,2); gbc.anchor=GridBagConstraints.WEST; */ JPanel pnl1,pnl2; pnl1=new JPanel(new FlowLayout(FlowLayout.LEFT)); pnl2=new JPanel(new FlowLayout(FlowLayout.RIGHT)); JLabel rno=new JLabel("Test Recpt. No.:"); pnl1.add(rno); txtRNo=new JTextField(8); txtRNo.setEditable(false); pnl1.add(txtRNo); JLabel vno=new JLabel("Voucher No.:"); txtVNo=new JTextField(8); txtVNo.setEditable(false); pnl2.add(vno); pnl2.add(txtVNo); row1.add(pnl1); row1.add(pnl2); /* row1.add(vno, gbc); row1.add(txtVNo, gbc); row1.add(txtRNo, gbc); */ //gbc.insets = new Insets(0,0,0,0); gbc.gridwidth=2; gbc.fill=GridBagConstraints.HORIZONTAL; add(row1, gbc); gbc.fill=GridBagConstraints.NONE; gbc.gridy = 1; //ADD Second Row //Adding 1st Col... gbc.gridwidth=1; gbc.weightx = 0; JLabel lblPtName=new JLabel("Pt. Name"); txtPtName=new JTextField(); add(lblPtName, gbc); //Addding 2nd Col... // gbc.gridy = GridBagConstraints.RELATIVE; gbc.weightx = 1.0; gbc.fill=GridBagConstraints.HORIZONTAL; // gbc.gridwidth=GridBagConstraints.REMAINDER; add(txtPtName, gbc); gbc.fill=GridBagConstraints.NONE; gbc.gridy++; //ADD Third Row //Adding 1st Col... // gbc.gridwidth=1; gbc.weightx = 0; lblPtName=new JLabel("Consult By"); txtPtName=new JTextField(); add(lblPtName, gbc); //Addding 2nd Col... // gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.weightx = 1.0; gbc.fill=GridBagConstraints.HORIZONTAL; add(txtPtName, gbc); gbc.fill=GridBagConstraints.NONE; gbc.gridy++; //ADD Fourth Row // gbc.fill=GridBagConstraints.BOTH; gbc.gridwidth = 2; gbc.weighty = 10.0; gridPreview=new JTable(2,2); Dimension d = gridPreview.getPreferredSize(); System.out.printf("d = [%d, %d]%n", d.width, d.height); d.width = 300; gridPreview.setPreferredScrollableViewportSize(d); gbc.anchor=GridBagConstraints.CENTER; gbc.fill=GridBagConstraints.HORIZONTAL; gbc.fill=GridBagConstraints.BOTH; add(new JScrollPane(gridPreview), gbc); gbc.weighty = 1.0; //Add Fifth Row gbc.fill=GridBagConstraints.NONE; gbc.anchor=GridBagConstraints.EAST; gbc.gridy++; btnOk=new JButton("OK"); btnOk.setMnemonic('O'); add(btnOk,gbc); btnOk.requestFocusInWindow(); } private static void showPanel() { final JFrame jf=new JFrame("GridBag Test"); PTRP pnlGridBag=new PTRP(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setContentPane(pnlGridBag); // jf.setPreferredSize(new Dimension(300,250)); jf.setLocationByPlatform(true); jf.pack(); SwingUtilities.invokeLater(new Runnable() { public void run() { jf.setVisible(true); } }); Dimension d = pnlGridBag.getPreferredSize(); Dimension s = pnlGridBag.getSize(); System.out.printf("pnlGridBag d = [%d, %d] s = [%d, %d]%n", d.width, d.height, s.width, s.height); } public static void main (String[] args) { showPanel(); } }
Gajesh
Similar Threads
-
java.lang.OutOfMemoryError: Java heap space
By paul in forum Advanced JavaReplies: 11Last Post: 06-12-2010, 05:30 PM -
get numeric value from a text field
By Lehane_9 in forum New To JavaReplies: 2Last Post: 06-14-2008, 03:19 AM -
Space Exploration dev 6
By JavaBean in forum Java SoftwareReplies: 0Last Post: 03-25-2008, 04:48 PM -
Print a blank space
By susan in forum New To JavaReplies: 2Last Post: 07-30-2007, 01:58 PM -
problem with date field
By Ed in forum JDBCReplies: 2Last Post: 06-12-2007, 04:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks