3

it is my first question in Stackoverflow. I have collected pages from here so much and didn't solved my problem. My problem is sometimes (not always) jframe i have created, appear not same with size I have set in properties. I use Netbeans 7.1.2 and only do drag n drop for GUI making. I can't do GUI coding yet. This is the picture for example

My window component looks like cropped. The size when run and size in properties (minimumsize and preferredsize) is different. This happened both with Free Design or Absolute Design. What is the problem of this? Do you know what usual problem with this? I don't know what happen. How do I can solve this? I am sorry for mistakes. Thank you.

I don't know where the error from the code but this is that frame's code:

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jButton4 = new javax.swing.JButton();
    jSeparator1 = new javax.swing.JSeparator();
    btBack = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    btNext = new javax.swing.JButton();
    jLabel10 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jSeparator2 = new javax.swing.JSeparator();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    lbJudul = new javax.swing.JLabel();
    jPanel1 = new javax.swing.JPanel();

    jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/otodidak/gambar/logomati.png"))); // NOI18N
    jButton4.setToolTipText("Tutup Otodidak");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Otodidak");
    setAlwaysOnTop(true);
    setLocationByPlatform(true);
    setPreferredSize(new java.awt.Dimension(787, 410));
    setResizable(false);
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(48, 53, 689, -1));

    btBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/otodidak/gambar/backlogo2.png"))); // NOI18N
    btBack.setToolTipText("Kembali");
    btBack.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            btBackMouseClicked(evt);
        }
    });
    btBack.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btBackActionPerformed(evt);
        }
    });
    getContentPane().add(btBack, new org.netbeans.lib.awtextra.AbsoluteConstraints(224, 340, 130, -1));

    jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/otodidak/gambar/logomati.png"))); // NOI18N
    jButton5.setToolTipText("Tutup Otodidak");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(718, 358, 46, -1));

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/otodidak/gambar/synapticwvdial.png"))); // NOI18N
    getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(388, 65, 348, 264));

    btNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/otodidak/gambar/nextlogo2.png"))); // NOI18N
    btNext.setToolTipText("Lanjut");
    btNext.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            btNextMouseClicked(evt);
        }
    });
    getContentPane().add(btNext, new org.netbeans.lib.awtextra.AbsoluteConstraints(396, 340, 130, 48));

    jLabel10.setFont(new java.awt.Font("Ubuntu", 1, 48)); // NOI18N
    jLabel10.setText("1");
    getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(48, 68, 36, 40));

    jLabel2.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
    jLabel2.setText("Instal Aplikasi wvdial");
    getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, -1, 24));
    getContentPane().add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 116, 216, 18));

    jLabel3.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
    jLabel3.setText("1) Buka Synaptic.");
    getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 132, 368, 26));

    jLabel4.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
    jLabel4.setText("3) Klik kanan wvdial, pilih mark for Installation. ");
    getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 207, 346, -1));

    jLabel5.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
    jLabel5.setText("2) Ketik wvdial. ");
    getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 170, 346, -1));

    jLabel6.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
    jLabel6.setText("4) Biarkan Synaptic menginstalkan wvdial. ");
    getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 244, 346, -1));

    jLabel7.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
    jLabel7.setText("Catatan: internet harus menyala.");
    getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 292, 298, 22));

    lbJudul.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
    lbJudul.setForeground(new java.awt.Color(254, 254, 254));
    lbJudul.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    lbJudul.setText("Cara Menginstal Smartfren AC682");
    getContentPane().add(lbJudul, new org.netbeans.lib.awtextra.AbsoluteConstraints(117, 12, 578, -1));

    jPanel1.setBackground(new java.awt.Color(1, 1, 1));
    getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, -2, 788, 50));

    pack();

Not whole the code, but maybe useful. Thank you for all you attentions...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

5
...AbsoluteConstraints(117, 12, 578, -1)  

This is the basis of the problem. It is as effective as a null layout when using pack().

The solution is plain. Use layout managers that honor the preferred sizes of the components.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So, what should I do? Delete the code AbsoluteConstraints? I can't delete or change anything inside SuppressWarning in Netbeans. Use what layout manager? I only have 2 layout manager options: Free Design and Absolute Design. I don't know, which one is suite me? – Ade Malsasa Akbar Jan 28 '13 at 14:35
  • 3
    Do not use a GUI builder. Code the GUI by hand. Based on your picture, I'm seeing 4 nested JPanels inside of a main JPanel. Each of the JPanels would have their own Swing layout manager. – Gilbert Le Blanc Jan 28 '13 at 14:37
  • 2
    *"..in Netbeans. Use what layout manager? I only have 2 layout manager options: Free Design and Absolute Design"* They will both produce the same problem. Note that developers cannot use the 'GUI designers' of Netbeans or Eclipse (or whatever) until they understand how the Java layouts work. On the subject of "Netbeans won't let me" I recommend, "stop being the IDEs dog". As it is, it has grabbed you by the short and curly pubic hairs, and is giving them a good hard yank. – Andrew Thompson Jan 28 '13 at 14:39
  • 3
    I would advise you to learn how to code the gui by yourself. There are some different advantages by this. 1. you know what you are doing, and what your code stands for.(with a gui builder you just drag and drop, but i something unexptected like this happens in your gui, you will be suprised everytime. 2. you will also be faster compared to the gui bilder. 3. you will get better skills. 4. you will know which layouts fit better to your situation. This all takes some effords, but after a while you will get the benefits of it – SomeJavaGuy Jan 28 '13 at 14:40
  • Umm, I am sorry but I am newbie. I create all with Matisse (GUI Builder). Only that I can. But thank you for advice. No, in my frame only 1 panel exist. Why do you say there is 4 nested panels? I am sorry if I made mistake... – Ade Malsasa Akbar Jan 28 '13 at 14:43
  • or to layout (for NullLayout) by using Insets from container instead of setBounds – mKorbel Jan 28 '13 at 14:43
  • Make sure to add @GilbertLeBlanc to notify them of a new comment (e.g. 4 panels). This is typically known as a 'nested layout'. There is one nested layout shown in the code of [this answer](http://stackoverflow.com/a/14538970/418556), as well as links to a further 3 nested layouts. – Andrew Thompson Jan 28 '13 at 14:45
  • @Andrew Thompson: Okay, Andrew. So, the solution is using NullLayout? To Kevin Esche: how to add reputation to you? Your 4 advices kindly good for me. – Ade Malsasa Akbar Jan 28 '13 at 14:49
  • *"Okay, Andrew. So, the solution is using NullLayout?"* I don't know where you got that from. **No, *nobody*** in this thread so far has recommended null layouts. *My* advice remains as it did 8 hours ago, expressed in the final sentence of the answer. *Use layout managers that honor the preferred sizes of the components.* – Andrew Thompson Jan 28 '13 at 22:57
  • But i don't understand your mean honor preferred size. What layout manager that you mean? I am sorry. – Ade Malsasa Akbar Feb 04 '13 at 06:36
  • You should ask a new question based specifically on that. This question has been answered. – Andrew Thompson Feb 04 '13 at 07:18