SpeedEX505
SpeedEX505

Reputation: 2076

JFrame wont show in netbeans platform

I have application build on netbeans project extension. I need to show new separated Jframe on button click in TopComponet. But when I call frame.setVisible(true) nothing happens. I tried method pack() and it doesn't help.

public void onClick(){
   MyFrame frame = new MyFrame();
   frame.pack();
   frame.setVisible(true);
}

I already read a few people had same problem but I haven't found any solution.

MyFrame Class

public class MyFrame extends javax.swing.JFrame {
public MyFrame() {
    initComponents();
}

private void initComponents() {    
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}            
}

EDIT:

javax.swing.JFrame t = new javax.swing.JFrame("test");
t.pack();
t.setVisible(true);
//works

MyFrame t = new MyFrame();
t.pack();
t.setVisible(true);
//doesnt work

UserDetailWindowFrame is JFrame genereted by Netbeans.

Upvotes: 1

Views: 3164

Answers (4)

Manos Mertzianis
Manos Mertzianis

Reputation: 1

Use the method .show() after creating an object of Class MyFrame, so it should seem more like this:

public void onClick(){
    MyFrame frame = new MyFrame();
    frame.pack();
    frame.show(); //it may create the frame at the top left corner.
}

Upvotes: 0

kazi
kazi

Reputation: 99

Ok, we were facing some problems with JFrame and TopComponent coupled together in one of our projects. I am trying to suggest one of the approaches. It might be somehow helpful. When your start a Netbeans Platform application, it creates a default Topcomponent, provided you don´t have any Topcomponent of your own. We wanted to ignore the default Top Component all together, and provide our own JFrame.

When the application was starting, we provided the following argument inside a config file(our one is a netbeans maven Project)-

default_options="-J-Dorg.netbeans.core.WindowSystem.show=false"

When the module is loaded we started our customized JFrame within the following chunk of code-

WindowManager.getDefault().invokeWhenUIReady(new Runnable(){    
@Override
public void run(){
MainJFrame parentJFrame = new MainJFrame("");
parentJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
parentJFrame.add(myJPanel);// your JPanel will come here
parentJFrame .revalidate();
parentJFrame .repaint();
parentJFrame .setVisible(true);
 }
 });    

If you are always working within the same TopComponent, then you might want to close/hide the TopComponent before showing your own JFrame.The following code might help-

 WindowManager.getDefault().getMainWindow().setVisible(false);
                            or
 WindowManager.getDefault().getMainWindow().setEnabled(false);

Hopefully, this helps. Using JFrame inside a Netbeans Platform Applicaiton can be nasty sometimes.

Upvotes: 1

ZakiMak
ZakiMak

Reputation: 2102

Can you set size on the JFrame and position it to the center(frame.setLocationRelativeTo(null)) and see if it works. Here is the related code:

private void initComponents() {    
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 300, Short.MAX_VALUE)
);

setSize(1000, 1000);
//pack(); no need to call this as we set a fixed size

}

Call your onClick method like this:

 private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         

       onClick();

    }  

Here is the onClick method:

public void onClick(){
   MyFrame frame = new MyFrame();
   //frame.pack(); no need to call this as we set a fixed size
   frame.setLocationRelativeTo(null); // position it to the center
   frame.setVisible(true);
}

Upvotes: 0

David Coler
David Coler

Reputation: 453

I got this to work creating a new empty frame.

public void onClick()
{
    MyFrame newframe = new MyFrame();
    newframe.setDefaultCloseOperation(1); //adding this will allow the new frames to close without the application closing
    newframe.setVisible(true);
    newframe.pack();
}

however if you close it, it will exit the application, you may want to think about building a specific new frame.

Upvotes: 0

Related Questions