0

As a part of my learning, I made a JPanel in a simple swing application. I made it undecorated. Now only a JPanel is shown. I applied component move and added close button to this panel but size of the panel is fixed.

How can I make my JPanel resizable?

Here's my panel

package x; public class TestRun extends javax.swing.JFrame {

/**
 * Creates new form TestRun
 */
public TestRun() {
    setUndecorated(true);
    initComponents();
    setMouseListener();
}
private void setMouseListener() {
    MoveMouseListener mlistener = new MoveMouseListener(jMenuBar1);
    jMenuBar1.addMouseListener(mlistener);
    jMenuBar1.addMouseMotionListener(mlistener);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1090, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 689, Short.MAX_VALUE)
    );

    jMenu1.setText("File");
    jMenuBar1.add(jMenu1);

    jMenu2.setText("Exit");

    jMenuItem1.setText("Exit");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem1ActionPerformed(evt);
        }
    });
    jMenu2.add(jMenuItem1);

    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    System.exit(EXIT_ON_CLOSE);
}                                          

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    }
    catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TestRun.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TestRun().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   

}

  • 3
    Can you show your codes of your JPanel? And how you want your JPanel to be resized? During run time? – user3437460 Nov 26 '15 at 17:55
  • How are you displaying this `JPanel`? – mr.celo Nov 26 '15 at 18:10
  • `I applied component move` - what does that mean? – camickr Nov 26 '15 at 18:30
  • Updated the code.. This JPanel. How to make it resizable at runtime? (Same way we resize window or JFrame) – user1986201 Nov 26 '15 at 19:47
  • Conceptually you would need to do something like [this](http://stackoverflow.com/questions/16869877/how-to-remove-window-box-from-any-java-gui/16869893#16869893). This example shows you how to move an undecorated window, which would be the bases for resizing as well – MadProgrammer Nov 26 '15 at 22:28

1 Answers1

0

You should be able to use setPreferredSize(Dimension size); on the JPanel and call pack(); on the JFrame

MircoProgram
  • 295
  • 1
  • 20
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Nov 26 '15 at 22:25
  • I know set minimum/preferred/maximum size is not the most elegant way to achieve this effect but in combination with 'pack();' it's the most easiest way to resize a component to a desire dimension and recalculate the container around it. Usually when doing this I set all 3 dimensions to the same size. – MircoProgram Nov 28 '15 at 07:32
  • The "other" problem is, this isn't what the op is actually asking, they want to be able to manually resize an undecorated frame. If you "need" to supply sizing hints, it's better to override the getter methods as you need – MadProgrammer Nov 28 '15 at 07:46