Java - pasar text field a label

   
Vista:
Imágen de perfil de Diego

pasar text field a label

Publicado por Diego (23 intervenciones) el 26/06/2014 09:18:52
tengo unos datos en una jtable y al momento de selecionarla me tiene que abrir otro j frame con los datos mostrandolo en labels el nombre apellido etc. este es mi codigo al momento de ejecutarlo m manda una excepcion

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

package practica13;

import Bean.PersonaBean;
import DAO.PersonaDao;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Diego
*/
public class ConsultasPersonasAvansada extends javax.swing.JFrame {

/**
* Creates new form ConsultasPersonasAvansada
*/

List<Integer> listaIds = new ArrayList<>();
public ConsultasPersonasAvansada() {
initComponents();
setLocationRelativeTo(this);

PersonaDao daoP = new PersonaDao();
List<PersonaBean> personas = daoP.obtenerPersonas();

DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
// for each
for (PersonaBean bean : personas) {

String[] fila = new String[4];
fila[0] = bean.getNombre();
fila[1] = bean.getApellidoP();
fila[2] = bean.getApellidoM();
fila[3] = bean.getEstado();
modelo.addRow(fila);

}
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"Nombres", "Apaterno", "Amaterno", "Estado"
}
) {
boolean[] canEdit = new boolean [] {
false, true, true, false
};

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);

jLabel1.setText("Buscar:");

jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField1KeyReleased(evt);
}
});

jButton1.setText("Limpiar Tabla");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jMenu1.setText("File");

jMenuItem2.setText("Consulta persona");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);

jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(18, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 586, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)
.addComponent(jButton1)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
limpiarTabla();


}

public void limpiarTabla() {
DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();

for (int i = modelo.getRowCount() - 1; i >= 0; i--) {
modelo.removeRow(i);

}
}

private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
limpiarTabla();
String cadena = jTextField1.getText();
PersonaDao daop = new PersonaDao();
List<PersonaBean> personas = daop.consultarPersonas(cadena);
DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();

for (PersonaBean bean : personas) {

String[] fila = new String[4];
fila[0] = bean.getNombre();
fila[1] = bean.getApellidoP();
fila[2] = bean.getApellidoM();
fila[3] = bean.getEstado();
modelo.addRow(fila);
listaIds.add(daop.obtenerIdPersona(bean));

}


}

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
ConsultaPersona cons = new ConsultaPersona();
cons.setVisible(true);
this.setVisible(false);
}

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int fila = jTable1.getSelectedRow();
int idPersona=listaIds.get(fila);
ConsultaPersona consul= new ConsultaPersona();
consul.setVisible(true);
PersonaDao daoP = new PersonaDao();
PersonaBean bean= daoP.consultarpersona(idPersona);
String cambio;
if (bean.getSexo()==1) {
cambio="M";

}else
cambio="H";


//
// ConsultaPersona.jLabel10.setText(jTable1.getValueAt(fila, 0).toString());
// ConsultaPersona.jLabel11.setText(jTable1.getValueAt(fila, 1).toString());
// ConsultaPersona.jLabel12.setText(jTable1.getValueAt(fila, 2).toString());
// ConsultaPersona.jLabel13.setText(cambio);
// ConsultaPersona.jLabel14.setText(jTable1.getValueAt(fila, 3).toString());
// ConsultaPersona.jLabel15.setText(bean.getTieneUsuario().getUsuario());
// ConsultaPersona.jLabel16.setText(bean.getTieneUsuario().getPassword());

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ConsultasPersonasAvansada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ConsultasPersonasAvansada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ConsultasPersonasAvansada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ConsultasPersonasAvansada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ConsultasPersonasAvansada().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder