Passing text from JFrame

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1358 Views - Last Post: 03 August 2012 - 09:54 AM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Passing text from JFrame

Posted 02 August 2012 - 04:49 PM

I have a JFrame with a textfield, and I'm trying to figure out how to pass the value in that field to another class so I can use it. This is way beyond the scope of what we've done in class, it's not even in my textbook. I've spent the last 5 hours or so reading up on it and trying to get it to work. Any advice would be appreciated.

Here is the text field code, and the method I wrote to return its' value:

 textIn  = new JTextField(20);
                window2.add(textIn);
                textIn.reshape(75, 50, 75, 30);


 protected String getValue()
    {
        return textIn.getText();
        
    }





I do have an enter button on this Frame, here is the Action Handler:
if (e.getSource() == btnEnter)
            {
               getValue();
               setVisible(false);
                
            }



And lastly, here is code from my main window where I am calling the value of the text field:

if(e.getSource() == btnCall)
			{
				windowIn = new InputWindow("How many minutes will you use?");
       
                                String minutesIn = windowIn.getValue();
                                int minutesOut = Integer.parseInt(minutesIn);
                                myPhone.makeCall(minutesOut);
                                
			}


I'm throwing an exception on this line
int minutesOut = Integer.parseInt(minutesIn);


Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

This post has been edited by synlight: 02 August 2012 - 04:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing text from JFrame

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Passing text from JFrame

Posted 02 August 2012 - 04:54 PM

The general concept seems right, although your button action handler doesn't really do anything. Getting the value without assigning it to something is pretty much useless.

Are you having some problems with your code? Errors? If so, please provide the details (exact error messages, copied and pasted) and the code necessary to repeat the error, if you think it'll help.
Was This Post Helpful? 1
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 02 August 2012 - 05:03 PM

I see your point about the button handler. I tried that before I made the get method.

I don't get an error in the IDE until after I run the program. I am taking the text field value and passing it to a method that calculates a cell phone bill. Here is the exact error I'm getting:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at JavaPhone$ButtonHandler.actionPerformed(JavaPhone.java:103)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


It traces back to line 6 below:

if(e.getSource() == btnCall)
			{
				windowIn = new InputWindow("How many minutes will you use?");
       
                                String minutesIn = windowIn.getValue();
                                int minutesOut = Integer.parseInt(minutesIn);
                                myPhone.makeCall(minutesOut);
                                
			}


Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Passing text from JFrame

Posted 02 August 2012 - 05:09 PM

This seems to say that:

Integer.parseInt( minutesIn ) throws a NumberFormatException,

because

minutesIn = "" (an empty String),

so

windowIn.getValue() is returning an empty String,

so

either there's nothing in the JTextField textIn, or the method getValue() is returning an empty string for some other reason.
Was This Post Helpful? 1
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 02 August 2012 - 05:22 PM

hmm.. that makes sense!! Now I have to figure out why it's returning an empty string.

When I run the program, I get the error when I press the "Call" Button, before I get a chance to enter my minutes. So... how would I separate that? I'm trying to puzzle through the logic.

if(e.getSource() == btnCall)
	{
	     windowIn = new InputWindow("How many minutes will you use?");
       
             String minutesIn = windowIn.getValue();
             int minutesOut = Integer.parseInt(minutesIn);
             myPhone.makeCall(minutesOut);
                                
        }



Is there a way to make the call function wait for the enter button to be pressed on the input window?

This post has been edited by synlight: 02 August 2012 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Passing text from JFrame

Posted 02 August 2012 - 05:34 PM

Ohhhhh. So the Call button presents a new input window to the user but doesn't wait for the user to provide input? The InputWindow() constructor should create the modal inputWindow instance, set it visible, and wait for the user to enter data and press the inputWindow's "OK" button (or similar). The "OK" button handler will dispose the inputWindow or set it invisible and either return the user's input value to the calling method or set the value in the program's Model (data handler). I prefer the latter, but if you haven't designed your program as MVC from the ground up, going back and redesigning and rewriting it to be MVC might be more than you're willing or able to do.

Simply creating a modal instance of inputWindow may solve your problems.
Was This Post Helpful? 2
  • +
  • -

#7 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 02 August 2012 - 05:45 PM

View PostGregBrannon, on 02 August 2012 - 07:34 PM, said:

