elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Consulta sobre código
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: Consulta sobre código  (Leído 8,329 veces)
SebastianJava

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Re: Consulta sobre código
« Respuesta #10 en: 28 Octubre 2013, 23:49 pm »

como hago eso amigo?


En línea

Mitsu

Desconectado Desconectado

Mensajes: 259



Ver Perfil WWW
Re: Consulta sobre código
« Respuesta #11 en: 28 Octubre 2013, 23:58 pm »

Tienes que colocar el code entre:

[ code==java]

[ / code]

Sin espacios.


En línea

SebastianJava

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Re: Consulta sobre código
« Respuesta #12 en: 29 Octubre 2013, 00:15 am »

te debo tener loco con tanta pregunta, discullpame 

Te explico de forma forma, estoy intentando hacer un CRUD, para lo cual, estoy tratando de insertar un JTABLE, para que me muestre los datos ingresados, un boton ingresar, modificar, consultar, eliminar, y limpiar, cuando inserte JTABLE, se me borraron los campos de nombre, direccion, etc... grcias.

Código
  1. package menu;
  2.  
  3. import javax.swing.JTextField;
  4. import javax.swing.JButton;
  5. import javax.swing.JPanel;
  6.  
  7. import java.awt.Color;
  8. import java.awt.FlowLayout;
  9. import java.awt.GridLayout;
  10.  
  11. import javax.swing.JComboBox;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14.  
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTable;
  17. import javax.swing.WindowConstants;
  18. import javax.swing.JOptionPane; // de aca en adelante para asignar accion al boton
  19. import javax.swing.JButton;
  20.  
  21.  
  22. import javax.swing.event.TableModelListener;
  23. import javax.swing.table.DefaultTableModel;
  24. import javax.swing.table.TableModel;
  25.  
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.util.Scanner;
  29. import java.util.Vector;
  30.  
  31.  
  32.  
  33. public class MenuPrincipal{
  34.  
  35. private JFrame frame;
  36. private JPanel panelSuperior;
  37. private JPanel panelInferior;
  38. private JPanel panelBoton;
  39.  
  40. private JLabel titulo;
  41.  
  42. private JLabel JLabel1;
  43. private JTextField JText1;
  44. private JLabel JLabel2;
  45. private JTextField JText2;
  46. private JLabel JLabel3;
  47. private JTextField JText3;
  48. private JLabel JLabel4;
  49. private JTextField JText4;
  50. private JLabel JLabel5;
  51. private JTextField JText5;
  52. private JLabel JLabel6;
  53. private JComboBox combo;
  54.  
  55. private JButton btnregistrar;
  56. private JButton btnconsultar;
  57. private JButton btnborrar;
  58. private JButton btnmodificar;
  59.  
  60. private JButton btnlimpiar;
  61. private JScrollPane jScrollPane;
  62. private JTable tabla;
  63.  
  64. private void itializeComponent()
  65.    {
  66.                  jsp1 = new JScrollPane();
  67.                  int nombreColumnas;
  68.  
  69.  
  70. modelo1 = new DefaultTableModel(null, nombreColumnas);
  71.  
  72.                  tabla1 = new JTable(modelo1);
  73.  
  74.                  jsp1.setViewportView(tabla1);
  75.    }
  76.  
  77. public MenuPrincipal() {
  78. construyePanelSuperior();
  79. construyePanelInferior();
  80. construyePanelBoton();
  81. construyeVentana();
  82.  
  83.  
  84.  
  85.  
  86. }
  87.  
  88.    private DefaultTableModel modelo1;
  89.    private JTable tabla1;
  90.    JScrollPane jsp1;
  91.  
  92.  
  93. public void construyePanelSuperior(){
  94.  
  95. panelSuperior = new JPanel();
  96. panelSuperior.setLayout(new FlowLayout());
  97. panelSuperior.setBackground(Color.LIGHT_GRAY);
  98.  
  99. titulo = new JLabel("Ingreso de Datos");
  100. panelSuperior.add(titulo);
  101.  
  102. }
  103.  
  104. public void construyePanelInferior() {
  105. panelInferior = new JPanel();
  106. panelInferior.setLayout(new GridLayout(3, 8, 12, 30)); // FILAS COLUMNAS
  107. // LARGO ANCHO
  108. JLabel1 = new JLabel("      Rut : ");
  109. JText1 = new JTextField(10);
  110.  
  111. JLabel2 = new JLabel("      Nombre : ");
  112. JText2 = new JTextField(10);
  113.  
  114. JLabel3 = new JLabel("      Apellido : ");
  115. JText3 = new JTextField(10);
  116.  
  117. JLabel4 = new JLabel("      Dirección : ");
  118. JText4 = new JTextField(10);
  119.  
  120. JLabel5 = new JLabel("      Teléfono : ");
  121. JText5 = new JTextField(10);
  122.  
  123. JLabel6 = new JLabel("      Ciudad  :");
  124. combo = new JComboBox();
  125.  
  126. combo.addItem("Valparaiso");// AGREGAR OPCIONES COMBO
  127. combo.addItem("Viña del Mar");
  128. combo.addItem("Villa Alemana");
  129. combo.addItem("Santiago");
  130. combo.addItem("Pto. Montt");
  131.  
  132. panelInferior.add(JLabel1);
  133. panelInferior.add(JText1);
  134.  
  135. panelInferior.add(JLabel2);
  136. panelInferior.add(JText2);
  137.  
  138. panelInferior.add(JLabel3);
  139. panelInferior.add(JText3);
  140.  
  141. panelInferior.add(JLabel4);
  142. panelInferior.add(JText4);
  143.  
  144. panelInferior.add(JLabel5);
  145. panelInferior.add(JText5);
  146.  
  147. panelInferior.add(JLabel6);
  148. panelInferior.add(combo);
  149. }
  150.  
  151. public void construyePanelBoton() {
  152. // se da formato a panel
  153. panelBoton = new JPanel();
  154. panelBoton.setLayout(new FlowLayout(FlowLayout.CENTER));
  155.  
  156. // se le da accion al boton registrar
  157. btnregistrar = new JButton("Registrar");
  158. btnregistrar.addActionListener(new ActionListener(){
  159.  
  160. public void actionPerformed(ActionEvent ae){
  161. JOptionPane.showMessageDialog(null,"Usuario Registrado");
  162.  
  163. }
  164. });
  165.  
  166. // se le da accion al boton borrar
  167. btnborrar = new JButton("Borrar");
  168. btnborrar.addActionListener(new ActionListener(){
  169. public void actionPerformed(ActionEvent ae){
  170. JOptionPane.showMessageDialog(null,"Usuario Eliminado");
  171. }
  172. });
  173.  
  174.  
  175. // se le da accion al boton modificar
  176. btnmodificar = new JButton("Modificar");
  177. btnmodificar.addActionListener(new ActionListener(){
  178. public void actionPerformed(ActionEvent ae){
  179. JOptionPane.showMessageDialog(null,"Usuario Modificado");
  180. }
  181. });
  182.  
  183.  
  184. // se le da accion al boton consultar
  185. btnconsultar = new JButton("Consultar");
  186. btnconsultar.addActionListener(new ActionListener(){
  187. public void actionPerformed(ActionEvent ae){
  188. JOptionPane.showMessageDialog(null,"Consulta Exitosa");
  189. }
  190. });
  191.  
  192.  
  193. // se le da accion al boton Limpiar OK!!!!!!!!!!!!!!!!!!!!!!!!!
  194. btnlimpiar = new JButton("Limpiar");
  195. btnlimpiar.addActionListener(new ActionListener(){
  196. public void actionPerformed(ActionEvent ae){
  197. JOptionPane.showMessageDialog(null,"Datos Limpiados");
  198. JText1.setText("");
  199. JText2.setText("");
  200. JText3.setText("");
  201. JText4.setText("");
  202. JText5.setText("");
  203. combo.setSelectedIndex(0);
  204. }
  205. });
  206.  
  207. // aca se agregan los paneles y botones
  208.  
  209. panelBoton.add(btnregistrar);
  210. panelBoton.add(btnborrar);
  211. panelBoton.add(btnmodificar);
  212. panelBoton.add(btnconsultar);
  213. panelBoton.add(btnlimpiar);
  214.  
  215. }
  216.  
  217.  
  218. public void construyeVentana() {
  219. frame = new JFrame(" ");
  220. frame.add(panelSuperior);
  221. frame.add(panelInferior);
  222. frame.add(panelBoton);
  223. frame.pack();
  224. frame.setVisible(true);
  225. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  226. frame.setTitle("Ingreso de Datos Clientes Ver. 1.0"); // borde titulo pantalla
  227.  
  228. }
  229.  
  230. public static void main(String[] arg) {
  231. new MenuPrincipal();
  232. }
  233. }[ / code]
