TextField taking up the entire window space
I have created a TextField in a frame but it takes up the entire window space. Below is the code
TextField licenseID = new TextField("My Text box");
licenseID.setBackground(Color.blue);
licenseID.setSize(20, 20);
add(licenseID);
This code is written inside the class -
class EnterLicense extends Frame
I am setting the size to 20,20 but it takes up the entire window size. Can someone please help?
Re: TextField taking up the entire window space
Are you adding to the frame or a JPanel? I believe this is an issue with the default layout manager or something. I can't remember off the top of my head since I haven't done any low-level GUI work for years. You either need to call getContentPane().add(licenseID);, or you need to create a new JPanel, add licenseID to the new JPanel, and then add the new JPanel to your frame.
Re: TextField taking up the entire window space
I am adding this to Frame (i cannot use JPanel). I tried using getContentPane() but it says getContentPane() is undefined.
Re: TextField taking up the entire window space
Why can't you use JPanels? The assignment specifics or something? Can you use any sort of container? Is it a Frame or a JFrame?
Re: TextField taking up the entire window space
It is Frame. What kind of container can I use to fix this?
Re: TextField taking up the entire window space
Well, since Frame is a Container, then you may be able to just change the LayoutManager. Try saying:
frame.setLayout(new FlowLayout());
before you add anything.
No idea if it will work, but its worth a try.
Re: TextField taking up the entire window space
Million thanks!!! It worked