Ohhhhh. So the Call button presents a new input window to the user but doesn't wait for the user to provide input? The InputWindow() constructor should create the modal inputWindow instance, set it visible, and wait for the user to enter data and press the inputWindow's "OK" button (or similar). The "OK" button handler will dispose the inputWindow or set it invisible and either return the user's input value to the calling method or set the value in the program's Model (data handler). I prefer the latter, but if you haven't designed your program as MVC from the ground up, going back and redesigning and rewriting it to be MVC might be more than you're willing or able to do.

Simply creating a modal instance of inputWindow may solve your problems.

Ah! That makes sense! I read about modal earlier, so I just need to look up the constructor and implement it.

I'm not above rewriting the program, but I don't have time. It's due monday and I still have a ton of functionality to add, plus write a user manual. I've been working on it for over 12 hours today as it is ugh. Thanks so much for your help!
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1405
  • View blog
  • Posts: 3,120
  • Joined: 05-April 11

Re: Passing text from JFrame

Posted 02 August 2012 - 06:00 PM

View Postsynlight, on 03 August 2012 - 12:45 AM, said:

View PostGregBrannon, on 02 August 2012 - 07:34 PM, said:

Ohhhhh. So the Call button presents a new input window to the user but doesn't wait for the user to provide input? The InputWindow() constructor should create the modal inputWindow instance, set it visible, and wait for the user to enter data and press the inputWindow's "OK" button (or similar). The "OK" button handler will dispose the inputWindow or set it invisible and either return the user's input value to the calling method or set the value in the program's Model (data handler). I prefer the latter, but if you haven't designed your program as MVC from the ground up, going back and redesigning and rewriting it to be MVC might be more than you're willing or able to do.

Simply creating a modal instance of inputWindow may solve your problems.

Ah! That makes sense! I read about modal earlier, so I just need to look up the constructor and implement it.

I'm not above rewriting the program, but I don't have time. It's due monday and I still have a ton of functionality to add, plus write a user manual. I've been working on it for over 12 hours today as it is ugh. Thanks so much for your help!


Sounds tough! Remember to take small breaks
Was This Post Helpful? 0
  • +
  • -

#9 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 02 August 2012 - 06:30 PM

Okay, I'm back at it this morning.

I couldn't figure out a simple way to make my JFrame modal, so I changed it to a JDialog Box. I am trying to teach myself, but I'm running out of time.

The dialog box works, but not the way I intended. I want the user to open the JavaPhone window, and click call. Then the InputWindow should open, and allow them to input how many minutes, then return those minutes to the ButtonHandler for the JavaPhone window call button.

I can't figure out how to set the owner of the JDialog to the JavaPhone frame. When I write JavaPhone, it doesn't recognize it. When I just leave it as "owner", I had to add setVisible(true), or the window doesn't show at all when I press Call, it just gives me a null String error.

SO, what I need to do is figure out how to make the JDialog visible when the user presses the Call button, take the input, return it, and go away.

Here is my JDialog class:



import javax.swing.JDialog; 

import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionEvent;



class InputWindow extends JDialog implements ActionListener
{
	
	private JPanel myPanel = null;
	private JLabel message;
    private JButton btnEnter, btnClear;
    private JTextField textIn;
  
    
    
    
    public InputWindow(String labelIn)
    {
       super();
       String question = labelIn;
       
       myPanel = new JPanel();
       getContentPane().add(myPanel);
       
      
       
       message = new JLabel(question);
                myPanel.add(message);
                message.reshape(10, 10, 200, 50);  
               
       textIn  = new JTextField(20);
                myPanel.add(textIn);
                textIn.reshape(75, 50, 75, 30);
                
       btnEnter = new JButton("Enter");
       btnEnter.addActionListener(this);
       myPanel.add(btnEnter);
		
		
       
       btnClear = new JButton("Clear");
       btnClear.addActionListener(this);
       myPanel.add(btnClear);
	            
		pack();
		setVisible(true);         
    
    }
    
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == btnEnter)
            {
       
               setVisible(false);
                
            }
            
            if (e.getSource() == btnClear)
            {
                textIn.setText(" ");
            }
        }
        
    
    protected String getValue()
    {
        return textIn.getText();
        
    }

	
}//END INPUT WINDOW CLASS



And here is the part of my JavaPhone class that calls the JDIalog:

if(e.getSource() == btnCall)
			{
				
				windowIn = new InputWindow("How many minutes will you use?");
                String minutesIn = windowIn.getValue();
                int minutes = Integer.parseInt(minutesIn);
                	
				myPhone.makeCall(minutes);
                                
			}

