aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using custom panel in NetBeans GUI builder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using custom panel in NetBeans GUI builder" Watch "Using custom panel in NetBeans GUI builder" New topic
Author

Using custom panel in NetBeans GUI builder

Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37

I know the NetBeans GUI builder is suppose to make things alot easier but I am having some trouble understanding how to add a JPanel I wrote to it. With the NetBeans GUI I created a JPanel Form. On this form I will have two panels. On one of the panels I am trying to insert the panel created by a seperate Java file. My second Java file does all of the work so it has its own main class and everything. I am a little confused where I need to insert the code from the second file into the panel created by the GUI. I hope my quesiton makes sense. I can post my code if needed. Thanks

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Jesse,

I'm not exactly sure if this is what you're trying to achieve but I guess your plan is to create a GUI with Matisse and substitute one of the JPanels with your own implementation, right?

Basically you should start to create your GUI with the standard components. Then you select the JPanel you want to be replaced with your own panel. Select this JPanel and go to the properties window. There you'll find one tab named "Code". Find the field named "Custom Creation Code" and insert the code to create your own JPanel implementation, for example "new YourJPanel()".

Afterwards you should switch to the source code view and check the code Matisse has generated to see what is really happening. Maybe you have to fix some imports but in general it should work similar to this description.

Hope that helps

Marco
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
Yea, I guess I was a little vauge with expalining. I have attached my code called SwingPaintDemo4.java. This program creates a JFrame and JPanel and displays some custom rectangels on it. Using the GUI I created form that just has two JPanels on it. I would like to have this SwingPaintDemo4 functionallity in one of the panels. I will have buttons and other stuff in the other panels. Is this a little clearer?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

OK, I see. But where do you experience problems? With you GUI builder you probably already created a basic Swing application with a main JFrame window where you have placed the two said panels. So you obviously don't need to type create the JFrame inside your code.

To substitute one of the JPanels you created with the GUI builder you should follow the steps I described above. For the "Custom Creation Code" you should use something like "new MyPanel()" which Mantisse places inside the generated code where the JPanels are created. This should replace the modified JPanel with your "MyPanel" implementation. The only thing you probably have to change is to promote the MyPanel class (and maybe the others too?) to public top-level classes to be able to use them from inside the GUI code. But that depends on the package structure of your application.

Marco
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
Ok, so where do I put my code from my original program so that the GUI can access it? Where do I put my main method and all of that? I guess I really dont understand how the GUI is going to make my program run. I know it just creates the panels so do I just paste my code at the bottom of the code it generates then just tell it to output onto the GUI's JPanel?

Could you go into a little more detail on how the GUI works? Thanks!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Java tutorial: Swing. If you follow that tutorial you should be able to writen GUIs without Netbeans or any other graphical designer. Most developers I know prefer it that way, because a) you get to know how Swing works, and b) you have more control.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

I can only agree with Rob!

Matisse is surely one of the best Swing GUI builders around and I've used it many times to design Swing GUIs. But as with a lot of tools it's almost impossible to use it effectively if you don't have at least a basic understanding of what's going on behind the scenes. It CAN be helpful to use Matisse but I doubt that it will be much fun if you are not familiar with Swing and the things Matisse does for you.

Besides the Swing tutorial Rob mentioned there are many good NetBeans tutorials out there which explain how to use Matisse! These documentations explain much more in detail how it works and what has to be done than I could do here in this forum

Marco
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a good example of gui buider vs hand coding is 'Hello World' in Swing

here's a hand-coded version


let Netbeans do the same, and compare the generated code
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
Took everyone's advice and set down and learned a little about coding Swing without the GUI. I was able to create my to JPanels in the same frame. However now I have another problem. I cannot get my panels to resize automatically when they are added to the frame initally. I also need to have the panels automatically resize themselves when the frame is resized by the user. I have been using BorderLayout but im guessing im doing something wrong. Can someone take a look? Thanks

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
do you want all panels to resize? or just MyPanel()?

if just MyPanel, make this change



Borderlayout tries to honour the preferred sizes of its components when they're added to
the outer edges north/soth/east/west or page start/end and line start/end, with any additional
space taken up by center.

so, in your code, if the user increases the width of the frame, the panels will remain at their preferredSize.width.
if you add mainPanel to center, when frame width increases, mainPanel (a FlowLayout) will increase its width,
but leave MyPanel at its preferredSize.

just add MyPanel directly to center, it will increase along with the frame.

is there a particular reason for this line
pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);

in your code all it does is reverse LINE_START and LINE_END, which just makes it harder to follow
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
Beautiful! Thanks so much guys!

As for the line: pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);

I put this in there to get the menu panel to dispaly on the left and the mainPanel to display on the right. It was reverse otherwise.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I put this in there to get the menu panel to dispaly on the left and the mainPanel to display on the right. It was reverse otherwise.

wouldn't it have been easier to put LINE_START at LINE_END and vice-versa?
certainly is harder to follow the code when something says LINE_START
and its at LINE_END, then you have to go through all the code to find out why
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using custom panel in NetBeans GUI builder