Results 1 to 14 of 14
  1. #1
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default How would I open a program from a single button of another program. Help...

    Experts, here's my program... It's running well now. But I just want to expand it. I want to have another program(on another frame), but I want to put the button of it on this program below to open it(when i click it). What i mean is I want to have a button that open another program, because i want to open my Cool3.java by clicking a button on the below program. Can you help me how to do that?

    Thanks...


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Cool2 extends JFrame implements ActionListener{
        JLabel heading = new JLabel("Given:");
        JLabel size = new JLabel("Sam Size:  ");
        JLabel variance = new JLabel("Variance: ");
        JLabel mean = new JLabel("Sam Mean");
        JLabel confidence = new JLabel("Confidence Interval:  ");
        JLabel output = new JLabel("Output:");
        
        JTextField inSize = new JTextField(7);
        JTextField inVar = new JTextField(7);
        JTextField inMean = new JTextField(7);
        JTextField inSize2 = new JTextField(7);
        JTextField inVar2 = new JTextField(7);
        JTextField inMean2 = new JTextField(7);
        JTextField inConf = new JTextField(7);
        JTextField outPut = new JTextField (7);
        JTextField outPut2 = new JTextField (7);
        
        JButton doit = new JButton("Do it!");
        
        JPanel hedPanel = new JPanel();
        JPanel sizPanel = new JPanel();
        JPanel varPanel = new JPanel();
        JPanel meaPanel = new JPanel();
        JPanel conPanel = new JPanel();
        JPanel outPanel = new JPanel();
        JPanel butPanel = new JPanel();
        
        double sam; // input: total calories per serving 
        double var; // input: grams of fat per serving 
        double mea; // result: percent of calories from fat
        double con;
        double out;
        double sam2;
        double var2;
        double mea2;
        double out2;
        
        public Cool2(){
            setTitle("calories from Fat");
            outPut.setEditable(false);
            setLayout(new FlowLayout());
            
            //Add component to Panels
            hedPanel.add(heading);
            sizPanel.add(size);
            sizPanel.add(inSize);
            sizPanel.add(inSize2);
            varPanel.add(variance);
            varPanel.add(inVar);
            varPanel.add(inVar2);
            meaPanel.add(mean);
            meaPanel.add(inMean);
            meaPanel.add(inMean2);
            conPanel.add(confidence);
            conPanel.add(inConf);
            outPanel.add(output);
            outPanel.add(outPut);
            outPanel.add(outPut2);
            butPanel.add(doit);
            
            // Add Panels to Frame
            add(hedPanel);
            add(sizPanel);
            add(varPanel);
            add(meaPanel);
            add(conPanel);
            add(outPanel);
            add(butPanel);
            
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            doit.addActionListener(this);
        }
        
        // The application
        public void ConfInt(){
            out = (mea-mea2) - con*Math.sqrt(Math.pow(var,2)/sam + Math.pow(var2,2)/sam2);
        }
        public void ConfInt2(){
            out2 = (mea-mea2) + con*Math.sqrt(Math.pow(var,2)/sam + Math.pow(var2,2)/sam2);
        }
        public void actionPerformed(ActionEvent evt){
            String userIn;
            String userIn2;
            userIn = inSize.getText();
            sam = Double.parseDouble(userIn);
      
            userIn2 = inSize2.getText();
            sam2 = Double.parseDouble(userIn2);
            
            userIn = inVar.getText();
            var = Double.parseDouble(userIn);
            
            userIn2 = inVar2.getText();
            var2 = Double.parseDouble(userIn2);
            
            userIn = inMean.getText();
            mea = Double.parseDouble(userIn);
            
            userIn2 = inMean2.getText();
            mea2 = Double.parseDouble(userIn2);
            
            userIn = inConf.getText();
            con = Double.parseDouble(userIn);
            ConfInt();
            ConfInt2();
            
            outPut.setText((out+ "    ").substring(0,6));
            repaint();
            
            outPut2.setText((out2+ "    ").substring(0,6));
            repaint();
        }
        
        public static void main (String [] args){
            Cool2 fatApp = new Cool2();
            fatApp.setSize(400, 250);
            fatApp.setResizable(false);
            fatApp.setVisible(true);
        }
        
    }
    Last edited by decgaid06; 03-19-2011 at 03:44 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Make the action listener for a button call the constructor of the new program(which should open a frame).

  3. #3
    Bmorebob is offline Member
    Join Date
    Mar 2011
    Location
    Manassas, VA
    Posts
    14
    Rep Power
    0

    Default

    sunde886 is right.

    public void actionPerformed(ActionEvent e){
    new Cool3().setVisible(true);
    }

  4. #4
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Yeah sunde886 is right, and tried his suggestion but just don't know what command I'll put on it... Thanks to you Bmorebob... I'll try that.

  5. #5
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default How to make a button specific only on a given action

    Hello experts, I'm having problem still on the two buttons I made, because it has the same function, I want the other one will function as "do it button" and the other one be "open button" for the other frame... I don't know how..
    heres the code
    Java Code:
            doit.addActionListener(this);
            open.addActionListener(this);
        }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If you are adding the same listener to both buttons then you need an if statement to determine which button was clicked.

  7. #7
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    sample code please... sorry I'm new to programming...

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What is the parameter to the actionPerformed method. Perhaps that class has what you need.

    Do and try and work things out for yourself before asking to be spoonfed code.

  9. #9
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    ok... thnks...

  10. #10
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Can you help us check the error of this program, the button "solve it" doesn't work...

    Thanks..

    Java Code:
    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
      
    public class Regression extends JFrame implements ActionListener
    {
      JLabel heading  = new JLabel("Given");
      JLabel x1 = new JLabel("sumX:");
      JLabel y = new JLabel("sumY:");
      JLabel x2 = new JLabel("sumX2:");
      JLabel xy = new JLabel("sumXY:");
      JLabel N = new JLabel("number of data:");
      JLabel reg = new JLabel("Y = ");
      JLabel plus = new JLabel("+");
      JLabel x = new JLabel("X");
      
      JButton but = new JButton("Solve Y");
      
      JTextField inx1 = new JTextField(7);
      JTextField iny = new JTextField(7);
      JTextField inx2 = new JTextField(7);
      JTextField inxy = new JTextField(7); 
      JTextField inN = new JTextField(7); 
      JTextField outreg = new JTextField(7); 
      JTextField outplus = new JTextField(7); 
      
      JPanel hedPanel   = new JPanel();
      JPanel x1Panel   = new JPanel();
      JPanel x2Panel   = new JPanel();
      JPanel yPanel   = new JPanel();   
      JPanel xyPanel   = new JPanel();  
      JPanel NPanel   = new JPanel();  
      JPanel regPanel   = new JPanel();  
      JPanel plusPanel   = new JPanel();   
      JPanel butPanel = new JPanel();
      
      double xx1; 
      double xxyy; 
      double yy; 
      double xx2; 
      double NN; 
      double reeg; 
      double pluss; 
      
      public Regression()  
      { 
        setTitle( "Regression Equation" );
        setLayout( new FlowLayout() ); 
       
        // Add components to Panels
        hedPanel.add( heading );
        x1Panel.add(x1);
        x1Panel.add(inx1);
        x2Panel.add(x2);
        x2Panel.add(inx2);
        yPanel.add(y);
        yPanel.add(iny);
        xyPanel.add(xy);
        xyPanel.add(inxy);
        NPanel.add(N);
        NPanel.add(inN);
        regPanel.add(reg);
        regPanel.add(outreg);
        regPanel.add(plus);
        regPanel.add(outplus);
        regPanel.add(x);
        butPanel.add(but);
        
        //frame
        add(hedPanel);
        add(x1Panel);
        add(x2Panel);
        add(yPanel);
        add(xyPanel);
        add(NPanel);
        add(regPanel);
        add(butPanel);
        
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
        but.addActionListener( this );   
        outreg.setEditable( false );
        outplus.setEditable( false );   
      }
      
      // The application
      public void calcreg( )
       {
        reeg = ((NN*xxyy)-(xx1*yy))/((NN*xx2)-Math.pow(xx1,2));
      }
        public void calcplus( ) 
      {
        pluss = (yy/NN)-(((NN*xxyy)-(xx1*yy)/(NN*xx2)-Math.pow(xx1,2))*(xx1/NN));
      }
      
      public void actionPerformed( ActionEvent evt) {
        String userIn ;
        userIn    = inx1.getText()  ;
        xx1  = Double.parseDouble( userIn ) ;
      
        userIn    = inx2.getText()  ;
        xx2  = Double.parseDouble( userIn ) ;
        
        userIn    = inxy.getText()  ;
        xxyy  = Double.parseDouble( userIn ) ;
      
        userIn    = iny.getText()  ;
        yy  = Double.parseDouble( userIn ) ;
        
        userIn    = inN.getText()  ;
        NN  = Double.parseDouble( userIn ) ;
        
        userIn    = outreg.getText()  ;
        reeg  = Double.parseDouble( userIn ) ;
        
        userIn    = outplus.getText()  ;
        pluss  = Double.parseDouble( userIn ) ;
        
         
        calcreg() ;
        calcplus() ;
      
        outreg.setText( (reeg+"      ").substring(0,6) );
        repaint();
        
        outplus.setText( (pluss+"      ").substring(0,6) );
        repaint();                 
      }
      
      public static void main ( String[] args ){
        Regression fatApp  = new Regression() ; 
        fatApp.setSize( 300, 280 );  
        fatApp.setResizable( true );
        fatApp.setVisible( true );        
      }
    }

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Explain "doesn't work".

  12. #12
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    What I mean is, everytime, I enter a given number... And press the button "Solve it" nothing happen, but what does this error mean, It just pop out in the output box of net beans..


    Java Code:
    init:
    deps-jar:
    Compiling 1 source file to D:\usersfiles\JavaLibrary1\build\classes
    compile-single:
    run-single:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
            at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
            at java.lang.Double.parseDouble(Double.java:510)
            at Regression.actionPerformed(Regression.java:113)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6041)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5806)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4413)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2440)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

  13. #13
    decgaid06 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Thanks for your help r035198,

    please try running it...

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by decgaid06 View Post
    What I mean is, everytime, I enter a given number... And press the button "Solve it" nothing happen, but what does this error mean, It just pop out in the output box of net beans..


    Java Code:
    init:
    deps-jar:
    Compiling 1 source file to D:\usersfiles\JavaLibrary1\build\classes
    compile-single:
    run-single:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
            at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
            at java.lang.Double.parseDouble(Double.java:510)
            at Regression.actionPerformed(Regression.java:113)
            at ...
    Read the message closely, it contains " at Regression.actionPerformed(Regression.java:113)" which is a line in your code.
    It says the problem at that line is "java.lang.NumberFormatException: empty String"
    Try to use that information to find out what the problem is.

Similar Threads

  1. JSP program to handle submit button
    By gurpreet.singh in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 03-09-2011, 05:46 AM
  2. Replies: 7
    Last Post: 03-31-2009, 01:29 PM
  3. [SOLVED] Making a program to open a file (.exe)
    By linux1man in forum New To Java
    Replies: 9
    Last Post: 12-23-2008, 12:27 AM
  4. i want to open a document from my program
    By akinpam in forum New To Java
    Replies: 1
    Last Post: 12-21-2008, 06:09 PM
  5. [SOLVED] open a program full size
    By keffie91 in forum Advanced Java
    Replies: 2
    Last Post: 10-05-2008, 10:26 AM

Posting Permissions

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