-1

For some reason when the condition of getSelector is equal to 2 is reached (I know for a fact this if statements condition is being reached because of System.out.println(""); so don't say that the statement is not getting reached) but when the condition is achieved for some reason the JLabel (problem) is not showing up on the JPanel (panel). (NOTE: The JPanel has been added to my JFrame) Why is this happening, I used the validate, and repaint methods. Thank you very much and happy 4th of July :-)!

NOTE: ALSO THE TIMERCLASS OBJECT'S CALLED METHODS ARE WORKING PROPERLY SO I KNOW THAT IS NOT THE SOURCE OF THE PROBLEM!

EDIT: I have now added all of the code so this could help to possibly answer my problem's source

  • what happens if instead of callin panel.validate you call panel.invalidate() ? – Mauricio Gracia Gutierrez Jul 05 '15 at 18:31
  • Not sure, let me quickly check. – user3887735 Jul 05 '15 at 18:32
  • Nope, this doesn't fix anything. – user3887735 Jul 05 '15 at 18:34
  • do you have enough visual space on your panel ? what kind of layout out are you using ? – Mauricio Gracia Gutierrez Jul 05 '15 at 18:35
  • It should be panel.revalidate(); But that aside why do you want to remove and add the label every time – Madhan Jul 05 '15 at 18:35
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 3) Tip: Add @user3887735 (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jul 05 '15 at 18:35
  • Yes I do, I know this actually because when I is equal to 14 I can see the JLabel (problem) so I know something wrong is happening. – user3887735 Jul 05 '15 at 18:36
  • 1
    Also when referring other persons use @MauricioGracia or whomever it is by @ personname so that the person will be notified – Madhan Jul 05 '15 at 18:37
  • Revalidate does not do anything else either. – user3887735 Jul 05 '15 at 18:37
  • 2
    Are you running this code inside an event handler? Please follow Andrew Thompson's advice and rewrite your program into an MCVE or SSCCE. It's hard to help without being able to run and see. – RealSkeptic Jul 05 '15 at 18:39
  • @Madhan I am not removing anything accept when i is equal to 14, I am simply trying to re validate the JLabel (problem). – user3887735 Jul 05 '15 at 18:40
  • Here now I am providing all of the code – user3887735 Jul 05 '15 at 18:41
  • Were you really adding this panel to the content pane ?? ie getContentPane().add(panel); – JHermit Jul 05 '15 at 18:48
  • Yes as you can see I added the JPanel panel, to the JFrame frame. – user3887735 Jul 05 '15 at 18:52
  • 2
    Here's a way to avoid the vast majority of the confusion you are experiencing now: always instantiate, populate, and validate the JPanel/similar objects, before adding them to your main window (e.g. JFrame). Thereafter, simply pack your JFrame, and show it. In this manner, you'll only need to have a layout in the panel, and you won't need to re-validate every single item you add. – Zesty Memes Jul 05 '15 at 18:55

1 Answers1

2

In your code you'd tried to add the panel to the JFrame via frame.add(panel). But the correct way of adding a component to the JFrame is via its getContentPane() method. ie as per your code it should be like frame.getContentPane().add(panel). Okay ? And also you're setting the panel's layout as null. It means you need to set the size of the component(s) you wish to add to this panel, manually. That is since you're adding the JLabel problem, you need to set the size of that JLabel like problem.setSize(width,height). Hope that this would resolve your issue.

JHermit
  • 190
  • 9
  • 1
    Also remove those panel.validate() and repaint() calls. It wont be necessary though. – JHermit Jul 05 '15 at 19:00
  • I edited my code with the frame.getContentPane().add(panel) but I do know for a fact that the setSize method not being called is not the answer to my problem because when i is equal to 14 I start seeing the JLabel problem – user3887735 Jul 05 '15 at 19:02