This post has been edited by synlight: 03 August 2012 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#10 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 03 August 2012 - 06:43 AM

bump to top because edit didn't do it.
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Passing text from JFrame

Posted 03 August 2012 - 07:19 AM

There is no way to make a JFrame modal - there's no such thing. But what you describe is a great use for JDialog. You can do everything with a JDialog that you can do with a JFrame, plus it has capabilities a JFrame does not, like being modal.

Quote

SO, what I need to do is figure out how to make the JDialog visible when the user presses the Call button, take the input, return it, and go away.

Doing this theoretically is too difficult. We've already wasted a bunch of time talking about something a JFrame can't do, because you didn't show you were trying to do it with a JFrame. Give us code we can run and provide more info about how you would like it to operate and how it's not meeting your requirements.
Was This Post Helpful? 0
  • +
  • -

#12 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 03 August 2012 - 07:25 AM

I'm sorry if I've wasted anyone's time. I try to post the code that seems relevant, to SAVE your time. Guess it backfired. I did mention in the OP that it was a JFrame, but I didn't include it in the code.

So.. here goes.

Here is my JavaPhone class

import javax.swing.*;
import javax.swing.JDialog;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.*;
//*****************************************
//*** FILE: JavaPhone.java                *
//***  PROG: 		  *
//**  PURP: Final Project - Cell Phone    *
//*****************************************


public class JavaPhone extends JFrame
{
    private JLabel labelDo, labelAsk;
    private JButton btnCall, btnBill, btnText;
    private JButton btnNav, btnWeb;
    private JTextField txtIn;
    private ButtonHandler bhandler;
    SmartPhone myPhone = new SmartPhone();
    InputWindow windowIn = new InputWindow("");
    BillWindow windowBill = new BillWindow("");
    /*
    **********************************
    *	CONTAINER
    **********************************
    */
	
    //constructor
    
    public JavaPhone()
    {
        super ("JavaPhone 1.0");
        
        Container cont = getContentPane();
        cont.setLayout(null);
		
        bhandler = new ButtonHandler();
		
        labelDo = new JLabel("JAVAPHONE");
        cont.add(labelDo);
        labelDo.reshape(112, 10, 200, 50);  
		
        btnCall = new JButton("Call");
		cont.add(btnCall);
		btnCall.reshape(85, 60, 120, 30);
		btnCall.addActionListener(bhandler);
                
        btnText = new JButton("Text Message");
		cont.add(btnText);
		btnText.reshape(85, 100, 120, 30);
		btnText.addActionListener(bhandler);
                
        btnNav = new JButton("GPS");
		cont.add(btnNav);
		btnNav.reshape(85, 140, 120, 30);
		btnNav.addActionListener(bhandler);
                
        btnWeb = new JButton("Internet");
		cont.add(btnWeb);
		btnWeb.reshape(85, 180, 120, 30);
		btnWeb.addActionListener(bhandler);
                
        btnBill = new JButton("View Bill");
		cont.add(btnBill);
		btnBill.reshape(85, 220, 120, 30);
		btnBill.addActionListener(bhandler);  
                
       
   
              
        setSize (303,403);
	show();
        
    }//END CONTAINER
    
    public static void main(String a[])
	{
		JavaPhone staticPhone = new JavaPhone();  
		staticPhone.addWindowListener
		    (new WindowAdapter()
		       {

				      public void windowClosing(WindowEvent e)
				         {System.exit(0);}
				}    );
	}
    
    
    class ButtonHandler implements ActionListener
	{
        
        
		public void actionPerformed(ActionEvent e)
		{
			
			
			if(e.getSource() == btnCall)
			{
				
				windowIn = new InputWindow("How many minutes will you use?");
                String minutesIn = windowIn.getValue();
                int minutes = Integer.parseInt(minutesIn);
                	
				myPhone.makeCall(minutes);
                                
			}
			
            if(e.getSource() == btnText)
			{
				myPhone.sendText();
			}
                        
            if(e.getSource() == btnBill)
			{
				
                 String total = myPhone.calcBill(myPhone.getMinutes(), myPhone.getTexts());
                 windowBill = new BillWindow(total);
			}
                        
            if(e.getSource() == btnNav)
			{
                 windowIn = new InputWindow("How many miles will you drive?");
                 int milesIn = Integer.parseInt(windowIn.getValue());
                 myPhone.useNav(milesIn);
			}
                        
            if(e.getSource() == btnWeb)
			{
				windowIn = new InputWindow("How many pages will you view?");
                int pagesIn = Integer.parseInt(windowIn.getValue());
                myPhone.useWeb(pagesIn);
			}
                }
                
        }//END BUTTON HANDLER		  
}//END JAVAPHONE








