-1

Possible Duplicate:
JLabel only shows if initComponents() is deleted

JavaApplication1.java

this is where Im calling my separate Panel.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import javax.swing.JFrame;

/**
 *
 * @author Aires
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       JFrame f = new JFrame();
       NewJPanel n1 = new NewJPanel();
       f.add(n1);
       f.pack();
       f.show();
    }
}

newJPanel.java im creating new JLabel to add in my current panel, but when I run the program, its not showing up.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Nov 10, 2012, 3:07:31 PM
 */
package javaapplication1;

import javax.swing.JLabel;


/**
 *
 * @author Aires
 */
public class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        //pag kinoment ko ito, tsaka lang lamalabas yung jlabel.
        initComponents();

        //dito Sir, di siya nalalagay sa panel.
        JLabel n = new JLabel();
        n.setText("asdasdasd");
        this.add(n);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(175, 175, 175)
                .addComponent(jLabel1)
                .addContainerGap(191, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jLabel1)
                .addContainerGap(159, Short.MAX_VALUE))
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}

The JLabel that I coded to create is not showing, it only shows when I comment out the //initComponents(); Any solution to this?

Community
  • 1
  • 1
Jong
  • 131
  • 1
  • 2
  • 12
  • Sorry, Im just new here. I tried deleting my first question. It just cant because there are already answers. – Jong Nov 10 '12 at 10:57
  • the idea is not _delete_ the former answer, instead _edit_ it with further clarification - if any: this has nothing new, as far as I can see. – kleopatra Nov 10 '12 at 11:28

2 Answers2

5
  • JLabel n = new JLabel(); isn't visible because used GroupLayout required bunch of code for vertical and horizontal coordinates in JPanel

  • JLabel n = new JLabel(); missing these coordinates used for jLabel1 = new javax.swing.JLabel(); into private void initComponents() { then JLabel n = new JLabel(); couldn't be visible in JPanel, and you can to see only jLabel1

  • remove code block private void initComponents() { and to continue with hand coding for your GUI

  • JPanel has implemented FlowLayout in API

  • JFrame has implemented BorderLayout in API

enter image description here

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JavaApplication1 {

    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();
    private JLabel label = new JLabel();

    public JavaApplication1() {
        label.setText("asdasdasd");
        panel.add(label);
        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JavaApplication1 j1 = new JavaApplication1();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I have designed my separate JPanel.java wherein there are already components i have put it there using netbeans drag and drop. But what I want is that I will add a JLabel to that existing JPanel.java using codes. Hope you get my explanation. – Jong Nov 10 '12 at 08:01
  • [GroupLayout is designated for GUI Builder only](http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html), don't use that for hand coding, event is possible, but why, no reason to bothering with that – mKorbel Nov 10 '12 at 10:06
3

IMO, you have to add your own JPanel into ContentPane of JFrame.

Besides, use SwingUtilities to show GUI.

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {    
            public void run() {
                JFrame f = new JFrame();
                NewJPanel n1 = new NewJPanel();
                f.getContentPane().add(n1, BorderLayout.CENTER);
                f.pack();
                f.show();
            }
        });
    }

Class NewJPanel code.

class NewJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public NewJPanel() {
        //pag kinoment ko ito, tsaka lang lamalabas yung jlabel.
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        JLabel n = new JLabel();
        n.setText("asdasdasd");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(175, 175, 175)
                .addComponent(jLabel1)
                .addComponent(n)
                .addContainerGap(191, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(127, 127, 127)
                .addComponent(jLabel1)
                .addComponent(n)
                .addContainerGap(159, Short.MAX_VALUE))
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}
Thinhbk
  • 2,194
  • 1
  • 23
  • 34
  • Still not showing. My intention is, it will add the new JLabel that I coded to my designed NewJPanel with netbeans drag and drop components that I have put. – Jong Nov 10 '12 at 08:10
  • 2
    Since, I believe Java 6, it's no longer required that you add your components to the content pane directly, the frame will take care of it. That's not to say you can't do it, it's just not required – MadProgrammer Nov 10 '12 at 08:19
  • @MadProgrammer: I faced problem of not showing JPanel in JFrame by adding it directly to JFrame. I use JRE 1.6.33 – Thinhbk Nov 10 '12 at 08:23
  • How can I add it into Layout? I created it via coding. – Jong Nov 10 '12 at 08:25
  • @Thinhbk I'm nt saying you can't or shouldn't do it, but JFrame#add basically calls getContentPane().add any way ;) – MadProgrammer Nov 10 '12 at 08:31
  • Try this: 1. Move: JLabel n = new JLabel(); into initComponents() method. 2. Add this label into layout. layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(175, 175, 175) .addComponent(jLabel1) .addComponent(n) .addContainerGap(191, Short.MAX_VALUE)) ); Same for layout.setVerticalGroup. 3. call this.setLayout(layout); after adding JLabels to layout. – Thinhbk Nov 10 '12 at 08:33
  • 1
    @AireskristianBancod you need to supply constraints for the label to be included. Unfourtantly I'm not experienced with GroupLayout & GroupLayout really wasn't deigned for hand coding, that doesn't mean you can't do it. You might like to have a read of [How to Use GroupLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html) – MadProgrammer Nov 10 '12 at 08:34
  • @Thinhbk, Unfortunately I cant edit the netbeans generated code. – Jong Nov 10 '12 at 08:41
  • OK, got your problem. Wait me a second. – Thinhbk Nov 10 '12 at 08:42
  • @Thinhbk, and my intention is to create those dynamically, what you're suggesting is like i will be manually coding it again making it static. – Jong Nov 10 '12 at 08:42
  • as @MadProgrammer commented, can you use another layout (for e.g. GridBagLayout) instead of GroupLayout? – Thinhbk Nov 10 '12 at 08:52
  • Look at this answer on SO and check it yourself. http://stackoverflow.com/questions/10094825/how-to-iteratively-add-components-to-a-swing-grouplayout-parallelgroup – Thinhbk Nov 10 '12 at 09:02
  • If I use GridBagLayout my JPanel.java design will change into something messy. – Jong Nov 10 '12 at 09:06
  • using GridBadLayout can make generated code a little more complex than using GroupLayout, but you may manipulate it easier. If the problem is adding control at run time, use GridBadLayout may help. – Thinhbk Nov 10 '12 at 09:13
  • Any other solution? because i have 4 jpanels already designed. – Jong Nov 10 '12 at 09:20
  • 4
    @AireskristianBancod you _either_ use the visual builder (that creates the mess in initComponents) _or_ code the ui manually (which I would strongly recommend) - not both. – kleopatra Nov 10 '12 at 09:55
  • You may use Google AWT Editor in Eclipse for drag&drop GUI design. – Thinhbk Nov 10 '12 at 15:14