• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}
}
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post an SSCCE showing your problem.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also only call setVisible on the top‑level container.
 
reply
    Bookmark Topic Watch Topic
  • New Topic