-1

Im making a quiz application. And when a team pushed their button, they aren't allowed to still see the question or see the video/audio piece. This kind of information is displayed in the quizview ( this is a jpanel ) added to a parent JFrame. What i'm trying to do is minimize the JFrame when a button is pushed from the teams. This works perfectly. If a button is pushed the administator get a pop up in his view. If the answer is incorrect the JFrame should be maximized again. So when he pushes a button if the answer is incorrect, a boolean will be set on true in the quiz model ( $maximize ). We update the views then. In the update of the view im checking if the boolean is set on true. If it is true, i call the maximize method. And maximize it again when the answer is wrong. Minimizing works perfectly, but maximizing not.

Anyone knows what's wrong?

This is my code in the view, this view is a JPanel in the bigger JFrame, where the maximizing happens :

public void update(Observable arg0, Object arg1) {
    $question = (Question) arg1;

    if(((QuizModel) getModel()).getMaximize()) /* Only maximize the JFrame when needed */
        maximizeFrame(); /* Maximize the frame first */     



    repaint();
}


/**
 * Maximize the parent JFrame so the teams can see the question
 */
protected void maximizeFrame() {
    System.out.println("MAXIMIZE");
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    topFrame.setState(JFrame.MAXIMIZED_BOTH);

}

The minimizing occurs when a team pushed their button, here is the code :

/**
 * If a button gets pressed we call this function
 * @param team the team that pressed their button
 */
protected void buttonPressed(int team) {
    /* Check if a button is pressed */
    if(((QuizModel) getModel()).getTeamPressed() > 0)
        $isPressed = true;
    else
        $isPressed = false;

    /* If there hasn't been pressed yet and the question is not null */
    if(!$isPressed && $question != null){
        minimizeFrame(); /* Minimize the frame */

        /* If this question has a media path we need to pause the audio/video, we also check if the user has installed vlcplayer and selected the right path */
        if($question.getMediaPath() != null && QuizSoftwareModel.$vlcPath != null)
            ((QuizController)getController()).pause(); /* Pause the video */

        /* Give a pop up message to the admin that a team has pushed their button */
        ((QuizController)getController()).showScoreView(team);
    }

}

/**
 * Minimize the parent JFrame so the teams can't see the question anymore 
 */
protected void minimizeFrame() {
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    topFrame.setState(JFrame.ICONIFIED);

}

[EDIT] Reduced the code.

Thanks!

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user3485470
  • 121
  • 5
  • 11
  • 1
    Does this help? http://stackoverflow.com/questions/5258207/how-to-maximize-a-jframe-through-code – Tim B May 21 '14 at 12:02
  • please isn't really now the time to post an SSCCE/MCVE – mKorbel May 21 '14 at 12:03
  • 2
    This is not PHP, don't use `$` in variable names. – user1803551 May 21 '14 at 12:03
  • @TimB Nope tried that already, still not working. – user3485470 May 21 '14 at 12:05
  • @user1803551 They told us at school to do this for java object orientated, i know this isn't adviced by the java documentation, but still they need to grade this code, so for now i am listening to them. – user3485470 May 21 '14 at 12:06
  • Post an [MCVE](http://stackoverflow.com/help/mcve) with emphasis on *minimal*. you don't need 4 classes to demonstrate a minimize-maximize problem. – user1803551 May 21 '14 at 12:06
  • 2
    "They told us at school to do this for java object orientated" this sentence makes no sense. Java is object oriented and using `$` is something used mostly in PHP and has nothing to do with object oriented languages. Remove it for better code readability. – user1803551 May 21 '14 at 12:08
  • (1) If you read the javadoc for `setState` you'd see that it says `Sets the state of this frame (obsolete)`, Use `setExtendedState` instead. (2) Get the state of the frame with `getExtendedState` instead of keeping a variable that checks the state. – user1803551 May 21 '14 at 12:24
  • Steps to debug: (1) Is `maximizeFrame` called when it should, do you see the print message? If yes, (2) dose the variable `topFrame` contain the correct frame? If yes, (3) does the `getExtendedState` tell you that the frame is not already maximized? If yes, (4) does `setExtendedState` maximize the frame? If no, (5) write a 5 line program that uses the same function call on a minimized frame and see if it works. If yes (and it will if you do it right), (6) find the differences. – user1803551 May 21 '14 at 12:31
  • @user1803551 I solved the problem, i used ExtendedState everywhere now. i also used topFrame.MAXIMIZED_BOTH, instead of JFrame.MAXIMIZED_BOTH, don't know if this was usefull. How can i put the JFrame on top when i maximize so it shows in front of everything? It doesn't always need to be on top, just after the maximize i want it to be. – user3485470 May 21 '14 at 12:50
  • See what reading the javadoc can do? Using `JFrame.MAXIMIZED_BOTH` has the same effect as `topFrame.MAXIMIZED_BOTH`, but accessing it statically it the right way. To bring the frame to the front use `toFront` or `requestFocusInWindow`. – user1803551 May 21 '14 at 13:01
  • You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188), instead of altering the title. – trashgod May 21 '14 at 14:15

1 Answers1

0

The solution is using topFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); instead of setState. Setstate only sets the state of the current frame, because i needed the parent frame i needed to use setExtendedState.

Also missed some booleans to maximize/minimize when needed.

user3485470
  • 121
  • 5
  • 11