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 7 of 7

Thread: programmimng problem or bug?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default programmimng problem or bug?

    NetBeans 8.0.1
    This is the 3rd time I've rebuilt this program.
    My problem seems to be when I add the Menu's.
    When I use RUN, the menu's sometimes show up or sometimes NOT.
    When I use DEBUG it works every time.
    This is the smallest I can get the program before the problem
    starts.
    When I add the menu's it fails?



     
     
    package myclockpaneltest;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import javax.swing.*;
     
    public class MyclockPanelTest {
     
        public JFrame f = new JFrame();
        public JPanel main = new JPanel();
        public JPanel p1 = new JPanel();
        public JPanel p2 = new JPanel();
        public JMenuBar jmb = new JMenuBar();
        public JMenu jm0, jm1;
        public JMenuItem mi0, mi1;
     
     
        public MyclockPanelTest(){
            f.setVisible(true);
            f.setSize(357,165);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        main.setBackground(Color.BLUE);
        f.add(main);
        main.add(p1);
        main.add(p2);
        p1.setPreferredSize(new Dimension(70,90));
        p1.setBackground(Color.ORANGE);
        p2.setPreferredSize(new Dimension(260,90));
        p2.setBackground(Color.BLACK);
     
        f.setJMenuBar(jmb);
        jm0 = new JMenu("Close");
              mi0 = new JMenuItem("Exit");
              jm0.add(mi0);
        jm1 = new JMenu("Time");
              mi1 = new JMenuItem("24-Hour clock");   
              jm1.add(mi1);
          jmb.add(jm0);
          jmb.add(jm1);     
        }
     
     
     
        public static void main(String[] args) {
            // TODO code application logic here
        new MyclockPanelTest();
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: programmimng problem or bug?

    Post the code that adds the menus and causes the failure.

    Swing applications should be run on the EDT.

    Use better variable names.

    Build the UI, pack() it, and then make it visible.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: programmimng problem or bug?

    Quote Originally Posted by GregBrannon View Post
    Post the code that adds the menus and causes the failure.

    Swing applications should be run on the EDT.

    Use better variable names.

    Build the UI, pack() it, and then make it visible.
    The entire code is posted.
    the menus are below setJMenubar

    The code I posted is completely runnable.

    I don't understand Swing applications should be run on the EDT.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: programmimng problem or bug?

    I misunderstood, "This is the smallest I can get the program before the problem starts."

    "Build the UI, pack() it, and then make it visible," means to move the following statement to the last line in the constructor:

    f.setVisible(true);

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    JoeB (October 8th, 2014)

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: programmimng problem or bug?

    Quote Originally Posted by GregBrannon View Post
    I misunderstood, "This is the smallest I can get the program before the problem starts."

    "Build the UI, pack() it, and then make it visible," means to move the following statement to the last line in the constructor:

    f.setVisible(true);
    Thanks, it works.
    I viewed some my programs done with the IDE.
    It all makes sense.
    I'm not a student of Java, it's just a hobby. It first 10 tutorials are my reference.

    JoeB

  7. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: programmimng problem or bug?

    You should still google for the EDT or EventDispatchThread.
    All Swing applications must be started on the EDT (and NOT your main thread) and every update to any Swing component must also happen on the EDT or Swing might not work.

  8. The Following User Says Thank You to Cornix For This Useful Post:

    JoeB (October 9th, 2014)

  9. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: programmimng problem or bug?

    Quote Originally Posted by Cornix View Post
    You should still google for the EDT or EventDispatchThread.
    All Swing applications must be started on the EDT (and NOT your main thread) and every update to any Swing component must also happen on the EDT or Swing might not work.
    Thanks, Now I know where invokeLater comes from in my IDE in my IDE programs.
    This gives me something new to learn.

    Joe

Similar Threads

  1. is this a bug in JAXB or a problem of me?
    By blahblah_gc in forum Java Theory & Questions
    Replies: 3
    Last Post: June 1st, 2014, 08:58 PM
  2. Incomprehensible bug
    By summit45 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 3rd, 2013, 04:38 PM
  3. Login bug
    By 0w1 in forum Loops & Control Statements
    Replies: 0
    Last Post: July 17th, 2013, 09:28 PM
  4. What's the bug in this code?
    By r3dApple in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 11th, 2012, 12:28 AM
  5. Apparent bug?
    By 342-173=147 in forum Loops & Control Statements
    Replies: 2
    Last Post: March 4th, 2011, 02:52 PM