Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Pass info from JDialog to JFrame

$
0
0
Hi,
I'm having a bit of trouble trying to pass information from a JDialog back to a JFrame.The trouble is I open the dialog box input my data into a JTextField, and then I thought I could just pass back a getter method from the dialog box back to the JFrame and add to the JTextField? But this doesn't seem to work, I'm a bit stumped I Know I can use the JOptionPane technique,but I want to try a diferrent technique


JDialog code
package buildJDialog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BuildDialog extends JDialog implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel bPanel;
	private JLabel label1;
	private JTextField input1;
	private JButton button1;
	
	private String num1;
		
	public BuildDialog()
	{
		// TODO Auto-generated constructor stub
		super();
		setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
	
		bPanel = new JPanel();
		label1 = new JLabel( "Input A: " );
		input1 = new JTextField( "0", 3 );
		button1 = new JButton( "Add" );
				
		num1 = "0";
				
		bPanel.add( label1 );
		bPanel.add( input1 );
		bPanel.add( button1 );
						
		button1.addActionListener( this );
						
		add( bPanel );
		setSize( new Dimension( 200, 200 ) );
		setVisible( true );
	}

	@Override
	public void actionPerformed( ActionEvent e ) 
	{
		// TODO Auto-generated method stub
		Object object = e.getSource();
		
		if( object.equals( button1 ) )
		{
			num1 = input1.getText();
			dispose();
		}
		
	}
	
	public String getNum1()
	{
		return num1;		
	}	

}




JFrame code
package buildJDialog;

import javax.swing.*;
import java.awt.event.*;

public class InitDialog extends JFrame implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	private BuildDialog in;	
	private JPanel bPanel;
	private JButton button;
	private JTextField input1;
	
	
	public InitDialog() 
	{
		// TODO Auto-generated constructor stub
		super();
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		bPanel = new JPanel();
		button = new JButton( "Press Me" );
		input1 = new JTextField( "0", 5 );
		
		bPanel.add( button );
		bPanel.add( input1 );
				
		button.addActionListener( this );
				
		add( bPanel );
		setSize( 500, 500 );
		setVisible( true );
	}
	

	@Override
	public void actionPerformed( ActionEvent e )
	{
		// TODO Auto-generated method stub
		Object object = e.getSource();
		
		if( object.equals( button ) )
		{			
			in = new BuildDialog();
			input1.setText( in.getNum1() );						
		}		
		
	}
	
		
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		new InitDialog();
	}

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>