En línea

Mitsu

Desconectado Desconectado

Mensajes: 259



Ver Perfil WWW
Re: Consulta sobre código
« Respuesta #13 en: 29 Octubre 2013, 01:37 am »

Disuclpa, no estaba. Aquí te dejo un pequeño code, no lo he probado pero debería funcionar. Agrega y elimina filas:

Código
  1. import java.awt.Dimension;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.JTable;
  9. import javax.swing.JTextField;
  10. import javax.swing.table.DefaultTableModel;
  11.  
  12. public class Tabla extends JFrame implements ActionListener{
  13.  
  14.    private JTextField txtNombre;
  15.    private JTextField txtApellidoP;
  16.    private JTextField txtApellidoM;
  17.    private JTextField txtDNI;
  18.    private JButton btnAdd;
  19.    private JButton btnRemove;
  20.    private JTable table;
  21.    private DefaultTableModel modelo;
  22.  
  23.    String[] cabeceras =
  24.        {
  25.            "Nombre","Apellido P.","Apellido M.","DNI"
  26.        };
  27.  
  28.    public static void main(String... args) {
  29.        new Tabla();
  30.    }
  31.  
  32.    public Tabla() {
  33.        super("Ejemplo Tabla");
  34.        setLayout(null);
  35.        JLabel lblNombre = new JLabel("Nombre:");
  36.        JLabel lblApellidoP = new JLabel("Apellido P.:");
  37.        JLabel lblApellidoM = new JLabel("Apellido M.:");
  38.        JLabel lblDNI = new JLabel("DNI:");
  39.  
  40.        txtNombre = new JTextField("",10);
  41.        txtApellidoP = new JTextField("",10);
  42.        txtApellidoM = new JTextField("",10);
  43.        txtDNI = new JTextField("",10);
  44.  
  45.        btnAdd = new JButton("Agregar");
  46.        btnRemove = new JButton("Eliminar");
  47.  
  48.        lblNombre.setBounds(20,20,80,20);
  49.        lblApellidoP.setBounds(250,20,80,20);
  50.        lblApellidoM.setBounds(20,70,80,20);
  51.        lblDNI.setBounds(250,70,80,20);
  52.        txtNombre.setBounds(130,22,100,25);
  53.        txtApellidoP.setBounds(330,22,100,25);
  54.        txtApellidoM.setBounds(130,72,100,25);
  55.        txtDNI.setBounds(330,72,100,25);
  56.        btnAdd.setBounds(30,102,90,35);
  57.        btnAdd.addActionListener(this);
  58.        btnRemove.setBounds(130,102,90,35);
  59.        btnRemove.addActionListener(this);
  60.  
  61.  
  62.        //Array bidimensional de objetos con los datos de la tabla
  63.        Object[][] data = {{null, null, null, null, null}
  64.        };
  65.  
  66.        //Creacion de la tabla
  67.        modelo = new DefaultTableModel(data,cabeceras);
  68.        table = new JTable(modelo);
  69.        table.setPreferredScrollableViewportSize(new Dimension(500, 80));
  70.  
  71.        //Creamos un scrollpanel y se lo agregamos a la tabla
  72.        JScrollPane scrollpane = new JScrollPane(table);
  73.        scrollpane.setBounds(20, 142, 400, 120);
  74.  
  75.  
  76.        this.add(lblNombre);
  77.        this.add(txtNombre);
  78.        this.add(lblApellidoP);
  79.        this.add(txtApellidoP);
  80.        this.add(lblApellidoM);
  81.        this.add(txtApellidoM);
  82.        this.add(lblDNI);
  83.        this.add(txtDNI);
  84.        this.add(btnAdd);
  85.        this.add(btnRemove);
  86.        this.add(scrollpane);
  87.  
  88.        this.setSize(500,500);
  89.        this.setVisible(true);
  90.        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  91.    }
  92.  
  93.    /* *********************************************************************************************************
  94.       EVENTOS DE LOS BOTONES
  95.     * *********************************************************************************************************/
  96.    @Override
  97.    public void actionPerformed(ActionEvent e) {
  98.        if(e.getSource() == btnAdd){
  99.            String nombre = txtNombre.getText();
  100.                String apellido = txtApellidoP.getText();
  101.                String apellidom = txtApellidoM.getText();
  102.                String dni = txtDNI.getText();
  103.  
  104.                modelo.addRow(new Object[]{nombre,apellido,apellidom,dni});
  105.        }
  106.        else if(e.getSource() == btnRemove) {
  107.            try {
  108.            modelo=(DefaultTableModel) table.getModel();
  109.            modelo.removeRow(table.getSelectedRow());
  110.  
  111.        } catch (Exception ex) {
  112.            }
  113.        }
  114.    }
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  


Las celdas son editables por defecto en los JTable. Para el resto, te toca investigar ;)
« Última modificación: 29 Octubre 2013, 01:55 am por JavaBrain » En línea