Here is the first Dialog, that isn't running properly



import javax.swing.JDialog; 
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;



class InputWindow extends JDialog implements ActionListener
{
	
	private static Frame owner;
	private JPanel myPanel = null;
	private JLabel message;
    private JButton btnEnter, btnClear;
    private JTextField textIn;
  
    
    
    
    public InputWindow(String labelIn)
    {
       super(owner, true);
       String question = labelIn;
       
       myPanel = new JPanel();
       getContentPane().add(myPanel);
       
      
       
       message = new JLabel(question);
                myPanel.add(message);
                message.reshape(10, 10, 200, 50);  
               
       textIn  = new JTextField(20);
                myPanel.add(textIn);
                textIn.reshape(75, 50, 75, 30);
                
       btnEnter = new JButton("Enter");
       btnEnter.addActionListener(this);
       myPanel.add(btnEnter);
		
		
       
       btnClear = new JButton("Clear");
       btnClear.addActionListener(this);
       myPanel.add(btnClear);
	            
		pack();
		setVisible(true);         
    
    }
    
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == btnEnter)
            {
       
               setVisible(false);
                
            }
            
            if (e.getSource() == btnClear)
            {
                textIn.setText(" ");
            }
        }
        
    
    protected String getValue()
    {
        return textIn.getText();
        
    }

	
}//END INPUT WINDOW CLASS


Here is the second dialog, runs properly but has no text to return

import javax.swing.JDialog; 
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;


class BillWindow extends JDialog implements ActionListener
{
  private JPanel myPanel = null;
  private JButton btnOK;
  private JLabel message, bill;
  NumberFormat billFormat = NumberFormat.getCurrencyInstance();
  
  public BillWindow (String billIn)
  {
      
      super();
      String total = billIn;
      
      
      myPanel = new JPanel();
      getContentPane().add(myPanel);
      
      message = new JLabel("Your total bill is:");
      myPanel.add(message);
      
      
      bill = new JLabel(total);
      myPanel.add(bill);
      
      btnOK = new JButton("OK");
	btnOK.addActionListener(this);
	myPanel.add(btnOK);
        
      pack();
      
      setVisible(true);  
  }
    
   public void actionPerformed(ActionEvent e) 
   {
	if(  e.getSource()==btnOK) 
        {
	    setVisible(false);
	}
	
    }
   
   
  

}//END DIALOG



Here are the DumbPhone and SamrtPhone classes

import java.text.NumberFormat;
import java.util.Random;

public class DumbPhone
{
    protected double bill;
    protected int minutes, texts;
    
    
    public DumbPhone() //constructor
	{
		
                bill = 0;
		minutes = 0;
		texts = 0;
	}
	
	public void makeCall(int minutesIn)
	{

		int callTime = minutesIn;
		minutes += callTime;
	}
	
	public void sendText()
	{
		texts += 1;
	}
	
	public String calcBill(int minutes, int texts)
	{
		
		double minTotal = 0, textTotal = 0;
		int minOver = 0;
        String bill;
                
		
		if (minutes < 100)
		{
			minTotal = minutes * .20;
		}
		else
		{
			minOver = minutes - 100;
		}
		
		textTotal = texts*.25;
		
		double billTotal = minTotal + textTotal;
                
                NumberFormat billFormat = NumberFormat.getCurrencyInstance();
                bill = billFormat.format(billTotal);
                
		return bill;
		
	}
	
	public double getBill()
	{
		return bill;
	}
	
	public int getMinutes()
	{
		return minutes;
	}
	
	public int getTexts()
	{
		return texts;
	}
	
	public int getRandom()
	{
		Random generator = new Random();
		int num = generator.nextInt();
		return num;
	}
	
    
}//END DUMBPHONE


class SmartPhone extends DumbPhone
{
    protected int data, web, nav;
    
    public SmartPhone ()
	{
		super();
		this.data = 0;
		this.web = 0;
		this.nav = 0;
	}
    
    public void useNav(int milesIn)
	{
		int miles = milesIn;
		nav += miles;
	}
   
