Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
How to set GridLayout for a JPanel that is inside JFrame having BorderLayout
Gaurav Gambhir
Ranch Hand
Posts: 256
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
For the following code, I am getting the run time error, Please help
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class FormExample4 { private JFrame f; private JButton ok,cancel; private JPanel top,center,bottom; private Color color1,color2,color3; private JLabel lbltitle,lblname,lbladdress,lblage; private JTextField txtname,txtaddress,txtage; public FormExample4(){ f = new JFrame("Border Layout"); top=new JPanel(); center=new JPanel(); bottom=new JPanel(); ok = new JButton("Add"); cancel = new JButton("Cancel"); color1=new Color(255,0,0); color2=new Color(255,100,210); color3=new Color(125,0,0); lbltitle=new JLabel("Employee Details"); lblname=new JLabel("Name"); lbladdress=new JLabel("Address"); lbladdress=new JLabel("Age"); txtname=new JTextField(); txtaddress=new JTextField(); txtage=new JTextField(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void launchFrame(){ top.add(lbltitle); f.setLayout(new BorderLayout(0,0)); top.setBackground(color1); f.add(top, BorderLayout.NORTH); bottom.setBackground(color2); bottom.add(ok); bottom.add(cancel); f.add(bottom, BorderLayout.SOUTH); center.setBackground(color3); center.setLayout(new GridLayout(3,2)); center.add(lblname); center.add(lbladdress); center.add(lblage); center.add(txtname); center.add(txtaddress); center.add(txtage); f.add(center, BorderLayout.CENTER); f.setSize(200,200); f.setVisible(true); } public static void main(String args[]){ FormExample4 guiWindow2 = new FormExample4(); guiWindow2.launchFrame(); } }
Runtime error is coming for the following lines,
center.setLayout(new GridLayout(3,2)); center.add(lblname); center.add(lbladdress); center.add(lblage); center.add(txtname); center.add(txtaddress); center.add(txtage);
Michael Dunn
Ranch Hand
Posts: 4632
posted 11 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
you have this
private JLabel ...,...,lbladdress,lblage;
then this
lbladdress=new JLabel("Address");
lbladdress=new JLabel("Age");
look odd?
Gaurav Gambhir
Ranch Hand
Posts: 256
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Even after making JLabel public doesn't solve my problem as when I run the program I am getting Exception in
thread
"main" java.lang.NullPointerException
Michael Dunn
Ranch Hand
Posts: 4632
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
read *every*
word
in my reply (hint: look for duplicates)
Gaurav Gambhir
Ranch Hand
Posts: 256
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That was really my very silly mistake Thanks
alot
Michael
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
applet cannot start error in browser ...
problem in layout a form.
refresh canvas, designer problem?
Alignment and Spacing between components
More...