Vamos então criar 2 métodos em FormCadastro: getResult() e getNome().

Então, o código do FormCadastro ficará assim:

package modulo1;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FormCadastro extends JDialog {
	
	private JButton butOK;
	private JButton butCancelar;
	private JTextField campoNome,campoEnd,campoTel,campoCid,campoCep;
	private JLabel textoNome,textoEnd,textoTel,textoCid,textoCep;

private boolean result = false;
	
	public FormCadastro(Frame frm) {
		super(frm, "Cadastro de cliente", Dialog.ModalityType.DOCUMENT_MODAL);

		textoNome = new JLabel("Nome:"); campoNome = new JTextField(15);
		textoEnd = new JLabel("Endereço:"); campoEnd = new JTextField(15);
		textoTel = new JLabel("Fone:"); campoTel = new JTextField(15);
		textoCid = new JLabel("Cidade:"); campoCid = new JTextField(15);
		textoCep = new JLabel("CEP:"); campoCep = new JTextField(15);
		
		butOK = new JButton("OK"); butCancelar = new JButton("Cancelar");
		
		setLayout(new GridLayout(3,2));
		add(textoNome); add(campoNome);
		add(textoEnd); add(campoEnd);
		add(textoTel); add(campoTel);
		add(textoCid); add(campoCid);
		add(textoCep); add(campoCep);
		
		add(butOK); add(butCancelar);
		
		butOK.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
				dispose();
				result = true;
			}
		});
		
		butCancelar.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
				dispose();
			}
		});
				
		pack();
	}	
	
	public String getNome() {
		return campoNome.getText();
	}
	
	public boolean getResult() {
		return result;
	}
}

E o código do ExemploLista ficará assim:

package modulo1;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExemploLista {

	private static JFrame frm;
	private static JList listbox;
	private static DefaultListModel listModel;
	
	/**
	 * @param args
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, 
IllegalAccessException
, UnsupportedLookAndFeelException { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); frm = new JFrame("Exemplo de Menus"); BorderLayout flow = new BorderLayout(); frm.setLayout(flow); frm.setVisible(true); // Cria uma barra de menu para o JFrame JMenuBar menuBar = new JMenuBar(); // Adiciona a barra de menu ao frame frm.setJMenuBar(menuBar); // Define e adiciona dois menus drop down na barra de menus JMenu cadMenu = new JMenu("Cadastro"); menuBar.add(cadMenu); JMenu ajuMenu = new JMenu("Ajuda"); menuBar.add(ajuMenu); // Cria e adiciona um item simples para o menu JMenuItem newAction = new JMenuItem("Novo cadastro"); cadMenu.add(newAction); JMenuItem edAction = new JMenuItem("Editar cadastro"); cadMenu.add(edAction); JMenuItem sairAction = new JMenuItem("Sair"); cadMenu. addSeparator(); cadMenu.add(sairAction); JMenuItem sobAction = new JMenuItem("Sobre"); ajuMenu.add(sobAction); JPanel topPanel = new JPanel(); topPanel.setLayout( new BorderLayout() ); frm.add( topPanel ); listModel = new DefaultListModel(); listModel.addElement("Fulano"); listModel.addElement("Beltrano"); listModel.addElement("Sicrano"); listbox = new JList( listModel ); topPanel.add( listbox, BorderLayout.CENTER ); frm.setSize(650,200); frm.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); newAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ FormCadastro form = new FormCadastro(frm); form.setVisible(true); if (form.getResult()) { listModel.addElement(form.getNome()); } } }); edAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ FormCadastro form = new FormCadastro(frm); form.setVisible(true); } }); sairAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }); sobAction.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ FormSobre sobreDlg = new FormSobre(frm); sobreDlg.setVisible(true); } }); } }
Copyright © 2014 AIEC.