Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: linking a class in a jbutton of netbeans gui form

  1. #1
    utsav1995 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Question linking a class in a jbutton of netbeans gui form

    Hello everyone
    I have found the following code of video player from net and i want to use it in my project.
    The problem is that i only work with netbeans gui interface whereas this code was in a class.
    All i know is drag and drop and a few commands.
    This code was of a class.I want it to be like suppose a gui jframe named "Video"
    There is a jbutton on the frame which has been dropped from the pallete.
    Suppose its variable name is changed to "openvid" and tex to "Click here to open video player".
    Now the thing i want is what should i do so that on cliking the jbutton, the frame closes and the class opens.
    I will appreciate all the help provided.
    Also this is my first post on this forums so i am not familiar with forum rules so if any mistake done then forgive me.

    Java Code:
    // MediaPlayer.java
    
    import javax.media.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class MediaPlayer extends Frame implements ActionListener,ControllerListener,ItemListener{
       Player player;
       Component vc, cc;
       boolean first = true, loop = false;
       String currentDirectory;
       
       MediaPlayer (String title)
       {
          super (title);
    
          addWindowListener
                    (new WindowAdapter ()
                     {
                         public void windowClosing (WindowEvent e)
                         {
                            // User selected close from System menu.
                            // Call dispose to invoke windowClosed.
    
                            dispose ();
                         }
    
                         public void windowClosed (WindowEvent e)
                         {
                            if (player != null)
                                player.close ();
    
                            System.exit (0);
                         }
                     });
    
          Menu m = new Menu ("File");
    
          MenuItem mi = new MenuItem ("Open...");
          mi.addActionListener (this);
          m.add (mi);
    
          m.addSeparator ();
    
          CheckboxMenuItem cbmi = new CheckboxMenuItem ("Loop", false);
          cbmi.addItemListener (this);
          m.add (cbmi);
    
          m.addSeparator ();
    
          mi = new MenuItem ("Exit");
          mi.addActionListener (this);
          m.add (mi);
    
          MenuBar mb = new MenuBar ();
          mb.add (m);
          setMenuBar (mb);
    
          setSize (500, 500);
          setAlwaysOnTop(true);
          setVisible (true);
       }
    
       public void actionPerformed (ActionEvent e)
       {
          if (e.getActionCommand ().equals ("Exit"))
          {
              // Call dispose to invoke windowClosed.
    
              dispose ();
              return;
          }
    
          FileDialog fd = new FileDialog (this, "Open File",FileDialog.LOAD);
          fd.setDirectory (currentDirectory);
          fd.show ();
    
          // If user cancelled, exit.
    
          if (fd.getFile () == null)
              return;
    
          currentDirectory = fd.getDirectory ();
    
          if (player != null)
              player.close ();
    
          try
          {
             player = Manager.createPlayer (new MediaLocator
                                               ("file:" +
                                                fd.getDirectory () +
                                                fd.getFile ()));
          }
          catch (java.io.IOException e2)
          {
             System.out.println (e2);
             return;
          }
          catch (NoPlayerException e2)
          {
             System.out.println ("Could not find a player.");
             return;
          }
    
          if (player == null)
          {
              System.out.println ("Trouble creating a player.");
              return;
          }
    
          first = false;
    
          setTitle (fd.getFile ());
    
          player.addControllerListener (this);
          player.prefetch ();
       }
    
       public void controllerUpdate (ControllerEvent e)
       {
          // A ControllerClosedEvent is posted when player.close is
          // called.  If there is a visual component, this component must
          // be removed.  Otherwise, this visual component appears
          // blanked out on the screen. (To be consistent, we do the same
          // thing for the control panel component.)
          //
          // Note: This problem occurs when run under JMF 2.1 Windows and
          //       SDK 1.3 on a Windows 98 SE platform.
    
          if (e instanceof ControllerClosedEvent)
          {
              if (vc != null)
              {
                  remove (vc);
                  vc = null;
              }
    
              if (cc != null)
              {
                  remove (cc);
                  cc = null;
              }
    
              return;
          }
    
          if (e instanceof EndOfMediaEvent)
          {
              if (loop)
              {
                  player.setMediaTime (new Time (0));
                  player.start ();
              }
    
              return;
          }
    
          if (e instanceof PrefetchCompleteEvent)
          {
              player.start ();
              return;
          }
    
          if (e instanceof RealizeCompleteEvent)
          {
              vc = player.getVisualComponent ();
              if (vc != null)
                  add (vc);
    
              cc = player.getControlPanelComponent ();
              if (cc != null)
                  add (cc, BorderLayout.SOUTH);
    
              pack ();
          }
       }
    
       public void itemStateChanged (ItemEvent e)
       {
          loop = !loop;
       }
    
       public void paint (Graphics g)
       {
          if (first)
          {
              int w = getSize ().width;
              int h = getSize ().height;
    
              g.setColor (Color.blue);
              g.fillRect (0, 0, w, h);
    
              Font f = new Font ("DialogInput", Font.BOLD, 16);
              g.setFont (f);
    
              FontMetrics fm = g.getFontMetrics ();
              int swidth = fm.stringWidth ("*** Welcome ***");
    
              g.setColor (Color.white);
              g.drawString ("*** Welcome ***",
                            (w - swidth) / 2,
                            (h + getInsets ().top) / 2);
          }
    
          // Call overridden Frame superclass paint method.  That method
          // will call each contained container and component (including
          // the control panel component) paint method.
    
          super.paint (g);
       }
    
       // Eliminate control panel component flicker by preventing frame
       // background from being cleared.
    
       public void update (Graphics g)
       {
          paint (g);
       }
    
       public static void main (String [] args)
       {
          new MediaPlayer ("Media Player 1.0");
       }
    }

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

    Default Re: linking a class in a jbutton of netbeans gui form

    For starters, you'll likely want to learn more about how to code Swing above and beyond NetBeans' drag-and-drop. Please have a look here: Swing Tutorial. Best of luck and welcome to our forum!
    utsav1995 likes this.

  3. #3
    utsav1995 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: linking a class in a jbutton of netbeans gui form

    Quote Originally Posted by Fubarable View Post
    For starters, you'll likely want to learn more about how to code Swing above and beyond NetBeans' drag-and-drop. Please have a look here: Swing Tutorial. Best of luck and welcome to our forum!
    Thank you for linking me with that topic but currently can you help me out what should i do in this case.
    Coz i have to submit this project in school in a few days.
    I dont want you guys to do the code for me all I want is tell me what should i do just now :)
    btw using drag and drop and some mouse events i made this small java game if you want test it and figure out how much i know.

    Utsav Maze Game

    EDIT:i didn't want pc so didn't double post but i would need help in one more thing its about converting a gui frame to applet to upload on web but that one i need later so solve my this problem :)
    Last edited by utsav1995; 05-30-2012 at 07:54 PM. Reason: coz i am not here for post count

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: linking a class in a jbutton of netbeans gui form

    I wouldn't use that code at all since it is AWT code, it doesn't obey Swing threading rules, and you are creating a Swing GUI. I'd recommend you create your code yourself based on your requirements and avoid using "borrowed" code as much as possible.

  5. #5
    utsav1995 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: linking a class in a jbutton of netbeans gui form

    Quote Originally Posted by Fubarable View Post
    I wouldn't use that code at all since it is AWT code, it doesn't obey Swing threading rules, and you are creating a Swing GUI. I'd recommend you create your code yourself based on your requirements and avoid using "borrowed" code as much as possible.
    1>well its been just 1 month that i have started learning java and i cant understand max of what you said like AWT code.
    umm can you guide me some links where i can learn the things you telling me so it would be easier for you to help me and easier for me to understand it :)

    2>I actually don't know that how this code works if i could have written such code why would i copy it, I just needed it for my java board exam project.

    I would learn java properly point to point after i clear my 12th boards:)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: linking a class in a jbutton of netbeans gui form

    Quote Originally Posted by utsav1995 View Post
    1>well its been just 1 month that i have started learning java and i cant understand max of what you said like AWT code.
    umm can you guide me some links where i can learn the things you telling me so it would be easier for you to help me and easier for me to understand it :)
    The link I've given in my first post is probably the best one to help you better understand Swing. AWT was the graphics library that was first used in Java 1.0, but it was then superseded by Swing which builds on AWT. The bottom line is though, since this code you've borrowed isn't Swing code, you really shouldn't use any of it in your program.

    2>I actually don't know that how this code works if i could have written such code why would i copy it, I just needed it for my java board exam project.
    Don't even bother knowing this code as you can't use it. Instead write out your requirements and based on your requirements, try to create your own code for your application. If you get stuck in your project, then bring *your* code and your questions, and we'll try to help you as best we can. Please realize that your board exam project is not our concern. Our goal is to help you learn Java, but what you do with it, or any school requirements you have are your issue not ours.

    I would learn java properly point to point after i clear my 12th boards:)
    Again, that's not our concern. But we'll be happy to help you with any specific questions you may have or help you fix bugs in your code, and will encourage you to do well with your studies and help cheer you to success. That we can do.

  7. #7
    utsav1995 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: linking a class in a jbutton of netbeans gui form

    Quote Originally Posted by Fubarable View Post
    The link I've given in my first post is probably the best one to help you better understand Swing. AWT was the graphics library that was first used in Java 1.0, but it was then superseded by Swing which builds on AWT. The bottom line is though, since this code you've borrowed isn't Swing code, you really shouldn't use any of it in your program.



    Don't even bother knowing this code as you can't use it. Instead write out your requirements and based on your requirements, try to create your own code for your application. If you get stuck in your project, then bring *your* code and your questions, and we'll try to help you as best we can. Please realize that your board exam project is not our concern. Our goal is to help you learn Java, but what you do with it, or any school requirements you have are your issue not ours.



    Again, that's not our concern. But we'll be happy to help you with any specific questions you may have or help you fix bugs in your code, and will encourage you to do well with your studies and help cheer you to success. That we can do.

    Ok ty will learn java properly then will consult you :)
    keep doing your work, you are doing great :)

Similar Threads

  1. Centering form in Netbeans
    By drimades in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2012, 09:48 AM
  2. Netbeans form wizard
    By garrym in forum New To Java
    Replies: 1
    Last Post: 04-14-2011, 12:46 AM
  3. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 07:53 AM
  4. Netbeans Master/Detail form and TableSorter
    By jmlee in forum NetBeans
    Replies: 0
    Last Post: 01-14-2010, 09:12 PM
  5. Netbeans Help JButton Imageicon
    By hitmen in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2009, 08:56 PM

Posting Permissions

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