JFrame calling a JFrame....

Hello,
I was trying to create a JFrame which has a BorderLayout() and there is a button set at the SOUTH of the layout of this JFrame........
Now when I click on this button..... this event would create another JFrame which also has a BorderLayout() and a button located at its SOUTH......
Now the problem is that when I click on the button of the first JFrame, it creates another JFrame as expected but it does not create the button at the SOUTH of this new JFrame...... How do I overcome this problem......
The code is given below..........
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TT extends JFrame implements ActionListener
     JButton but;
     public TT()
          setLayout(new BorderLayout());
          but=new JButton();                    
          but.addActionListener(this);
          add(but,BorderLayout.SOUTH);
     public void actionPerformed(ActionEvent ae)
          JFrame jj=new JFrame();          
          JButton jb=new JButton();
          jj.setLayout(new BorderLayout());
          jj.setSize(100,100);
          jj.setLocation(300,300);
          jj.setVisible(true);
          add(jb,BorderLayout.SOUTH);
     public static void main(String args[])
          TT obj=new TT();
          obj.setSize(400,400);
          obj.setLocation(200,200);
          obj.setVisible(true);
}It would be highly appreciated if anybody who knows the answer would take the PAIN of answering this question....... Thank you in advance.......

Hai,
Execute this modified code. It's executing perfectly. U better assign some text to the button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TT extends JFrame implements ActionListener
     JButton but;
     public TT()
          getContentPane().setLayout(new BorderLayout());
          but=new JButton();                    
          but.addActionListener(this);
          getContentPane().add(but,BorderLayout.SOUTH);
     public void actionPerformed(ActionEvent ae)
          JFrame jj=new JFrame();          
          JButton jb=new JButton();
          jj.getContentPane().setLayout(new BorderLayout());
          jj.setSize(100,100);
          jj.setLocation(300,300);
          jj.setVisible(true);
          jj.getContentPane().add(jb,BorderLayout.SOUTH);
     public static void main(String args[])
          TT obj=new TT();
          obj.setSize(400,400);
          obj.setLocation(200,200);
          obj.setVisible(true);
}rgds,
rdRose