SebastianJava

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Re: Consulta sobre código
« Respuesta #14 en: 29 Octubre 2013, 02:23 am »

Eres un genio, como hiciste eso, yo que estuve bastante tiempo trtando de hacerlo, mi codigo es gigante comparado al tuyo, mil gracias, investigare el resto,  ;)
En línea

Mitsu

Desconectado Desconectado

Mensajes: 259



Ver Perfil WWW
Re: Consulta sobre código
« Respuesta #15 en: 29 Octubre 2013, 02:40 am »

Me alegro que te haya servido. Sigue practicando!


Saludos.
En línea

SebastianJava

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Re: Consulta sobre código
« Respuesta #16 en: 29 Octubre 2013, 02:44 am »

AMIGO ME RECOMIENDA ALGUN LIBRO PARA PRINCIPIANTES C;
En línea

Mitsu

Desconectado Desconectado

Mensajes: 259



Ver Perfil WWW
Re: Consulta sobre código
« Respuesta #17 en: 29 Octubre 2013, 02:56 am »

Como programar en Java - Deitel: http://depositfiles.org/files/rqppw0qp4

También, Pensar en Java de Bruce Eckel.
En línea

SebastianJava

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Re: Consulta sobre código
« Respuesta #18 en: 29 Octubre 2013, 03:02 am »

gracias amigo
En línea

Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
consulta sobre codigo PHP subir fotos
PHP
Dannysoft 1 1,964 Último mensaje 5 Septiembre 2007, 06:45 am
por alone-in-the-chat
consulta codigo
Java
juang06 1 2,787 Último mensaje 23 Abril 2010, 21:01 pm
por Cuantico
Consulta parte codigo
Programación Visual Basic
sersik 3 2,045 Último mensaje 12 Septiembre 2013, 23:34 pm
por sersik
[Consulta] ¿Se desaprueba este código?. « 1 2 »
Programación C/C++
sabeeee 13 6,057 Último mensaje 29 Enero 2015, 05:53 am
por DarK_FirefoX
Consulta sobre código batch para control de servicios en servers
Scripting
duviadrian 0 1,872 Último mensaje 14 Marzo 2018, 22:12 pm
por duviadrian
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines