Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't create a brand new panel

 
Morgan Morten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,
Here is a simple code that creates a frame, a panel and a text field inside the panel.
The frame has a button. Once the user clicks the button, a brand new panel is created on the frame (and of course, a new text field inside the panel)
Forgive my ignorance, but I can't seem to be able to instantiate a brand new panel! The button commands "JPanel panel = new JPanel();" to no avail. Thank you for your assistance ~Morgan

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morgan Morten wrote:Once the user clicks the button, a brand new panel is created


That's true.

on the frame (and of course, a new text field inside the panel)


That's not true. I'm not sure what you mean by "on the frame" but at any rate you don't put that new JPanel into the JFrame in any way, so there's no way for it to be seen by the user.

And I'm not sure what makes you think that a new text field is created in that panel -- I don't see any code which does that.

I'm not going to suggest how you should fix that, because adding new components to an existing GUI is not a common thing to do. A lot of the time when you see code which is trying to do that, it was written by somebody who didn't know about better alternatives. And anyway I'm not sure what you expect to happen after you click the button a dozen times and have 13 panels in there somewhere.
 
Morgan Morten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As suggested, the button now adds the panel to the frame, but still the frame is not showing the newly instantiated panel:

public void actionPerformed(ActionEvent evt){
if(evt.getSource()==button){

frame.remove(panel);
JPanel panel = new JPanel();
frame.add(panel);
panel.setVisible(true);
frame.setVisible(true);
frame.repaint();

}

I would assume when a new instance of the panel is created, the text field located inside the panel is also created, correct?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are basic misunderstandings throughout the code, causing some confusion.

You've defined a class called MainFrame that extends JFrame. By that alone, MainFrame is a JFrame. You don't need this line:

JFrame frame = new JFrame();

You don't need frame at all, so that all references to frame can be removed. Then your constructor looks like:

Constructor:

As you've demonstrated, you can add components before completely characterizing or building them (as long as they've been instantiated), but the program is much easier to read if you build first then add.

Then, your main() method can simply be:

new MainFrame();

And when the button is pressed, "this," which refers to the instance of MainFrame created by the main() method, will disappear. The program will still be running by the way, because you didn't set the Frame's defaultCloseOperation.

I realize that I haven't directly answered the question you asked, but hopefully I've cleared up some of the mystery. There's a CardLayout Tutorial that you may want to work through, because it kind of does what you've described, though I'm not exactly sure what you're trying to do.

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morgan Morten wrote:I would assume when a new instance of the panel is created, the text field located inside the panel is also created, correct?


No. Why would you assume that "new JPanel()" does that? Just because your code was written to do that for some other JPanel, that doesn't mean that it will automatically happen for any other JPanels.
 
Morgan Morten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg, what I am trying to do (using this simple example and -unfortunately- with a limited knowledge of Java) is to create a new instantiated panel inside a frame.
In reality, I am writing a Point-of-Sale, so the panel is in fact a "sales" screen. When the user is done with a sale (let's say you enter 'abc' on that text field), the button is clicked and a new 'Sale' screen comes
up on the frame for the user to do a new sale (in this simple example, a new panel and a new text field that is ready to receive input again) ~M
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why would you need a new panel and a new textfield?

when done with the sale, create a sale object from the data on the screen,
save that to a db?, then clear the screen
 
Morgan Morten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Create a new object from the data captured on the screen".. I might give that a try.
Thank you folks for the help! ~Morgan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!