Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

swing problem: can't update JPanel

Thread view: 
djbitchpimp@snowboard.com - 25 Jan 2006 09:06 GMT
I am making a simple media player for the Java Media Framework.
Basically I want it to be able to open a video from a url and play it
in a swing component.

The problem I am having is that when I start my player, the JPanel that
I want to redraw with the player window will not refresh itself to show
the video screen. Right now it is displaying the control panel
component but not the video component.

I am not very familiar with swing, so could someone please point out
any obvious errors in my code where this might be happening? Thanks!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;

public class MediaPlayerGui implements ActionListener{

    protected JFrame clientWin ; // window frame
    protected Component playerWin ; // the visual component of the player
    protected Component controls ; // player controls
    final JFileChooser open = new JFileChooser () ;
    final JButton stopButton = new JButton ("Stop"); // stop button
    final JButton openButton = new JButton ("Open File"); // open button
    final JButton openURLButton = new JButton ("Open URL"); // openURL
button
    final JTextField URLlocation = new JTextField (22) ; // URL text area
    final JLabel URLLabel = new JLabel ("URL:") ;
    final JLabel loopStartLab = new JLabel ("Loop Start:") ;
    final JLabel loopEndLab = new JLabel ("Loop End:") ;
    final JTextField loopStart = new JTextField (7);
    final JTextField loopEnd = new JTextField (7);
    final JPanel pane = new JPanel(new BorderLayout ());
    final JCheckBox loopCheck = new JCheckBox ("Auto Restart") ;
    final JCheckBox autoCheck = new JCheckBox ("Loop") ;
    final JPanel windowPanel = new JPanel () ;
    Player player ;
    String dataLocation = new String () ; // a string to hold the data
location
    MediaLocator dataSource ; // the datasource to be played
    GridBagConstraints c = new GridBagConstraints();
    //DataSource mediaSource = new DataSource () ;

    public MediaPlayerGui () {
        Manager man ;

        dataSource = new MediaLocator (dataLocation) ;

        // get the preferred size of the media source

        player = null ;

        windowPanel.setLayout(new GridLayout (0,1)) ;
        windowPanel.setBorder(BorderFactory.createEmptyBorder(
               0, //top
               0, //left
               240, //bottom
               320) //right
       );
        windowPanel.setBackground(Color.black) ;

    }

    public Component createComponents() {

       stopButton.addActionListener(this);
       openButton.addActionListener(this);
       openURLButton.addActionListener(this);

       pane.setLayout (new GridBagLayout ());

       c.gridx = 0;
       c.gridy = 0;
       c.anchor = GridBagConstraints.LINE_START ;
       pane.add(openButton, c);

       c.gridx = 1;
       pane.add(stopButton, c);

       c.gridx = 0;
       c.gridy = 1;
       c.gridwidth = 4 ;
       pane.add(windowPanel, c);

       c.gridx = 0;
       c.gridy = 2;
       pane.add(URLLabel, c);

       c.gridx = 1;
       c.gridy = 2;
       pane.add(URLlocation, c);

       c.gridx = 0;
       c.gridy = 3;
       pane.add(openURLButton, c);

       c.gridx = 0;
       c.gridy = 4;
       pane.add(loopCheck, c);

       c.gridx = 0;
       c.gridy = 5;
       pane.add(autoCheck, c);

       c.gridx = 0;
       c.gridy = 6;
       pane.add(loopStartLab, c);

       c.gridx = 1;
       c.gridy = 6;
       pane.add(loopStart, c);

       c.gridx = 0;
       c.gridy = 7;
       pane.add(loopEndLab, c);

       c.gridx = 1;
       c.gridy = 7;
       pane.add(loopEnd, c);

       return pane;
   }

    public static void main(String[] args) {

        JFrame clientWin = new JFrame ("Media Player") ;
        MediaPlayerGui window = new MediaPlayerGui () ;
        Component contents = window.createComponents() ;
        clientWin.getContentPane().add(contents);

        clientWin.pack();
        clientWin.setVisible(true);

        clientWin.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });
    }

    public void actionPerformed (ActionEvent e) {
        if (e.getSource() == openButton) {
            int returnVal = open.showOpenDialog(pane);
       }
        else if (e.getSource() == stopButton) {
            player.stop () ;

        }
        else if (e.getSource() == openURLButton) {
            // get the text from the textbox
            String URL = URLlocation.getText() ;

            // make a medialocator with that URL
            dataSource = new MediaLocator (URL) ;
            //System.out.println(URL) ;

            try {
                player = Manager.createRealizedPlayer(dataSource) ;
            } catch (Exception ex) {
                ex.printStackTrace() ;
            }

            playerWin = player.getVisualComponent() ;
            controls = player.getControlPanelComponent() ;

            windowPanel.add (playerWin) ;
            windowPanel.add (controls) ;
            player.start () ;
            windowPanel.repaint();
           
        }
    }
}
Thomas Weidenfeller - 25 Jan 2006 09:54 GMT
> The problem I am having is that when I start my player, the JPanel that
> I want to redraw with the player window will not refresh itself to show
> the video screen. Right now it is displaying the control panel
> component but not the video component.

Likely the typical threading mistake. See the comp.lang.java.gui FAQ,
list of the Top 5 questions.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.