    public void useWeb(int pagesIn)
	{
		int pages = pagesIn;
		web += pages;
	}
    
    public double calcDataCost()
	{
		double dataCost = 30;
		data = web + nav;
		if (data > 10)
		{
			double dataOver = data - 10;
			dataCost = 30 + (dataOver * 10);
		}
		
		return dataCost;
	}
    
    public int getWeb()
	{
		return web;
	}
	
	public int getNav()
	{
		return nav;
	}
	
	public int getData()
	{
		return data;
	}
}//END SMARTPHONE



Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1405
  • View blog
  • Posts: 3,120
  • Joined: 05-April 11

Re: Passing text from JFrame

Posted 03 August 2012 - 07:38 AM

Don't add a static Frame variable to your dialog!
Instead modify your constructor to take a parent/owner

public InputWindow(Frame parent, String labelIn)



When you call the constructor, you'll have to notice the ButtonHandler is an inner class and you shouldn't pass the ButtonHandler as the parent (by using 'this')
To get the instance of the outer class use JavaPhone.this
windowIn = new InputWindow(JavaPhone.this, "How many minutes will you use?");



Now just call the appropriate super constructor of JDialog that takes a parent and modal value :)

This post has been edited by CasiOo: 03 August 2012 - 07:39 AM

Was This Post Helpful? 1
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Passing text from JFrame

Posted 03 August 2012 - 07:52 AM

So what's not working as you'd like?

I can make a 30-minute call, view the bill, and it's $6.00. I make another 30-minute call, view the bill, and it goes to $12.00. Seems to work fine.

I did notice that the bill window shows up before it should showing a blank total bill, behind the JavaPhone's first menu before there have been any calls, so that needs to be fixed. Close that and proceed as I mentioned above, and that part seems okay.

Let me know what you need help with.

Edit: The reason you're getting an empty BillWindow is because you create the instance of windowBill in your JavaPhone instance variable declarations (lines 21 - 23). Do you need those instances created there?

If you modify the instance variables at the top to:
    SmartPhone myPhone;  // = new SmartPhone();
    InputWindow windowIn; //  = new InputWindow("");
    BillWindow windowBill; // = new BillWindow("");


And then create an instance of myPhone in the JavaPhone constructor:

myPhone = new SmartPhone();

before making it visible, you should be fine.

By the way, JFrame.show() is deprecated. Use JFrame.setVisible( true );

This post has been edited by GregBrannon: 03 August 2012 - 08:01 AM

Was This Post Helpful? 1
  • +
  • -

#15 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Passing text from JFrame

Posted 03 August 2012 - 07:59 AM

View PostCasiOo, on 03 August 2012 - 09:38 AM, said:

Don't add a static Frame variable to your dialog!
Instead modify your constructor to take a parent/owner




Now just call the appropriate super constructor of JDialog that takes a parent and modal value :)


Thank you!!!

So.. I did what you said. I modified the constructor:
 public InputWindow(Frame parent, String labelIn)
 {
       super(parent, true);
       String question = labelIn;
       



And I modified the calls from JavaPhone

windowIn = new InputWindow(JavaPhone.this,"How many minutes will you use?");

windowIn = new InputWindow(JavaPhone.this,"How many miles will you drive?");

(etc.)

It runs, no more empty string, and I understand about 90% of what I just did (which is awesome LOL). THANK YOU SO MUCH! I was very close to scrapping the window and using a random number generator.. I'm so glad I didn't.

The JDialog is still popping up when I open the JavaPhone though, before I press any buttons. The JavaPhone GUI won't open until I close the original JDialog. I've combed over the code and I don't understand why. I even compared the 2 JDialogs in the program line for line, and I can't see anything different (other than components). I don't understand why one shows up when it shouldn't, and the other one works fine?

View PostGregBrannon, on 03 August 2012 - 09:52 AM, said:

So what's not working as you'd like?

I can make a 30-minute call, view the bill, and it's $6.00. I make another 30-minute call, view the bill, and it goes to $12.00. Seems to work fine.

I did notice that the bill window shows up before it should showing a blank total bill, behind the JavaPhone's first menu before there have been any calls, so that needs to be fixed. Close that and proceed as I mentioned above, and that part seems okay.

Let me know what you need help with.


Thank you for looking! The JDialog shouldn't be opening when I run JavaPhone, until I press a button. On my computer, the Input Window runs before the JavaPhone, and JavaPhone won't run unless I close the Input window.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2