|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-30-2007, 12:07 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 15
|
|
|
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:
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
|
|

11-30-2007, 11:58 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,015
|
|
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, 08:44 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 15
|
|
|
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:-
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();
}
}
thanx again
Gajesh
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|