Eu preciso adicionar os valores de uma JTable para vários TextFields de uma JDialog.
Como eu faço para passar esses valores?
Eu preciso colocar no construtor?
Meu código da JFrame com o JTable:
import java.io.IOException;
import java.sql.SQLException;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Main extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel painelBotao = new JPanel();
private JTable tabela = new JTable();
private JButton botaoIncluir = new JButton("Incluir");
private JButton botaoDetalhe = new JButton("Detalhes");
private Container tela = getContentPane();
public static void main(String[] args) throws SQLException, IOException {
JFrame tela = new Main();
tela.setVisible(true);
}
Main() throws SQLException {
super("InfraBridge Config Manager");
initialize();
}
private void initialize() throws SQLException {
setSize(900, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
tela.setLayout(new BorderLayout());
tela.add(new JScrollPane(getTabela()), BorderLayout.NORTH);
tela.add(getPainelBotao(), BorderLayout.SOUTH);
setResizable(false);
}
private JTable getTabela() throws SQLException {
tabela.setModel(new ModeloTabela());
return tabela;
}
private JButton getBotaoIncluir() {
botaoIncluir.addActionListener(this);
return botaoIncluir;
}
private JButton getBotaoDetalhe() {
botaoDetalhe.addActionListener(this);
return botaoDetalhe;
}
private JPanel getPainelBotao() {
painelBotao.add(getBotaoIncluir());
painelBotao.add(getBotaoDetalhe());
return painelBotao;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==botaoIncluir){
TelaIncluir incluir = new TelaIncluir(this);
incluir.setVisible(true);
}
if(e.getSource()==botaoDetalhe){
TelaAltDel ad = new TelaAltDel(this);
ad.setVisible(true);
}
}
}
Meu código do JDialog:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TelaAltDel extends JDialog {
private static final long serialVersionUID = 1L;
private JPanel painelBotao = new JPanel();
private JPanel painelTxtLabel = new JPanel(new GridLayout(7,2,5,5));
private JLabel labelIdComponente = new JLabel("ID Componente");
private JLabel labelComponente = new JLabel("Componente");
private JLabel labelServidorOrigem = new JLabel("Servidor de Origem");
private JLabel labelServidorDestino = new JLabel("Servidor de Destino");
private JLabel labelAliasComponente = new JLabel("Alias do Componente");
private JLabel labelOrdem1 = new JLabel("Ordem de Origem");
private JLabel labelOrdem2 = new JLabel("Ordem de Destino");
private JTextField txtfIdComponente = new JTextField();
private JTextField txtfComponente = new JTextField();
private JTextField txtfServidorOrigem = new JTextField();
private JTextField txtfServidorDestino = new JTextField();
private JTextField txtfAliasComponente = new JTextField();
private JTextField txtfOrdem1 = new JTextField();
private JTextField txtfOrdem2 = new JTextField();
private JButton botaoAlterar = new JButton("Alterar");
private JButton botaoDeletar = new JButton("Deletar");
private Container tela = getContentPane();
public TelaAltDel(JFrame parent) {
super(parent, "InfraBridge Config Manager - Alterar/Deletar", true);
initialize();
}
private void initialize() {
txtfIdComponente.setEditable(false);
setSize(350, 300);
setLocationRelativeTo(null);
tela.add(getPainelTxtLabel());
tela.add(getPainelBotao(), BorderLayout.SOUTH);
setResizable(false);
}
private JButton getBotaoDeletar() {
botaoDeletar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "You clicked the button");
}
});
return botaoDeletar;
}
private JButton getBotaoAlterar(){
return botaoAlterar;
}
private JPanel getPainelBotao() {
painelBotao.add(getBotaoDeletar());
painelBotao.add(getBotaoAlterar());
return painelBotao;
}
private JPanel getPainelTxtLabel(){
painelTxtLabel.add(labelIdComponente); painelTxtLabel.add(txtfIdComponente);
painelTxtLabel.add(labelComponente); painelTxtLabel.add(txtfComponente);
painelTxtLabel.add(labelServidorOrigem); painelTxtLabel.add(txtfServidorOrigem);
painelTxtLabel.add(labelServidorDestino); painelTxtLabel.add(txtfServidorDestino);
painelTxtLabel.add(labelAliasComponente); painelTxtLabel.add(txtfAliasComponente);
painelTxtLabel.add(labelOrdem1); painelTxtLabel.add(txtfOrdem1);
painelTxtLabel.add(labelOrdem2); painelTxtLabel.add(txtfOrdem2);
return painelTxtLabel;
}
}