Results 1 to 2 of 2

Thread: return the value of a textbox on an instance of a form

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    return the value of a textbox on an instance of a form

    I am currently learning the basics on JAVA GUIs.

    And I made a Class that extends the javax.swing.JFrame Class.
    The name of that class is MAINCLASS. On the form i made a swing menu.

    I made a 2nd class that also extends the javax.swing.JFrame Class. This class has an input box and is called INPUTCLASS.

    When the user hits the menu on the Mainclass, then it creates an instance of the inputclass and Shows it. The user needs to fill in the data in the textbox and then hit ok.

    But now, how should I return the inputvalue to the MainClass?

    Thank you in advance.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    dunno if this helps
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class frmmain extends JFrame{
        JMenuBar mbar=new JMenuBar();
        JMenu mnufile=new JMenu("File");
        JLabel lbl=new JLabel("Nothing");
        public frmmain(){
            super("Main");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400,300);
            getContentPane().setLayout(new FlowLayout());
    
            JMenuItem mnunew=new JMenuItem("New");
            mnunew.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    mnunew_click(e);
                }
            });
            mnufile.add(mnunew);
            mbar.add(mnufile);
            setJMenuBar(mbar);
    
            getContentPane().add(lbl);
        }
        void mnunew_click(ActionEvent e){
            frmsome f=new frmsome(this,true);
            f.show();
    
            lbl.setText(f.getInput());
        }
    
        public static void main(String[] args){
            new frmmain().show();
        }
    }
    
    class frmsome extends JDialog{
        JLabel lbl=new JLabel("Something");
        JTextField txt=new JTextField(10);
        public frmsome(JFrame f,boolean modal){
            super(f,"Some",modal);
            setSize(300,200);
            getContentPane().setLayout(new FlowLayout());
    
            getContentPane().add(lbl);
            getContentPane().add(txt);
        }
        public String getInput(){
            return txt.getText();
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width