Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: My JPanel-form doesn´t show up

  1. #1
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Default My JPanel-form doesn´t show up


    I've just started to try out the GUI of Netbeans and I can't understand why my JPanel doesn't show up. The program is just running for 1 second and no error is generated. Here is my code:

    public class HängaGubbe2 extends javax.swing.JPanel {
        private String valtOrd;
        public HängaGubbe2() {
         * 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.
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jProgressBar1 = new javax.swing.JProgressBar();
            jButton1 = new javax.swing.JButton();
            jLabel3 = new javax.swing.JLabel();
            jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
            jLabel1.setText("Gissa på en bokstav i ordet");
            jButton1.setText("Hämta nytt ord");
            jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                            .addGap(18, 18, 18)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(31, 31, 31)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(104, 104, 104)
                            .addGap(95, 95, 95)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(21, 21, 21)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                    .addGap(7, 7, 7))
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JProgressBar jProgressBar1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    public void actionPerformed(ActionEvent e)
            if (e.getSource() == jButton1)
                try {
                     Scanner sc = new Scanner(new FileReader("ord.txt"));
                     java.util.List<String> ord = new ArrayList<String>();
                     while (sc.hasNext())
                     Random rand = new Random();
                     int slump = rand.nextInt(ord.size());
                     valtOrd = ord.get(slump);
                     char[] array = new char[valtOrd.length()];
                     Arrays.fill(array, '?');
                     String hemligtOrd = new String(array);
                catch (FileNotFoundException fe){
                    System.out.println("Filen finns inte");
            else if (e.getSource() == jTextField1)
                String txt = jTextField1.getText();
                    JOptionPane.showMessageDialog(null, "Bokstaven hittades i ordet!");
                        char [] hit = new char[jLabel3.getText().length()];
                        Arrays.fill(hit, '?');
                        char c = txt.charAt(0);
                        hit[valtOrd.indexOf(txt)] = c;
                        String hemligtOrd = new String(hit);
                    JOptionPane.showMessageDialog(null, "Bokstaven finns inte!");
        public static void main(String [] arg)
            HängaGubbe2 h = new HängaGubbe2();

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 827 Times in 770 Posts
    Blog Entries

    Default Re: My JPanel-form doesn´t show up

    Where is the JFrame that contains the JPanel?

    FWIW, if you are new to Swing, I highly recommend learning (at the very least) the basics of putting together a user interface manually.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    While a UI editor might seem handy, for newcomers it makes it much more difficult to debug (case in point) and customize.

Similar Threads

  1. defaultCloseOperation doesn't work plus JButttons don't show up
    By Gugubo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2013, 05:42 AM
  2. [SOLVED] Why doesn't my Applet show up in my browser?
    By SendBorg in forum Java Applets
    Replies: 3
    Last Post: January 29th, 2012, 07:30 AM
  3. how to show/open/load JPanel(form) in an external jar
    By iskandar in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 16th, 2012, 06:19 AM
  4. Hide/Show Form?
    By chickenhawk in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 1st, 2011, 06:26 AM
  5. J2ME-Show RMS data in tabular form
    By Sunil Raghuvanshi in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: January 24th, 2011, 07:06 AM

Tags for this Thread