Results 1 to 5 of 5
  1. #1
    LoupGarou is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default Frame contents not displaying when called from elsewhere

    Ok, not sure if I worded the question properly but here is the problem I am having. Description of the problem and code to follow:

    I am trying to display a "grid" of buttons. When I run the program and have main create the GridProblem and call openGrid everything displays the way it should.

    When I add a new level to this, via OptionPane.buildOptionFrame() I get the initial option frame; however, when I hit continue I get a bland frame that just hangs the computer. Any help on this would be greatly appreciated.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.KeyboardFocusManager;
    
    import java.awt.Container;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class GridProblem {
    
       private int rows = 2;
       private int cols = 2;
    
       private static boolean firstTime = true;
    
       private static  int currentTimer = 0;
       private static  long countdown = 0;
       
       private JFrame frame = new JFrame("Grid Test");
       private Container contentPane = frame.getContentPane();
    
       public GridProblem() {
          rows = 2;
          cols = 2;
       }
       public GridProblem(int r, int c) {
          rows = r;
          cols = c;
       }
    
       public int getGridSize() {
          return (rows * cols);
       }
    
       /*
        * swapTimer - Used to switch the timer.
        */
       public static void swapTimer(int next) {
          System.out.println("Timer now " + currentTimer);
       }
       
       /*
        * buldGridButton - Makes the buttons that are used for the grid
        */
       public static JButton buildGridButton(int key) {
    
          final JButton button = new JButton("" + key);
    
          ActionListener actionListener = new ActionFocusMover() {
             public void actionPerformed(ActionEvent actionEvent) {
                swapTimer(Integer.parseInt(button.getText()) - 1);
             }
          };
    
          MouseListener  mouseListener  = new MouseEnterFocusMover();
    
          button.addActionListener(actionListener);
          button.addMouseListener(mouseListener);
          
          //button.revalidate();      
          return button;
       }
       
       /*
        * setTestingGrid - Makes and displays the testing grid.
        */
       public void setTestingGrid() {
          contentPane.setLayout(new GridLayout(rows, cols));
          //contentPane.setLayout(new FlowLayout());
          for (int i = 1; i <= getGridSize(); ++i) {
             JButton button = GridProblem.buildGridButton(i);
             System.out.println("Adding button " + button.getText());
             contentPane.add(button);
          }
          frame.setSize((rows * 90), (cols * 50));
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       /*
        * open - The main controller.
        */
       public void openGrid() {
         
          System.out.println("Here 1");
                
          setTestingGrid();
          
          System.out.println("Here 2");
          
          try {
             Thread.sleep(5000);
             
          }
          catch (InterruptedException e) {
             
          }
          
          frame.setVisible(false);
          
          System.out.println("Here 3");
         
          countdown = 0;
          }
       
       public static void main(String args[]) {
          //GridProblem g = new GridProblem(4,4);
          //g.openGrid();
          OptionPane.buildOptionFrame(); 
          
       }
       
    }
    
    class ActionFocusMover implements ActionListener {
       public void actionPerformed(ActionEvent actionEvent) {
         KeyboardFocusManager manager = 
           KeyboardFocusManager.getCurrentKeyboardFocusManager();
         manager.focusNextComponent();
       }
     }
    class MouseEnterFocusMover extends MouseAdapter {
       public void mouseEntered(MouseEvent mouseEvent) {
    
          Component component = mouseEvent.getComponent();
          if (!component.hasFocus()) {
             component.requestFocus();
          }
       }
    }
    
    class OptionPane {
       private static JFrame optionFrame = new JFrame("Options");
       private static Container optionPane = optionFrame.getContentPane();
       
       public static void launchGrid(int r, int c) {
          GridProblem ng = new GridProblem(r, c);
          ng.openGrid();
          //Grid2.openGrid();
       }
       
       public static void buildOptionFrame() {
          //final Grid2 ng = new Grid2();
          
          optionPane.setLayout(new FlowLayout()); 
          
          final JTextField rowEntry = new JTextField("4", 2);
          final JTextField colEntry = new JTextField("4", 2);
          JLabel dim                = new JLabel("x");
    
          optionPane.add(rowEntry);
          optionPane.add(dim);
          optionPane.add(colEntry);
          
          JButton confirmEntry = new JButton("Continue");
          
          ActionListener actionListener = new ActionFocusOption() {
             public void actionPerformed(ActionEvent actionEvent) {
                optionFrame.setVisible(false);
                launchGrid(Integer.parseInt(colEntry.getText()), Integer.parseInt(rowEntry.getText()));
             }
          };
    
          confirmEntry.addActionListener(actionListener);
    
          optionPane.add(confirmEntry, BorderLayout.SOUTH);
    
          optionFrame.setLocationRelativeTo(null);
          optionFrame.setSize(100, 100);
    
          optionFrame.setResizable(false);
          optionFrame.setVisible(true);
       }
     
    }
    
    class ActionFocusOption implements ActionListener {
       public void actionPerformed(ActionEvent actionEvent) {
         KeyboardFocusManager manager = 
           KeyboardFocusManager.getCurrentKeyboardFocusManager();
         manager.focusNextComponent();
       }
    }
    Last edited by LoupGarou; 10-30-2009 at 10:06 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You are calling Thread.sleep on the Event Dispatch Thread or EDT, which is the single thread that Swing uses to paint apps and to allow apps to interact with the user. So rather than display your grid for 5 seconds, and then hide it, it puts your whole app to sleep, something I don't think you want to have happen.

    My recommendation: look up on how to use a Swing Timer in the Sun Swing tutorials and use one.

    Much luck and HTH.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    For instance, check out this modification to the openGrid() method:
    Java Code:
      public void openGrid() {
        System.out.println("Here 1");
        setTestingGrid();
        System.out.println("Here 2");
    
        // create a swing timer that hides frame after 5 seconds
        javax.swing.Timer timer = new javax.swing.Timer(5000, new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
            System.out.println("Here 3");
            countdown = 0;
          }
        });
        timer.setRepeats(false); // timer does not repeat
        timer.start();   // get timer running
    
        //!! thread sleep was here
        //frame.setVisible(false);
      }

  4. #4
    LoupGarou is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    Wonderful. Thanks.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 7
    Last Post: 11-02-2009, 06:31 PM
  2. update() gets never called
    By flok in forum AWT / Swing
    Replies: 8
    Last Post: 10-19-2009, 04:45 PM
  3. My constructor not called
    By rdtindsm in forum New To Java
    Replies: 2
    Last Post: 09-20-2009, 01:38 AM
  4. Help frame is not displaying.
    By gammaman in forum New To Java
    Replies: 24
    Last Post: 07-22-2009, 01:00 PM
  5. javascipt function is nt getting called
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-20-2008, 08:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •