Olá Amigos, sou meio que novato em java e tenho um problema com JFrame e Dialog, o caso é o seguinte, estou desenvolvendo um sistema que tela uma tela class principal JFrame e alguns e uma class Dialog, e uma outra class Dados.
O meu problema é na tela principal eu instancio um objeto Dados e seto uma informação no atributo teste, só que minha duvida é como eu pego esta informação que foi setado na Intancia Dados no Dialog. e jogar num text.
Se não me engano o Dialog tem que herdar dados mais como faz isso no Dialog para que recupe os dados setados.
Agradeço
meu codigo:
Principal.java
package br.com.mtc.estatistica;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Dialog;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Principal extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private Dados dados;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Principal frame = new Principal();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Principal() {
setIconImage(Toolkit.getDefaultToolkit().getImage(Principal.class.getResource("/br/com/mtc/estatistica/iconejanela.png")));
setTitle("Estat\u00EDstica F\u00E1cil");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 653, 350);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(Principal.class.getResource("/br/com/mtc/estatistica/icone.png")));
lblNewLabel.setBounds(10, 15, 131, 270);
contentPane.add(lblNewLabel);
JLabel lblComeceMontandoSua = new JLabel("Comece montando sua tabela de distribui\u00E7\u00E3o de Frequ\u00EAncia");
lblComeceMontandoSua.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblComeceMontandoSua.setBounds(151, 15, 363, 30);
contentPane.add(lblComeceMontandoSua);
JButton btnAjuda = new JButton("Ajuda");
btnAjuda.setBackground(SystemColor.menu);
btnAjuda.setBounds(520, 17, 89, 23);
contentPane.add(btnAjuda);
JPanel panel = new JPanel();
panel.setForeground(SystemColor.window);
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Seus dados s\u00E3o:", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.windowText));
panel.setToolTipText("");
panel.setBounds(151, 221, 458, 64);
contentPane.add(panel);
panel.setLayout(null);
JRadioButton rdbtnUmaPopulao = new JRadioButton("Uma Popula\u00E7\u00E3o");
rdbtnUmaPopulao.setFont(rdbtnUmaPopulao.getFont().deriveFont(rdbtnUmaPopulao.getFont().getStyle() & ~Font.BOLD));
rdbtnUmaPopulao.setBounds(11, 21, 119, 23);
panel.add(rdbtnUmaPopulao);
JRadioButton rdbtnUmaAmostra = new JRadioButton("Uma Amostra");
rdbtnUmaAmostra.setFont(rdbtnUmaAmostra.getFont().deriveFont(rdbtnUmaAmostra.getFont().getStyle() & ~Font.BOLD));
rdbtnUmaAmostra.setBounds(132, 21, 115, 23);
panel.add(rdbtnUmaAmostra);
JButton btnIntervalos = new JButton("Intervalos");
btnIntervalos.setBackground(SystemColor.menu);
btnIntervalos.setBounds(337, 21, 104, 23);
panel.add(btnIntervalos);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(151, 68, 458, 142);
contentPane.add(scrollPane);
JTextArea txtrDasd = new JTextArea();
txtrDasd.setText("dasd");
scrollPane.setViewportView(txtrDasd);
JButton btnRol = new JButton("ROL");
btnRol.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Dados txto = new Dados();
txto.setTeste(txtrDasd.getText());
Dialog tabela = new Tabela();
tabela.setVisible(true);
}
});
btnRol.setBackground(SystemColor.menu);
btnRol.setBounds(253, 21, 74, 23);
panel.add(btnRol);
}
}
Tabela.java
package br.com.mtc.estatistica;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
public class Tabela extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private final JPanel contentPanel = new JPanel();
private JTextField caixa;
private Dados dados;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Tabela dialog = new Tabela();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public Tabela() {
super();
setModal(true);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
contentPanel.setBounds(0, 0, 434, 1);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel);
contentPanel.setLayout(null);
{
JPanel buttonPane = new JPanel();
buttonPane.setBounds(0, 228, 434, 33);
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
{
caixa = new JTextField();
caixa.setBounds(69, 36, 261, 48);
getContentPane().add(caixa);
caixa.setColumns(10);
// Pegar as informações na Instancia Dados que foi criada na pela principal, so que retorna um erro e não pega as informações
caixa.setText(dados.getTeste());
}
}
}
Dados.java
package br.com.mtc.estatistica;
public class Dados {
private String teste;
public Dados(){
}
public void setTeste(String texto){
this.teste = texto;
}
public String getTeste(){
return this.teste;
}
}