Results 1 to 8 of 8
  1. #1
    web_dev is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Open JPanel with JButton

    Hi
    I have tow class in brief:
    Java Code:
    //Base.java
    public class Base extends javax.swing.JFrame {
    
        public Base() {
            initComponents();
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Base().setVisible(true);
    
                }
            });
        }
    
    private javax.swing.JButton setting_btn;
    
    }
    And
    Java Code:
    //Settings.java
    public class Settings extends javax.swing.JPanel {
    
        public Settings() {
            initComponents();
        }
    
    }
    Main method is in base.java and I have a button on it.
    I want action that when click on button, open Jpanel that exist on seetings.java file. I use below code in base.java but it doesn’t work!
    Java Code:
     private void setting_btnMouseClicked(java.awt.event.MouseEvent evt) {
            // TODO add your handling code here:
            new Settings().setVisible(true);
    }
    How?

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

    Default

    The component: Settings must be added to a displayable container to be displayed. Just creating a new instance of it doesn't make it visible. It needs to be in a container that is visible.
    Last edited by Norm; 06-13-2011 at 09:48 PM. Reason: Wrong usage of container

  3. #3
    web_dev is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks Norm, How i can put it in a container? I'm new to java...

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

    Default

    You'd best read the Java Tutorial about building GUIs to get the full picture.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

  5. #5
    web_dev is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    JPanel isn't swing container?!

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by web_dev View Post
    JPanel isn't swing container?!
    See what a JPanel extends from: a JComponent and that class extends a Container; al Swing components do that so they're all Containers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    @web_dev - the point is that panels can't display on their own - like most other GUI components they need to be put in a main window, such as a JFrame, to display. GUI applications are built inside main windows, usually JFrames.

  8. #8
    web_dev is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks all friend
    I use JFrame first and put JPanel and content in it.

Similar Threads

  1. Adding JButton to JPanel
    By razmyasdfg in forum AWT / Swing
    Replies: 8
    Last Post: 05-23-2011, 11:03 PM
  2. open text document using JButton
    By coopc in forum New To Java
    Replies: 3
    Last Post: 05-04-2011, 12:02 AM
  3. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 06:53 AM
  4. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM
  5. how to click a jbutton and open an url
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:44 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
  •