Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/01/2016, 16:44
Avatar de andres_15_
andres_15_
 
Fecha de Ingreso: septiembre-2008
Ubicación: Cali
Mensajes: 232
Antigüedad: 15 años, 7 meses
Puntos: 7
Exclamación Crear toolbar genérico para todo mi proyecto

Hola, estoy trabajando en un proyecto nuevo y quiero organizar la estructura de esta forma:

1. Dejar un MDI, para que cuando presione en una opción del menú se abra dentro de la misma ventana y no me cree otra aparte.

2. Dejar una barra de herramientas estándar para todo el proyecto; No quiero crear la misma barra o dar copiar y pegar en cada forma nueva que cree.

3. Utilizar una interface que me obligue a implementar todos los botones que irían en el Toolbar.

Estas son las clases donde tengo el problema:

Código Java:
Ver original
  1. package prueba1.Principal;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JToolBar;
  5.  
  6.  
  7. public class principal_barra_menu extends javax.swing.JInternalFrame {
  8.  
  9.  
  10.     public principal_barra_menu() {
  11.         initComponents();
  12.     }
  13.  
  14.     protected JToolBar get_toolbar() {
  15.         return barPrincipal;
  16.     }
  17.  
  18.     protected JButton getBotonSalvar() {
  19.         return btnGuardar;
  20.     }
  21.  
  22.     protected JButton getBotonNuevo() {
  23.         return btnNuevo;
  24.     }
  25.  
  26.    
  27.     @SuppressWarnings("unchecked")
  28.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  29.     private void initComponents() {
  30.  
  31.         barPrincipal = new javax.swing.JToolBar();
  32.         btnGuardar = new javax.swing.JButton();
  33.         btnNuevo = new javax.swing.JButton();
  34.  
  35.         barPrincipal.setRollover(true);
  36.  
  37.         btnGuardar.setText("Guardar");
  38.         btnGuardar.setFocusable(false);
  39.         btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  40.         btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  41.         btnGuardar.addActionListener(new java.awt.event.ActionListener() {
  42.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  43.                 btnGuardarActionPerformed(evt);
  44.             }
  45.         });
  46.         barPrincipal.add(btnGuardar);
  47.  
  48.         btnNuevo.setText("Nuevo");
  49.         btnNuevo.setFocusable(false);
  50.         btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  51.         btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  52.         btnNuevo.addActionListener(new java.awt.event.ActionListener() {
  53.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  54.                 btnNuevoActionPerformed(evt);
  55.             }
  56.         });
  57.         barPrincipal.add(btnNuevo);
  58.  
  59.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  60.         getContentPane().setLayout(layout);
  61.         layout.setHorizontalGroup(
  62.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63.             .addComponent(barPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
  64.         );
  65.         layout.setVerticalGroup(
  66.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  67.             .addGroup(layout.createSequentialGroup()
  68.                 .addComponent(barPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
  69.                 .addGap(0, 253, Short.MAX_VALUE))
  70.         );
  71.  
  72.         pack();
  73.     }// </editor-fold>                        
  74.  
  75.     private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  76.  
  77.     }                                          
  78.  
  79.     private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {                                        
  80.         // TODO add your handling code here:
  81.     }                                        
  82.  
  83.  
  84.     // Variables declaration - do not modify                    
  85.     private javax.swing.JToolBar barPrincipal;
  86.     private javax.swing.JButton btnGuardar;
  87.     private javax.swing.JButton btnNuevo;
  88.     // End of variables declaration                  
  89. }

Código Java:
Ver original
  1. package prueba1.Principal;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.beans.PropertyVetoException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9.  
  10. public class principal_crear_menu_principal extends principal_barra_menu implements principal_barra_herramientas_interface {
  11.  
  12.  
  13.     public principal_crear_menu_principal() {
  14.         initComponents();
  15.         this.barPrincipal.add(super.get_toolbar());
  16.        
  17.     }
  18.  
  19.  
  20.     @SuppressWarnings("unchecked")
  21.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  22.     private void initComponents() {
  23.  
  24.         barPrincipal = new javax.swing.JToolBar();
  25.         panelPrincipal = new javax.swing.JPanel();
  26.         lblDescripcion = new javax.swing.JLabel();
  27.         txtDescripcion = new javax.swing.JTextField();
  28.  
  29.         setClosable(true);
  30.         setMaximizable(true);
  31.         setResizable(true);
  32.         setTitle("Crear menú");
  33.         setToolTipText("");
  34.         setAutoscrolls(true);
  35.         addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
  36.             public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
  37.             }
  38.             public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
  39.             }
  40.             public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
  41.             }
  42.             public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
  43.             }
  44.             public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
  45.             }
  46.             public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
  47.             }
  48.             public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
  49.                 formInternalFrameOpened(evt);
  50.             }
  51.         });
  52.  
  53.         barPrincipal.setFloatable(false);
  54.         barPrincipal.setRollover(true);
  55.  
  56.         lblDescripcion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  57.         lblDescripcion.setText("Descripción:");
  58.         lblDescripcion.setToolTipText("");
  59.  
  60.         javax.swing.GroupLayout panelPrincipalLayout = new javax.swing.GroupLayout(panelPrincipal);
  61.         panelPrincipal.setLayout(panelPrincipalLayout);
  62.         panelPrincipalLayout.setHorizontalGroup(
  63.             panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  64.             .addGroup(panelPrincipalLayout.createSequentialGroup()
  65.                 .addContainerGap()
  66.                 .addComponent(lblDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
  67.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  68.                 .addComponent(txtDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
  69.                 .addGap(183, 183, 183))
  70.         );
  71.         panelPrincipalLayout.setVerticalGroup(
  72.             panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  73.             .addGroup(panelPrincipalLayout.createSequentialGroup()
  74.                 .addContainerGap()
  75.                 .addGroup(panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  76.                     .addComponent(lblDescripcion)
  77.                     .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  78.                 .addContainerGap(217, Short.MAX_VALUE))
  79.         );
  80.  
  81.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  82.         getContentPane().setLayout(layout);
  83.         layout.setHorizontalGroup(
  84.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  85.             .addComponent(barPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  86.             .addComponent(panelPrincipal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  87.         );
  88.         layout.setVerticalGroup(
  89.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  90.             .addGroup(layout.createSequentialGroup()
  91.                 .addComponent(barPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
  92.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  93.                 .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  94.         );
  95.  
  96.         pack();
  97.     }// </editor-fold>                        
  98.  
  99.     private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {                                        
  100.         try {
  101.             //Dejamos la ventana maximizada por defecto
  102.             this.setMaximum(true);
  103.         } catch (PropertyVetoException ex) {
  104.             Logger.getLogger(principal_crear_menu_principal.class.getName()).log(Level.SEVERE, null, ex);
  105.         }
  106.     }                                        
  107.  
  108.  
  109.            
  110.     private javax.swing.JToolBar barPrincipal;
  111.     private javax.swing.JLabel lblDescripcion;
  112.     private javax.swing.JPanel panelPrincipal;
  113.     private javax.swing.JTextField txtDescripcion;
  114.          
  115.  
  116.     @Override
  117.     public void fnGuardarCambios() {
  118.        
  119.     }
  120.  
  121. }

El problema es que todas las nuevas formas se extienden de la barra que estoy creando, pero como el evento ocurre en la forma padre (un click por ejemplo) este no se ve reflejada en la forma hija, y en la forma hija es donde necesito implementar los metodos de guardar, actualizar y borrar. ¿Alguna idea? Gracias.