Results 1 to 9 of 9
  1. #1
    shruts is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default panel is not visible on my Jframe

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;
    
    import javax.media.ControllerEvent;
    import javax.media.ControllerListener;
    import javax.media.Manager;
    import javax.media.Player;
    import javax.media.RealizeCompleteEvent;
    
    import javax.swing.*;
    import javax.swing.JOptionPane;
    
    
    
    
        public class demo2 extends JFrame implements Runnable,ActionListener{
        private Player player;
    final JFrame f =new JFrame();
        private File file;
        private JPanel panel;
    
    
    
        public demo2() {
    
            super("KABOOM Player");
            setLayout(new FlowLayout());
    
        f.setVisible(true);
        f.setSize(600,200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        initcomponents();
        }
        public void initcomponents(){
    
        JMenuBar mb= new JMenuBar();
    
        JMenu fileone=new JMenu("File");
        mb.add(fileone);
        JMenu playback=new JMenu("Playback");
        mb.add(playback);
    
        JMenuItem op=new JMenuItem("Open");
        fileone.add(op);
    
        JMenuItem ex=new JMenuItem("Exit");
        fileone.add(ex);
    
       /* JMenuItem prev=new JMenuItem("Previous");
        playback.add(prev);
    
        JMenuItem next=new JMenuItem("Next");
        playback.add(next); */
    
         JMenuItem st=new JMenuItem("Pause");
        playback.add(st);
    
        JMenuItem pl=new JMenuItem("Play");
        playback.add(pl);
    
         panel = new JPanel();
             panel.setPreferredSize(new Dimension(1,1));
         panel.setBackground(Color.red);
    
        JButton playit = new JButton("playss");
         playit.addActionListener(this);
    
             getContentPane().add(panel);
             panel.add(playit,FlowLayout.TRAILING);
    
      JLabel lbl=new JLabel(new ImageIcon("/e3.gif"))   ;
       lbl.setSize(300, 100);
    
       f.add(lbl);
    
        
        JButton stop=new JButton("Stop");
        f.setLocation(20, 0);
        f.setVisible(true);
        f.add(stop,BorderLayout.CENTER);
    
        JButton play1=new JButton("Play");
        play1.size();
        f.add(play1,BorderLayout.WEST);
    
    
    
    
            op.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    openFile();
                    createPlayer();
                }
            });
           ex.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                 f.hide();
                }
            });
    
            st.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                 player.stop();
                }
            });
    
             pl.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                 player.start();
                }
            });
    
            f.setJMenuBar(mb);
            f.add(mb,5000);
             f.setBackground(Color.red);
        }
    
    
        public static void main(String args[]) {
            demo2 app = new demo2();
            app.setTitle("KABOOM PLAYER");
            app.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
    
    
    
        }
         private void openFile() {
            JFileChooser fileChooser = new JFileChooser();
    
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = fileChooser.showOpenDialog(this);
    
            // user clicked Cancel button on dialog
            if (result == JFileChooser.CANCEL_OPTION)
                file = null;
            else
                file = fileChooser.getSelectedFile();
        }
    
        private void createPlayer() {
            if (file == null)
                return;
    
            removePreviousPlayer();
    
            try {
                // create a new player and add listener
                player = Manager.createPlayer(file.toURL());
               // player.addControllerListener(new EventHandler());
                player.start(); // start player
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Invalid file or location",
                        "Error loading file", JOptionPane.ERROR_MESSAGE);
            }
        }
    
        private void removePreviousPlayer() {
            if (player == null)
                return;
    
            player.close();
    
            Container c = getContentPane();
    
        }
    
        public void run() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void actionPerformed(ActionEvent e) {
            if(e.getActionCommand(). equals("play")){
                player.start();
            }
        }
    
      
    
    }



    here when i am adding panel then neither the play and stop button and neither the picture is appearing..blank screen comes ie. even panel is not visible.
    when i remove the panel the image and the two buttons appear on frame.
    but i want to add buttons on panel and then panel on frame..plz help
    Last edited by Fubarable; 07-31-2011 at 04:11 PM. Reason: code tags added

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

    Default

    I'm not 100% sure what is wrong, but my guess is that you're using layout managers incorrectly, and in fact your code throws exceptions for this. Have you read the tutorial on how to use layout managers?

    Note: I have edited your original post and have added code tags so your code will retain its formatting and be readable. To learn how to do this yourself, please read the link in my signature below.

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

    Default

    Also, you are using setSize() rather than the preferred setPreferredSize(), you are not calling pack() on the JFrame and you are adding components after setVisible(true) has been called on the JFrame. So add all components first, then call pack(), then call setVisible(true), in that order.

  4. #4
    shruts is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    i edited n implemented my code in that order but its not helping..
    if i remove the code for panel then the buttons are visible that are on the frame. but when the code for panel is there even those buttons are not visible

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

    Default

    Can you upload a picture what your GUI looks like and another picture of what you want it to look like? Either that or describe in great detail what you're trying to achieve because your current description kind of assumes that we know in advance what you're trying to achieve -- and we don't.

  6. #6
    shruts is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    i am just tryin to make a JFrame..on which i want to add a panel with few buttons on it and a picture. also i want to add a menu bar on the frame..THAT'S IT.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    i am just tryin to make a JFrame..on which i want to add a panel with few buttons on it
    Then post your Short, Self Contained, Correct Example that demonstrates this. Most of the code you posted is completely unrelated to that requirement. ActionListener and file chooser code for example is the next step once you solve this problem. Code your program one step at a time. That is get the components showing. Then add some ActionListeners to the buttons. Then add some menus etc. That way you debug and solve incremental problems which is easier than trying to debug an entire program.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    i am just tryin to make a JFrame
    Your code is creating two JFrames:
    Java Code:
      public class demo2 extends JFrame implements Runnable,ActionListener{  // One here
        private Player player;
    final JFrame f =new JFrame();  //<<<<<<<<<AND ONE HERE

  9. #9
    shruts is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Replies: 10
    Last Post: 04-11-2011, 09:34 PM
  2. JFrame, isn't visible sometimes..
    By AndroidAppNewbie in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 02:13 PM
  3. panel on a panel not visible
    By warchieflll in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:29 PM
  4. Label not visible on panel
    By warchieflll in forum Advanced Java
    Replies: 4
    Last Post: 01-29-2011, 01:29 PM
  5. How to add panel to JFrame?
    By geekchick in forum New To Java
    Replies: 17
    Last Post: 08-01-2010, 05:23 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
  •