Results 1 to 5 of 5
  1. #1
    DarkAnHell is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Question Two windows instead of one?

    Hello there, I'm new here :)

    I bet this is a really easy thing to solve, but I don't find a way to solve it since i'm just a newbie on Java.

    The problem here is that I'm trying to create one window with some things on it by code, but i have rewritten it so many times that i have reached some point where two windows are created.

    One of them has the things I want it to have (for now it just has a Label because i've realised this problem before adding more things), and the other is just a blank window with no size.

    Here is the code:
    Java Code:
    package home;
    import java.awt.AWTException;
    
    import static java.awt.Frame.ICONIFIED;
    import static java.awt.Frame.NORMAL;
    
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    @SuppressWarnings("serial")
    public class HOME extends javax.swing.JFrame {
        
    
        public HOME() {
            initComponents();
    
            //
            // Add image icon
            // 
            Image image = Toolkit.getDefaultToolkit().getImage("image.gif");
            
            // Name on display when hovering mouse
            trayIcon = new TrayIcon(image, "H.O.M.E.", null);       
    
        }
        
                             
        private void initComponents() {
    
            //Components of tray icon
            //*****************************
            popMen = new javax.swing.JPopupMenu();
            menuItemOptions = new javax.swing.JMenuItem();
            separator = new javax.swing.JSeparator();
            menuItemClose = new javax.swing.JMenuItem();
            menuItemStartStop = new javax.swing.JMenuItem();
            menuItemRestart = new javax.swing.JMenuItem();
            TextPop = new javax.swing.JLabel();
    
            //adding a Label field to use it as a message system. <<Add an icon??>>
            // And do it more.. clean.
            TextPop.setText("            H.O.M.E.");
            popMen.add(TextPop);
            
            // adding the buttons on the menu
            
            //this first one will serve as start and stop button, changing his name.
            menuItemStartStop.setText("Start");
            menuItemStartStop.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    menuItemStartStopActionPerformed(evt);
                }
            });
            popMen.add(menuItemStartStop);
            
            menuItemRestart.setText("Restart");
            menuItemRestart.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    menuItemRestartActionPerformed(evt);
                }
            });
            popMen.add(menuItemRestart);
            //There is no point on "restart" if you haven't started already.
            menuItemRestart.setEnabled(false);
            
            
            
            menuItemOptions.setText("Options");
            menuItemOptions.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    menuItemOptionsActionPerformed(evt);
                }
            });
            popMen.add(menuItemOptions);
            popMen.add(separator);
    
            menuItemClose.setText("Close");
            menuItemClose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    menuItemCloseActionPerformed(evt);
                }
            });
            popMen.add(menuItemClose);
    
            
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            
            
            
            //End of components of tray icon
            //****************************************
            
            //Components of main window
    
            
            
            Frame = new javax.swing.JFrame();
            Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Text1 = new javax.swing.JLabel("", JLabel.LEFT);
            Text2 = new javax.swing.JLabel();
            Text3 = new javax.swing.JLabel();
            Panel = new javax.swing.JPanel();
            
            Panel.setBackground(Color.white);
            Panel.setLayout(null);
            
            Text1.setText("Start on system startup");
            Text1.setSize(300, 30);
            Text1.setLocation(5,5);
            
            
            
            Panel.add(Text1);
            
            Frame.setContentPane(Panel);
            Frame.setSize(400, 200);
            Frame.setVisible(true);
            
            
            Frame.setTitle("H.O.M.E. Proyect");
            Frame.setResizable(false);
            Frame.addWindowStateListener(new java.awt.event.WindowStateListener() {
                public void windowStateChanged(java.awt.event.WindowEvent evt) {
                    StateChanged(evt);
                }
            });
            
            
    
    
            pack();
        }                        
        
        private void StateChanged(java.awt.event.WindowEvent evt) {                                
            
            if ( evt.getNewState() == ICONIFIED) {
                this.setState(NORMAL);
                this.setVisible(false);
    
                if (SystemTray.isSupported()) {
                    
                    tray = SystemTray.getSystemTray();
    
                    MouseListener mouseListener = new MouseListener( ) {
    
                        public void mouseClicked(MouseEvent e) {
                            if ( e.getButton()==MouseEvent.BUTTON1 ) {
                                //Just a debug message. Change it maybe?
                                trayIcon.displayMessage(
                                        "H.O.M.E", 
                                        "Click on this message to open up the"
                                        + " options window.    "
                                        + "Click with the right button of your mouse on the icon"
                                        + " to open up a simple menu.",TrayIcon.MessageType.INFO);
                            }
                            
                        }
                        // Just hadling this ones, but no use for now.
                        public void mouseEntered(MouseEvent e) {
    
                        }
    
                        public void mouseExited(MouseEvent e) {
    
                        }
    
                        public void mousePressed(MouseEvent e) {
                            
                        }
                        //
                        
                        public void mouseReleased(MouseEvent e) {
                            if (e.isPopupTrigger()) {
                                popMen.setLocation(e.getX(), e.getY());
                                popMen.setInvoker(popMen);
                                popMen.setVisible(true);
                            }                    
                        }
                    };
    
                    trayIcon.setImageAutoSize(true);
                    trayIcon.addMouseListener(mouseListener);
                    trayIcon.addActionListener( new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                menuItemOptionsActionPerformed(e);
                            }
                        }
                    );
    
    
                    try {
                        tray.add(trayIcon);
                    } catch (AWTException e) {
                        System.err.println("Couldn't add program to tray bar");
                        this.setVisible(true);
                    }
                } else {
                    System.exit(1);
                }
            }
        }                               
    
        private void menuItemStartStopActionPerformed(java.awt.event.ActionEvent evt){
            //handling the 3 possibilities: The user clicked while on Start, while on Stop
            // or.. something went wrong and the text is neither of those
            if("Start".equals(menuItemStartStop.getText())){
                
                if(MProc.Start() == 0 ||MProc.Start() == 1){
                    // If the code run
                    
                    
                }
                else if(MProc.Start() == -1){
                    //Error message??
                    // Log system??
                }else{
                    // Another weird error that shouldn't appear...
                }
                
            }
            else if("Stop".equals(menuItemStartStop.getText())){
                
                if(MProc.Stop() == 0 ||MProc.Stop() == 1){
                    // If the code run. Also, log system??
    
                }
                else if(MProc.Stop() == -1){
                    //Error message??
                    // Log system??
                }else{
                    // Another weird error that shouldn't appear...
                }
                
                
            }else{
                System.exit(1); // This shouldn't happen..
            }
        }
        
        private void menuItemRestartActionPerformed(java.awt.event.ActionEvent evt){
            if(MProc.Restart() == 0 ||MProc.Restart() == 1){
                    // If the code run
    
                }
                else if(MProc.Restart() == -1){
                    //Error message??
                    // Log system??
                }else{
                    // Another weird error that shouldn't appear...
                }
            
        }
        
        
        private void menuItemOptionsActionPerformed(java.awt.event.ActionEvent evt) {                                                
            this.setVisible(true);                                               
            this.toFront();
            tray.remove(trayIcon);
            //not sure about removing the tray icon... Should i leave it?
        }
    
        private void menuItemCloseActionPerformed(java.awt.event.ActionEvent evt) {                                              
            MProc.Stop();
            System.exit(0);
        }  
        
        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new HOME().setVisible(true);
                   
                }
            });
        }
        
        // Variables declaration                    
        public static javax.swing.JMenuItem menuItemOptions;
        public static javax.swing.JMenuItem menuItemClose;
        public static javax.swing.JMenuItem menuItemStartStop;
        public static javax.swing.JMenuItem menuItemRestart;
        private javax.swing.JPopupMenu popMen;
        private javax.swing.JSeparator separator;
        public static javax.swing.JLabel TextPop,Text1,Text2,Text3;
        public static javax.swing.JCheckBox ChkBox;
        private javax.swing.JPanel Panel;
        private javax.swing.JFrame Frame;
                       
    
        private SystemTray          tray;
        private final TrayIcon      trayIcon;
    
        private final MainProcedures MProc = new MainProcedures(); 
    
        // End of variables declaration    
    
    
    }
    The MProc function is on another java file, but it doesn't touches the graphical thing so i didn't added it here.
    Also, the code is full of coments because I make notes to myself about it :p. Also, because i plan on adding some people to the proyect.

    Anyway, does someone see the error here?...
    Thanks :)

    PS: I know I may have posted many lines that doesn't have anything to do with the graphical stuff but.. since I couldn't find the error by myself, I thought it would be better not to delete any lines, just in case.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Two windows instead of one?

    You extend JFrame in your main class HOME (which you really shouldn't do). Then you create an instance of HOME and setVisible(true). There is one window. Then you do an explicit creation of a JFrame. There is the other window.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarkAnHell is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Thumbs up Re: Two windows instead of one?

    Well that was a quick answer!

    I don't have time to change it now but i will definetly solve it in no time, thanks to you!

    Also, double thanks for the advice on the extension matter, I will look it up :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Two windows instead of one?

    Quote Originally Posted by DarkAnHell View Post
    Also, double thanks for the advice on the extension matter, I will look it up :)
    I am not certain that is something you can look up. It is a matter or proper technique. Normally, you don't extend something unless you are going to override some methods or add functionality. So extending a JFrame is usually not necessary. Simply create an instance of it in your class and add other components such as JLables, and JPanels to it. In OO lingo this is referred to as composition in preference to subclassing.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    DarkAnHell is offline Member
    Join Date
    Jul 2014
    Posts
    3
    Rep Power
    0

    Default Re: Two windows instead of one?

    I see. Thank you again, the code is woking just fine :)

Similar Threads

  1. Replies: 1
    Last Post: 05-03-2013, 02:57 PM
  2. Replies: 1
    Last Post: 05-02-2013, 08:11 PM
  3. Replies: 13
    Last Post: 07-20-2012, 07:58 PM
  4. Windows java vs Windows AIX
    By sysp in forum New To Java
    Replies: 0
    Last Post: 01-26-2011, 05:32 AM
  5. swt for windows
    By Gajesh Tripathi in forum SWT / JFace
    Replies: 2
    Last Post: 10-18-2007, 06:43 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
  •