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.


>> REGISTER NOW TO START POSTING


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
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default My JPanel-form doesn´t show up

    Hello!

    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() {
            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();
            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);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(104, 104, 104)
                            .addComponent(jButton1))
                        .addGroup(layout.createSequentialGroup()
                            .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))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .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))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .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)
            {
     
                jProgressBar1.setValue(jProgressBar1.getMinimum());
                try {
     
                     Scanner sc = new Scanner(new FileReader("ord.txt"));
                     java.util.List<String> ord = new ArrayList<String>();
     
                     while (sc.hasNext())
                     {
                         ord.add(sc.next());
                     }
     
                     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);
     
                     jLabel3.setText(hemligtOrd);
                }
                catch (FileNotFoundException fe){
     
                    System.out.println("Filen finns inte");
                }
     
            }
            else if (e.getSource() == jTextField1)
            {
                String txt = jTextField1.getText();
     
                if(valtOrd.indexOf(txt)!=-1)
                {
     
                    JOptionPane.showMessageDialog(null, "Bokstaven hittades i ordet!");
     
                    if(valtOrd.indexOf(txt)==valtOrd.lastIndexOf(txt))
                    {
                        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);
                        jLabel3.setText(hemligtOrd);
     
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Bokstaven finns inte!");
                    jProgressBar1.setValue(jProgressBar1.getValue()+1);
                }
     
            }
     
        }
     
        public static void main(String [] arg)
        {
            HängaGubbe2 h = new HängaGubbe2();
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    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, 08: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, 07: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, 08:06 AM

Tags for this Thread