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

Thread: Imitating a JFrame extended program with JPanel; help needed...

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Imitating a JFrame extended program with JPanel; help needed...

    hi all,
    there is a simple prog in the book "Sams teach ..." which extends
    JFrame. its an example for actionListener.
    since i heard i cant apply lookandfeel() for JFrame, i wanted to write
    the programe extending JPanel.
    the code i wrote correctly gets compiled. but nothing is getting
    displayed ( i mean no buttons popping out).

    please can you check whats the prob?

    below are the two codes. (first one from the book extending JFrame and
    next mine extending JPanel)

    thank you for your time and support.





    /* 
      * To change this template, choose Tools | Templates 
      * and open the template in the editor. 
      */ 
     
    package DayEleven; 
     
    /** 
      * 
      * @author arshad 
      */ 
     import java.awt.*; 
     import java.awt.event.*; 
     import javax.swing.SwingUtilities; 
     import javax.swing.UIManager; 
     
    public class AL extends Frame implements WindowListener,ActionListener 
     { 
     
            TextField text = new TextField(20); 
             Button b; 
             private int numClicks = 0; 
     
            public static void main(String[] args) { 
     
                    AL myWindow = new AL("My first window"); 
     
                    myWindow.setSize(350,100); 
     //        myWindow.lookAndFeel(); 
                     myWindow.setVisible(true); 
     
            } 
     
            public AL(String title) { 
     
                    super(title); 
                     setLayout(new FlowLayout()); 
                     addWindowListener(this); 
     
                    b = new Button("Click me"); 
                     add(b); 
                     add(text); 
                     b.addActionListener(this); 
     
            lookAndFeel(); 
     
            } 
     
        public void lookAndFeel(){ 
                     try{ 
                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
                     SwingUtilities.updateComponentTreeUI(this); 
                     }catch(Exception e){ 
                             System.out.println("errr"); 
                     } 
             } 
     
            public void actionPerformed(ActionEvent e) { 
                     numClicks++; 
                     text.setText("Button Clicked " + numClicks + " times"); 
     
            } 
     
            public void windowClosing(WindowEvent e) { 
                     dispose(); 
                     System.exit(0); 
             } 
     
            public void windowOpened(WindowEvent e) {} 
             public void windowActivated(WindowEvent e) {} 
             public void windowIconified(WindowEvent e) {} 
             public void windowDeiconified(WindowEvent e) {} 
             public void windowDeactivated(WindowEvent e) {} 
             public void windowClosed(WindowEvent e) {} 
     
    }



    --------------------------------------------------
    import java.awt.event.ActionEvent; 
     import java.awt.event.ActionListener; 
     import javax.swing.*; 
     import java.awt.*; 
     import net.miginfocom.swing.MigLayout; 
     
    /** 
      * 
      * @author arshad 
      */ 
     public class AL2 extends JPanel implements ActionListener{ 
     
        JTextField countText=new JTextField(); 
         JButton button=new JButton("Click to increment"); 
         private int numClicks=0; 
     
        public AL2(){ 
     
            super(); 
             themes(); 
             Dimension 
     d=java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 
             setSize(d); 
             JPanel pane=new JPanel(new MigLayout("Wrap 1")); 
     
            pane.add(countText); 
             pane.add(button); 
             add(pane); 
             button.addActionListener(this); 
             setVisible(true); 
     
        } 
     
        public void actionPerformed(){ 
             numClicks++; 
             countText.setText("Button CLicked"+numClicks+"Times"); 
         } 
     
        public void actionPerformed(ActionEvent arg0) { 
             throw new UnsupportedOperationException("Not supported yet."); 
         } 
     
        public static void main(String arg[]){ 
             AL2 a=new AL2(); 
         } 
     
        public void themes(){ 
     
    try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
             SwingUtilities.updateComponentTreeUI(this);}catch (Exception e) 
     { 
                 System.out.println("errror in applying the theme"); 
             } 
         }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Imitating a JFrame extended program with JPanel; help needed...

    JPanels need a display container to be visible, so you need to add it to the content pane of a Jframe and set the visibility of that JFrame to true to see it.

    JFrame frame = new JFrame();
    frame.getContentPane().add( new AL2());
    frame.setVisible(true);

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

    emigrant (February 18th, 2010)

Similar Threads

  1. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  2. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  3. array program assistance needed
    By JavaNoob82 in forum Collections and Generics
    Replies: 4
    Last Post: December 14th, 2009, 05:49 AM
  4. Array program help needed
    By SCM in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2009, 11:28 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM