• Post Reply Bookmark Topic Watch Topic
  • New Topic

components dont show after i add them using GridBagLayout...What am i doing wrong

 
Hakim Sarbogu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class FormPanel extends JPanel {
private JLabel btn1;
private JLabel btn2;
private JLabel btn3;
private JLabel btn4;
private JLabel btn5;
private JLabel btn6;
private JLabel btn7;
private JLabel btn8;
private JLabel btn9;
private JLabel btn10;
private JLabel btn11;
private JLabel btn12;
private JLabel btn13;

private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JTextField text5;
private JTextField text6;
private JTextField text7;
private JTextField text8;
private JTextField text9;
private JTextField text10;
private JTextField text11;
private JTextField text12;

private JComboBox combo1;
private JComboBox combo2;
private JComboBox combo3;
private JComboBox combo4;

private JButton generateButton;
private JButton resetButton;

public FormPanel() {

// //////initializing JLabel///////////

btn2 = new JLabel("Used by");
btn3 = new JLabel("Select Cab");
btn4 = new JLabel("KMS Used");
btn5 = new JLabel("Parking & toll");
btn6 = new JLabel("Out station Allowance");
btn7 = new JLabel("Booking Amount Paid");
btn8 = new JLabel("Branch");
btn9 = new JLabel("Package");
btn10 = new JLabel("Car Model");
btn11 = new JLabel("Hours Used");
btn12 = new JLabel("Others");
btn13 = new JLabel("Nights");

// //////initializing TextFields///////////

text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
text4=new JTextField(10);
text5=new JTextField(10);
text6=new JTextField(10);
text7=new JTextField(10);
text8=new JTextField(10);
text9=new JTextField(10);
text10=new JTextField(10);
text11=new JTextField(10);
text12=new JTextField(10);

/////////////////////initializing JButtons//////////////

combo1=new JComboBox();
combo2=new JComboBox();
combo3=new JComboBox();
combo4=new JComboBox();

////////////////////initializing buttons//////////////////////
generateButton=new JButton("Generate Invoice");
resetButton=new JButton("Reset Info");

setBackground(new Color(170, 175, 255));


setVisible(true);
layout();
setSize(new Dimension(500, 250));
}



public void layout() {

setLayout(new GridBagLayout());

GridBagConstraints gc = new GridBagConstraints();

/////////////first row/////////////////////////////
gc.gridx=0;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
gc.insets=new Insets(2, 2, 2, 2);
gc.anchor=GridBagConstraints.NONE;

add(new JButton("Booked by"), gc);

gc.gridx=1;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
gc.insets=new Insets(2, 2, 2, 2);
gc.anchor=GridBagConstraints.NONE;

add(text1, gc);

}
}
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post an SSCCE showing your problem.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always use code tags.

The problem you have is probably quite simple. You should call setVisible after you have added all Components to your display. You appear only to be adding two Components in that code anyway. Maybe you are adding the others elsewhere.

Don't make all those Components fields. They can probably be local variables. They have very confusing names. Names like btn1 btn2 and btn3 are not at all easy to understand, but they obviously are buttons. Applying such names to labels is even more confusing.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also only call setVisible on the top‑level container.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!