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

Thread: How to stop the same window from having more than one instance of it open at a time.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy How to stop the same window from having more than one instance of it open at a time.

    I am trying to make a jump all but one peg game.

    I was trying to get it make sure that users can't have 100s of help menus open at same time.

    However, I can't figure what works.

    The isShowing() isn't doing what I thought it would.

       import java.awt.*;
       import java.util.*;
       import java.io.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import javax.swing.Timer;
       import java.awt.event.*;
     
       public class PegGame extends JFrame
       {
     
       /*  peg1, peg2, peg3, peg4, peg5, peg6, peg7, peg8, peg9, peg10, peg11, peg12, peg13, peg14, and peg15 - The 15 Peg objects that correspond to the 15 pegs in the game.
       panel- the JPanel object
       timer- a Timer object that I might need to redraw the pegs
       fileReader-  reads the high score file (Is handled if it doesn't exist.)
       file - the File object that contains the high scores , but where to put it, I don't know.
       highScoreRank - the ranking  like 1, 2, 3, ,4 and so on.  Has no maximum limit.
       textRank-  a String, could be either "Peg Master" (1 peg left)  "Not Bad"(2 pegs left),  "Needs Improvement"(3 pegs left) or "Hopeless" (4 or more pegs left).
       name- the String to corresponders to the name of the high score holder
       date- the date the person in question got the high score (note, this is a String, not a Date or Calendar object.)
       newText- this is to correspond to the stuff read in.  It is saved and then altered when more is added if someone gets a high score.
       It was created so that I don't have to mess with the getText() method of the JLabel and risk getting the header text read in/out and causing errors later on. 
       oldText- the original label headings. 
       scoreLabel - a JLabel that contains the text for the high score page. */
     
          private Peg peg1,peg2, peg3, peg4, peg5, peg6, peg7,
          peg8, peg9, peg10, peg11, peg12, peg13, peg14, peg15;
          private JPanel panel;
          private Timer timer;
          private Scanner fileReader;
          private File file;
          private Integer highScoreRank;
          private Integer pegsLeft;
          private String textRank;
          private String name;
          private String date;
          private String newText;
          private String oldText;
          private  JLabel  scoreLabel;
          private JFileChooser load2, save2;
       //private FileChooserFilter pegFilter;
          private Scanner fileReader2;
          private FileWriter writer;
          private JMenuBar jMenuBar;
          private JMenu file2, about;
          private HelpMenu hm;
          private JMenuItem load, save, newWindow, exit, help, about2, highScores2;
          private ImageIcon logo;
          private File logoFile;
     
     
     
          private class HelpMenu extends JFrame
          {
             private JPanel panel;
             private JLabel label;
     
             public HelpMenu()
             {
                super("Help Menu");
                panel = new JPanel();
                String str = "<html><br> Welcome to the Take All but One Peg Game.  </br>" +" <br> The object of the game is to take all but one peg. </br>" +" <br>You can jump a peg but can't move if it's not a jump. </br>" +"<br> The files are saved as .peg extensions. </br></html>";
                label = new JLabel(str);
                label.setForeground(new Color(23, 255, 23));
                label.setFont(new Font("Kristen ITC", Font.BOLD + Font.ITALIC, 20));
                panel.setBackground(new Color(234, 124, 54));
                panel.add(label);
     
                setVisible(true);
                setContentPane(panel);
     
             }
     
          }
     
          private class HighScoreFile  extends JFrame 
          {
             private JButton  reset;
             private  JPanel panel;
             private JScrollPane pane;
     
     
             public HighScoreFile()
             {
                super("High Scores");
     
                panel = new JPanel();
                pane = new JScrollPane(panel, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                setVisible(true);
                reset = new JButton("Reset");
     
                file = new File("C:/Users/HighScores.txt");
                newText = "";
                scoreLabel = new JLabel("<html><b><br>High Scores</html></b></br>"  +  "<html><b>Rank</html></b>       " + "<html><b>Pegs Left      </html></b>       " + "<html><b> Title                                  </html></b>" + "<html><b> Name                                                            </html></b>" +
                   "<html><b> Date                                            </html></b>");
                oldText = scoreLabel.getText();
     
     
                try
                {
                   fileReader = new Scanner(file);
                   while (fileReader.hasNext())
                   {
                      highScoreRank = fileReader.nextInt();
                      pegsLeft = fileReader.nextInt();
                      textRank = fileReader.nextLine();
                      name = fileReader.nextLine();
                      date = fileReader.nextLine();
     
                      newText = newText + "<html><br>" + highScoreRank.toString() + "               " + pegsLeft.toString() + "              " + textRank + "                " +   name + "                                            " +  date + "</html></br>";
     
                      scoreLabel.setText(scoreLabel.getText() + newText);
     
                   }
                }
     
                   catch(FileNotFoundException fnfeRef)
                   {
                      scoreLabel.setText(scoreLabel.getText() + "<html><br> NO HIGH SCORES YET </html></br>");
     
     
                   }
     
                   catch(Exception e)
                   {
                      e.printStackTrace();
     
                   }
     
                panel.add(scoreLabel);
     
                panel.add(reset);
     
                reset.addActionListener(
                      new ActionListener() {
     
                         public void actionPerformed(ActionEvent e)
                         {
                            scoreLabel.setText(oldText + "<html><br> NO HIGH SCORES YET </html></br>");
                         }
                      });
     
                setContentPane(pane);
             }
     
          }
     
          private class Peg 
          {
             private Point location;
             private Color color;
             private int x, y;
     
     
             public Peg(Point location, Color color)
             {
                this.location = location;
                this.color = color;
             }
     
             public void setPoint(Point location)
             {
                this.location = location;
             }
     
             public Point getPoint()
             {
                return location;
             }
     
             public void setColor(Color color)
             {
                this.color = color;
             }
     
             public Color getColor()
             {
                return color;
             }
     
             public void setX(int x)
             {
                setPoint(new Point(x,(int) location.getY()));
             }
     
     
     
             public void setY(int y)
             {
                setPoint(new Point((int) location.getX(), y));
             }
     
             public void setPoint2(int x, int y)
             {
                setPoint(new Point(x,y));
             }
     
             public boolean canJump(Peg p)
             {
                return true;
     
             }
     
          }
     
          public PegGame()
          {
             super("Jump All But One Peg Game");
             setVisible(true);
             panel = new JPanel();
     
             jMenuBar = new JMenuBar();
             setJMenuBar(jMenuBar);
             //panel.add(jMenuBar);
             file2 = new JMenu("File");
             jMenuBar.add(file2);
             about = new JMenu("About");
             jMenuBar.add(about);
             help = new JMenuItem("Help");
             about.add(help);
             about.addSeparator();
     
             load = new JMenuItem("Load");
             file2.add(load);
             file2.addSeparator();
             save = new JMenuItem("Save");
             file2.add(save);
             panel.setBackground(new Color(40, 255, 40));
     
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
             help.addActionListener(
                   new ActionListener() {
     
                      public void actionPerformed(ActionEvent e)
                      {
                         hm = new HelpMenu();
                         hm.setVisible(false);
                         if (!hm.isShowing())
                         {
                            hm.setVisible(true);
                         }
     
                         else
                         {
                            JOptionPane.showMessageDialog(null, "You already have the help menu open!", "Help menu is already open!", JOptionPane.WARNING_MESSAGE);
     
                         }
     
     
                      }});
     
             setContentPane(panel);
     
          }
     
          public static void main(String[] args)
          {
             PegGame pg = new PegGame();
     
     
          }
     
          public void paint(Graphics g)
          {
             super.paint(g);
             drawBoard(g);
             drawWindowFeatures(g);
     
          }
     
          public void drawBoard(Graphics g)
          {
             Polygon poly = new Polygon();
     
             poly.addPoint(600, 95);
             poly.addPoint(280, 512);
             poly.addPoint(925, 512);
     
             g.setColor(new Color(251, 148, 79));
             g.fillPolygon(poly);
             g.setColor(Color.BLACK);
             g.drawPolygon(poly);
     
     
          }
     
          public void drawWindowFeatures(Graphics g)
          {
     
     
             g.drawLine(327, 50, 327, 83);
             g.drawLine(500, 83, 327, 83);
     
     
             g.setColor(new Color(0, 206,206));
             g.setFont(new Font("Papyrus", Font.BOLD + Font.ITALIC, 23));
             g.drawString("THE JUMP ALL BUT ONE PEG GAME", 327, 80);
     
          }
     
     
       }

    Particularly this area is the area of concern.

     help.addActionListener(
                   new ActionListener() {
     
                      public void actionPerformed(ActionEvent e)
                      {
                         hm = new HelpMenu();
                         hm.setVisible(false);
                         if (!hm.isShowing())
                         {
                            hm.setVisible(true);
                         }
     
                         else
                         {
                            JOptionPane.showMessageDialog(null, "You already have the help menu open!", "Help menu is already open!", JOptionPane.WARNING_MESSAGE);
     
                         }
     
     
                      }});


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to stop the same window from having more than one instance of it open at a t

    With a very quick glance, I suspect that your issue is that you're instantiating a new HelpMenu object each time.

    Try placing hm = new HelpMenu(); outside of the ActionListener.
    so that only setVisible() is changed in the ActionListener.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    javapenguin (July 9th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to stop the same window from having more than one instance of it open at a t

    Ok, I got a DoublyLinkedList to make sure that it won't let two be open at same time, though my error message isn't displaying.

    Also, I'm still trying to figure out how to change the if structure if the user closes the help menu, therefore, according to my new code,

    (Also, the remove method called has been set to remove an element, if there, from the DoublyLinkedList. Not an index. )

    Anyway, how can I know if a Window is closed?

    If the sole help menu is closed, I want it removed from the DoublyLinkedList and then be able to add more.

    As is, I can keep it from having two up, but if the original closes, I can't open any more of them, even if none are showing.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to stop the same window from having more than one instance of it open at a t

    Ok. What you mentioned, I tried and it worked.

    Thanks!

Similar Threads

  1. Craating non active window ,and inputing to the non active window.
    By java-beginner in forum Java Theory & Questions
    Replies: 0
    Last Post: February 25th, 2011, 09:13 PM
  2. Thread for a given time window
    By zu1u in forum Threads
    Replies: 1
    Last Post: November 9th, 2010, 11:57 AM
  3. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  4. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 10:35 PM
  5. Facing problem with open modeless dialog from modal window
    By Divya in forum Java Theory & Questions
    Replies: 1
    Last Post: May 14th, 2009, 03:18 AM