Similar Messages

  • JFrame calling another JFrame

    Hello,
    I have a simple question:
    2 java files with 2 JFrames in them
    JFrame1(Cars.java) calls JFrame2(AddCar.java) from actionlistener.
    What I think is wrong is JFrame1 actionListener:
    if(ae.getSource() == CarEdit_btn){
    AddCar objEditAddCar = new AddCar();
    objEditAddCar.EditCar();
    And JFrame2's EditCar() method:
    public void EditCar(){
    AddCar windowAddCar1 = new AddCar();
    windowAddCar1.setTitle("Add Car");
    windowAddCar1.pack();
    windowAddCar1.show();
    AddCarMilage_txt.setText("544");
    The problem is that AddCarMilage_txt.setText("544"); doesnt update the value of the JTextField of JFrame2.
    Any ideas? Thanks
    Or another way to go around for me is, how do I use args[] from main inside the constructor?

    read this:
    http://forum.java.sun.com/thread.jspa?threadID=5199666&messageID=9792440#9792440
    it has also 2 classes and updates the textfield of the other class just copy and paste the code.

  • A JFrame calling another JFrame running a Thread

    Hi,
    here is my problem: I have an instance of Frame1 (extending JFrame) containing a button. When this button is clicked, a new instance of Frame2 (extending JFrame too) is created. The constructor of this Frame2 then calls the run method of an implementation of Runnable (called MyThread). For the purpose of this example I put a simple incrementation of a counter in the run method of the thread.
    The goal is to stop the incrementation by clicking anytime on the stop button of Frame2, hence ending the thread.
    When I create a new instance of Frame2, it works fine and I am able to end the thread, but when I create an instance of Frame1, the application freezes and I don't get any control on Frame2.
    The code for those classes is:
    public class Frame1 extends JFrame implements ActionListener{
         public static void main(String[] args) {
              new Frame1();
         public Frame1(){
              getContentPane().setLayout(new GridLayout(1,1));
              JButton b = new JButton("Run");
              b.addActionListener(this);
              getContentPane().add(b);
              setVisible(true);
              setSize(400,400);          
         public void actionPerformed(ActionEvent e) {
              Frame2 f= new Frame2();
    public class Frame2 extends JFrame implements ActionListener{
         MyThread t;
         boolean running = true;
         public Frame2(){
              getContentPane().setLayout(new GridLayout(1,1));
              JButton b = new JButton("Stop");
              b.addActionListener(this);
              getContentPane().add(b);
              setVisible(true);
              setSize(200,200);          
              t = new MyThread();
              t.run();
         public void actionPerformed(ActionEvent e) {
              t.setRunning(false);
    public class MyThread implements Runnable{
         boolean running = true;
         public void run(){
              int count = 0;
              while(isRunning())
                   count++;
              System.out.println("Last count: "+count);
         public boolean isRunning() {
              return running;
         public void setRunning(boolean running) {
              this.running = running;
    One thing I have tried is to make Frame2 implementing Runnable, but it didn't work. Any idea how to get the control on Frame2?
    Thanks

    You are calling t.run() and you really should be using t.start(). The code inside run() in MyThread is not running in a separate thread, so the call that you make to t.run() never returns.

  • Java.lang.stackoverflowerror issue when calling another jframe

    i created a login screen with a username/password field that i call from my main applicaton. I call the jframe with the following code
      createDirectories();
            startNotes();
            login.start();
            getLists();once it gets to the login.start(); it give the following error
    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
    at sun.awt.Win32GraphicsConfig.getBounds(Win32GraphicsConfig.java:240)
    at java.awt.Window.init(Window.java:368)
    at java.awt.Window.<init>(Window.java:407)
    here is the full code for my login.java minus the swing component creation
    import javax.swing.JOptionPane;
    public class loginScreen extends javax.swing.JFrame {
        TESTapplication app = new TESTapplication();
            patientObject patient = new patientObject();
        public loginScreen() {       
            initComponents();        
       //initcomponents removed from here
    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                        
            sendLogin();
        private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.out.println("User exited application");
            System.exit(3);
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {              
                 // start();
        // Variables declaration - do not modify                    
        private javax.swing.JButton btnCancel;
        private javax.swing.JButton btnLogin;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel lblLoginInfo;
        private javax.swing.JPasswordField pswrdFldPassword;
        private javax.swing.JTextField txtFldUsername;
        // End of variables declaration                  
        public void start(){
            new loginScreen().setVisible(true);
        public void sendLogin(){
            if (
                    txtFldUsername.getText().trim().equals("") ||  //trim removes whitespace
                    pswrdFldPassword.getText().trim().equals("") )
                JOptionPane.showMessageDialog(null,"Complete empty field(s)","Warning",JOptionPane.WARNING_MESSAGE);
            } else {
                patient.setUsername(txtFldUsername.getText().trim());
                patient.setPassword(pswrdFldPassword.getText().trim());
                patient.setAction("login");
                app.sendPatient();
    }thanks for all help given

    I only need to call 1 new login screen and no new application... how can i call a
    function from another java file?From a method of any class you can invoke (call) methods of other classes. I use "method" rather than "function" here to emphasise that they are much more than bits of code that do something: they have a context (the object of which they are a method).
    for instance i want to call the start function of the loginScreen.java from the
    testapplication.java. i don't need any new pieces, just want to be able to open the
    loginscreen jframe from the test application jframe.I wondered before why that start() method was creating a new loginScreen, but leaving that aside... One way might be for the TESTapplication class to have a member variable of type loginScreen. When some method of TESTapplication wants to invoke the start() method it just does so:loginScreen myLoginScreen = new loginScreen();
    // much later...
    myLoginScreen.start();There is no need for the loginScreen to have its own TESTapplication member variable since, as you say. "i don't need any new pieces".
    The start() method appears to next to nothing. It makes the loginScreen instance visible. But there's already a public method to do that. Perhaps loginScreen should be a modal JDialog: calling setVisible(true) will allow the login screen to do its thing and not return until it has finished (without blocking the event queue).
    It's all really a bit speculative and will remain so until you break down your application into small pieces: not functions (actions), but classes (types of obejcts including the things they can do.) Each class should have a clear and documented description of what it does.

  • How sholud we call one jframe class from another jframe class

    Hi
    In my application i am calling one jframe class from another jframe clas.
    how sholud we make previous jframe inactve when another jframe is invoked?(user sholud not able to make any changes on on parent jframe window when another jframe is invoked)
    Pls reply.

    Sorry for me it is not possible to change existing code,
    pls suggest me any other solution so that i can inactive parent jframe when child jframe execution is going on.

  • JFrame call from jsp

    Hi, i need to call a JFrame from JSP if you now how can i do it please tell me.

    Mr. Strider,
    1) JSP are server applications
    Sometimes the server that is running JSPs is a Unix (or a mainframe!) box that has no X Server and is accessible only via
    telnet/ssh.
    2) JFrames are used in Swing client applications
    The classic example is running a JApplet that hosts JFrames in a
    browser page in a Windows computer.
    You must write two programs - the JSP that will show you a web page and
    an JApplet that will be shown in the browser. You can not call directly
    the JFrame from the JSP.

  • Please help - JFrame, menu, another JFrame, setBackground

    I am trying to make a simple program that draws a JFrame with a menu. The menu has one item, which when selected draws another JFrame. That JFrame has a button, which when pressed changes the background color. Many hours have gone into this, despite the fact that I made a similar program a few weeks ago.
    The compiler complains about the method getContentPane, when I remove that, it runs and draws the first window, but the menu item does not make the second window. Also, the setDefaultCloseOperation confuses the compiler as well.
    Please help.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class MenuAction2
    public static void main(String[] args) 
         MainWindow window = new MainWindow("Menu action example");
      //window.setDefaultCloseOperation.(JFrame.EXIT_ON_CLOSE);
         window.setVisible(true);      
    class MainWindow extends JFrame
         MainWindow(String title)
         super(title);
         getContentPane.setLayout(new BorderLayout());
         Toolkit theKit = getToolkit();          
         Dimension wndSize = theKit.getScreenSize();  
         setBounds(0, 0,wndSize.width, wndSize.height);
         setJMenuBar(menuBar);
         fileMenu = new JMenu("File");
         connectItem = new JMenuItem();
         connectItem = fileMenu.add("Connect...");
         connectItem.addActionListener(menuHandler = new Handler());
         menuBar.add(fileMenu);
         getContentPane.add(toolBar, BorderLayout.NORTH);  
    class Handler implements ActionListener
         public void actionPerformed(ActionEvent e)
          win = new ConnectWindow("Connect to Database");
          getContentPane().add(win, BorderLayout.NORTH);
          setVisible(true);
      private JMenu fileMenu;
      private JMenuItem connectItem;
      private JMenuBar menuBar = new JMenuBar(); 
      private JToolBar toolBar = new JToolBar();
      private Handler menuHandler;
      private ConnectWindow win;
    class ConnectWindow extends JFrame
         ConnectWindow(String title)
              super(title);
              setSize(200,200);
              setLayout(new BorderLayout());
              JButton connectB = new JButton("Connect");
              connectB.addActionListener(new ActionListener()
                   public void actionPerformed(ActionEvent e)
                        setBackground(Color.red);
              add(connectB,BorderLayout.SOUTH);
              setVisible(true);
    }

    getContentPane is a method so you should use it that way:
    getContentPane().setLayout(new BorderLayout());
    getContentPane.add(new Button("Ok"));

  • Open a jFrame on another jFrames close

    New to java
    I have 2 jFrames, frame1.java & frame2.java
    frame1 has a button which makes frame1 invisible and opens frame2.
    I can do the above.
    but when I close frame2 I want to re-open frame1.
    I do not want to use a button on frame2, I want to use the X at the top right of the jFrame.
    I'm using NetBeans 6.1
    Please help
    Thanks

    Recommended: don't use 2 JFrames, use one JFrame and make the second a modal JDialog.
    But if you must do it this way, take a look at WindowListener#windowClosing.
    In future (not this time), Swing related questions should be posted in the [Swing forum|http://forums.sun.com/forum.jspa?forumID=57]
    db

  • Calling modal JFrame from  JFrame?? is it possible

    query desc:
    i have created a frame and there is a button on that frame.
    on push of the button i want a modal frame to open up..
    i am unable to make this modal its modless by default in my code.
    or
    how to put a frame inside a Jdialog
    and calling JDIalog from a button on another frame.
    any help ???

    nileshweb wrote:
    i have created a frame and there is a button on that frame.
    on push of the button i want a modal frame to open up..You can't do this.
    i am unable to make this modal its modless by default in my code.Right, again you can't do this.
    how to put a frame inside a Jdialog
    and calling JDIalog from a button on another frame.And you can't do this either.
    What you need to do is create a class that creates a JPanel, not a JFrame. In your main application (that's within a JFrame) when the JButton is pushed, create a modal JDialog (or perhaps better a JOptionPane) and place the created JPanel in the dialog or optionpane and then show. This will work, and fairly easily.
    Usually even my main application is created in a JPanel (I'm trying not to subclass unless absolutely necessary), and I add the JPanel to a JFrame when I want to show this main app. Later, if I decide that I want to show the main app as a JApplet, it's a trivial thing to just create the JApplet, add my main JPanel to it, and show the JApplet. Programming this way increases your flexibility tremendously.

  • Calling another JFrame

    I have only takin classes with JSP, and I am trying to develop a windows application. (first off, am I in the right forum?)
    Question:
    Lets say I have a pane, with everything I need in that pane. I now want to make my admin button close the current jFrame thats open and open the admin jFrame. How do I call it?
    Thanks for any help! I know its a simply question, but I just can't seem to get it.

    I have only takin classes with JSP, and I am trying
    to develop a windows application. (first off, am I in
    the right forum?)Not really but it doesn't stop everyone else! You will get more expert advice in the Swing forum. However I'm impressed by your politeness and post the two classes you need below. I just hope I haven't done your homework!
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class FirstJFrame extends JFrame {
         public FirstJFrame(){
              super("First JFrame");
              JButton toAdmin = new JButton("Go to Admin");
              toAdmin.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e) {
                        new AdminJFrame();
                        dispose();
              JButton dispose = new JButton("Quit");
              dispose.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e) {
                        System.exit(0);
              JPanel jp = new JPanel();
              jp.add(toAdmin);
              jp.add(dispose);
              add(jp);
              setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
              setSize(300,300);
              setVisible(true);
         public static void main(String[] args){
              new FirstJFrame();
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class AdminJFrame extends JFrame {
         public AdminJFrame(){
              super("Admin JFrame");
              JButton toFirst = new JButton("Go to First JFrame");
              toFirst.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e) {
                        new FirstJFrame();
                        dispose();
              JButton dispose = new JButton("Quit");
              dispose.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e) {
                        System.exit(0);
              JPanel jp = new JPanel();
              jp.add(toFirst);
              jp.add(dispose);
              add(jp);
              setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
              setSize(300,300);
              setVisible(true);
    }

  • Jframe calls jframe

    hi every body
    my problem is :
    i have a main Jframe with a button its name is "search", when i click on it , i want a second frame appear with a message like "wait until search completed"and the rest of search code below it when the search completed i make the second frame disapear. this is logically acceptabland easy to do , but when i execute it with this senario the second frame appear and dissapear quickly .
    i do not know why inspite of the time of seach is enough to give the second frame the chance to appear .
    please help
    sorry for my weak language
    thanks in advance

    Are you sure that the end of the search is what triggers the window to close? It sounds like you are doing some thing like this:
    waitFrame = new WaitFrame();
    waitFrame.dispose();

  • Bind Child JFrame to Parent JFrame

    Is there anyway of creating a JFrame that could have another JFrame attached to it? I don't so much mean an JInternalFrame, because what I need is a separate window (a properties window) that shows the user information, but doesn't register with the OS as a new window (i.e. place itself in the Windows taskbar). As far as I know, a JInternalFrame will actually be limited to the bounds of the JFrame containing it. I want mine to be a fully separate window, just bound to the parent window.
    Is it possible to do this by making both windows JInternalFrames and adding them both to a JDesktopPane???
    Thanks in advance for the help.

    What's with all those asterisks in your code? They're very distracting and prevent us from using your code.
    Anyway, the second window should be a JDialog, not a JFrame. I have to wonder why you call initComponents twice? Perhaps by doing so, the components displayed are not the ones with actionListeners added. It's hard to say not being able to run and test your code. Can you code Swing without using NetBeans code generation? If so, you should create a simpler example of this, one that we can run and test, and let us see exactly what is wrong. If you can't do this, then it would be worth your while to learn more Swing from the Sun/Oracle tutorials as this will serve you well in the long term.

  • How to resize the components in a JFrame when the JFrame Resizes?

    Hi all,
    i have some problems with my app. I have set the JFrame resizable and that works fine for the JFrame but not for the components in the JFrame.
    So my question is how can i set the components in a JFrame to resize automatically when the JFrame resizes?
    Here are the important things from my code,...if you need more, just let me know, its my first post in a forum .
    Its a JFrame with a JTabbedPane and on the Tabs are Panels with buttons and Tables.
    Thank you in advance!
    public class MainMonitor extends JFrame {
    public MainMonitor() {
              super();
              initialize();
              setVisible(true);
         private void initialize() {
              this.setSize(1145, 785);
              this.setIconImage(Toolkit.getDefaultToolkit().getImage("Images/c.gif"));
              this.setContentPane(getJContentPane());
              this.setTitle("Company Manager");
              this.setResizable(true);
              this.setLocationRelativeTo(null);
         private JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new JPanel();
                   jContentPane.setLayout(null);
                   jContentPane.setOpaque(true);
                   jContentPane.add(getJTabbedPane(), null);
                   jContentPane.add(getJPanel11(), null);
              return jContentPane;
         private JTabbedPane getJTabbedPane() {
              if (jTabbedPane == null) {
                   jTabbedPane = new JTabbedPane();
                   jTabbedPane.setLocation(new Point(6, 69));
                   jTabbedPane.setSize(new Dimension(1120, 684));
                   jTabbedPane.addTab("P", null, getJPanel(), null);
                   jTabbedPane.addTab("K", null, getJPanel1(), null);
                   jTabbedPane.addTab("B", null, getJPanel2(), null);
              return jTabbedPane;
         

    Giasaste wrote:
    Thanks a lot, i didnt knew that the Layout Manager is a must.It's not a must, but it is the way to allow your app to be resized nicely and makes it much easier to maintain and enhance your program. Imagine creating a complex gui then later decide that you want another radiobutton and to remove a jlabel / jtextfield pair. with layout managers a chore becomes easy.

  • Killing JFrames in a JFrame

    Hi,
    I have a few JFrames popping up in my program with GUI stuff in it, what i want to do is to use the 'OK' / 'CANCEL' buttons i put in the JFrames to work, i.e. 'Cancel' should kill the internal JFrame and 'Ok' should do something with my inputted values and then exit, any help greatly appreciated :)

    Hi there
    Why exactly are yo using JFrames when you easily use a JOptionPane.showConfirmDialog(...) to get input from your users?
    This saves you the over-head of hiding the dialog after receiving the input.
    The ICEMAN

  • Launching a JFrame from another JFrame

    Hi,
    I have a JFrame containing a menu(buttons). When I click on a button, I want that it launches another JFrame.
    I tried making use of the code
    Process p = Runtime.getRuntime().exec("java CreateStudentRecord");
    where CreateStudentRecord is the JFrame I want to launch. however this did not work.
    Can anyone help me find out how I can launch another JFrame when I click on a button within another JFrame.
    Thanks,
    Rahul.

    How about just using the following:
    private CreateStudentRecord csr;
    JButton b1 = new JButton("New Student Record");
    b1.addActionListener( new ActionListener() {
         public void actionPerformed(ActionEvent e){
              csr = new CreateStudentRecord().show();
    Rick

Maybe you are looking for

  • Indesign CS5.5 Problem layer with video with ipad and share video in a swf file on web

    Hi Everyone, I have two problems: I'm just working on my portfolio - I've a page with 3 layers (Cover, Video, base) I'd like that the "cover" layer hide part of the video when it's playing. With a preview as SWF file it's working fine but when i try

  • Filling PDF Form with XML data file

    Hello everybody. I need help. I created a form with LiveCycle Designer. I filled it and generated a XML data files. Now, I would like to fill the same blank PDF form with this XML data file. I know that I can do it with Adobe 8.0 but I must do it in

  • Cannot generate AWR/ADDM reports in Oracle 10g

    Hi, We are running 10.2.0.3.0 and troublshooted the performance problem for some queries. But when tried to run AWR/ADDM reports from OEM and got the following messages: Insufficient Data in Interval. For displaying data on this page, two historical

  • Menu audio won't play on first Chapters page

    My last project had 8 chapter or scene markers, six of which appear on the first Scene Selection page. The audio assigned to the menu on this page will not play even though the settings are identical to those on the main menu page and the second Scen

  • Why does my ipod automatically reset itself when im listening to it?!

    I will be clicking through songs and all of a sudden it just resets itself...and also if im fast forwarding through a song it also just resets it self.....everything it up to date so what is the problem 5th gen video   Windows XP