Hola gacs. No analice el codigo que incluiste en tu post, pero basado en tu explicacion se me ocurre que podes crear una clase abstracta que herede de JToolBar en la cual definis todas las cuestiones que no queres que los JInternalFrame modifiquen (tamaños, imagenes etc..) y luego definis metodos abstractos para la funcionalidad de los botones. De esta forma en cada internal frame creas una jtoolbar que herede de la abstracta creada por vos y asi podes implementar los metodos abstractos y darles la funcionalidad correcta en cada caso, sin que se modifiquen el resto de las cosas. Espero haber sido claro y poderte ayudar.
Buenas a todos
su ayuda, tengo una aplicacion que quiero que una jtoolbar la hereden varios jinternalfrema, pero en cada JIframe se pueda modificar el desempeño de los botones que se mantenga tamaño y iconos de los botones pero que no se modifiquen los botenes
este es el codigo del internalframe
package gsinv; import java.awt.Frame; import java.awt.event.ActionEvent; /** * * @author gacs */ public class frmfactura extends javax.swing.JInternalFrame { /** * Creates new form factura */ public frmfactura() { initComponents(); } /*private void cmdclosed(java.awt.event.ActionEvent evt){ this.dispose(); } */ /** * 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() { jtbstandar = new clases.jtbarbase(); setTitle("Factura"); jtbstandar.setRollover(true); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jtbstandar, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jtbstandar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 226, Short.MAX_VALUE)) ); pack(); }// </editor-fold> // Variables declaration - do not modify private clases.jtbarbase jtbstandar; // End of variables declaration }
este el codigo del jtoolbar
package clases; import javax.swing.JToolBar; import javax.swing.JButton; import javax.swing.ImageIcon; public class jtbarbase extends javax.swing.JToolBar { public jtbarbase(){ initComponents(); } private void initComponents(){ JButton jbnew = new JButton(); JButton jbsave = new JButton(); JButton jbdel = new JButton(); JButton jbprint = new JButton(); JButton jbclose = new JButton(); // valores de jbnew jbnew.setAlignmentY(CENTER_ALIGNMENT); jbnew.setIcon(new ImageIcon(getClass().getResource("/imagenes/add.png"))); jbnew.setToolTipText("Agrega un nuevo registro"); // valores de jbsave jbsave.setAlignmentY(CENTER_ALIGNMENT); jbsave.setIcon(new ImageIcon(getClass().getResource("/imagenes/save.png"))); jbsave.setToolTipText("Guarda las modificaciones"); // valores de jbdel jbdel.setAlignmentY(CENTER_ALIGNMENT); jbdel.setIcon(new ImageIcon(getClass().getResource("/imagenes/borrar.png"))); jbdel.setToolTipText("Borra un registro"); // valores de jbprint jbprint.setAlignmentY(CENTER_ALIGNMENT); jbprint.setIcon(new ImageIcon(getClass().getResource("/imagenes/print.png"))); jbprint.setToolTipText("Imprime informe"); // valores de jbclose jbclose.setAlignmentY(CENTER_ALIGNMENT); jbclose.setAlignmentX(RIGHT_ALIGNMENT); jbclose.setIcon(new ImageIcon(getClass().getResource("/imagenes/close.png"))); jbclose.setToolTipText("Cierra la pantalla"); jbclose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdclosePerformed(evt); } }); add(jbnew); add(jbsave); add(jbdel); add(jbprint); add(jbclose); setFloatable(false); } private javax.swing.JButton jbnew; private javax.swing.JButton jbsabe; private javax.swing.JButton jbprint; private javax.swing.JButton jbclose; }
estoy trabajando con netbeans 8.0 para linux, cuando agrego el jtoolbar al jiframe no puedo modificar los botones , no puedo agregar métodos de ejecución.
buscando por la red me encontré este código, pero no se como implementarlo a lo que estoy haciendo ya que no encuentro información SmallButton
import java.awt.BorderLayout; import java.awt.Font; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JToggleButton; import javax.swing.JToolBar; import javax.swing.border.BevelBorder; import javax.swing.border.Border; public class SimpleToolbar extends JFrame { public static final String FontNames[] = { "Serif", "SansSerif", "Courier" }; protected Font fonts[]; protected JFileChooser fileChooser = new JFileChooser(); protected JToolBar toolBar; protected JComboBox cbFonts; protected SmallToggleButton bBold; protected SmallToggleButton bItalic; public SimpleToolbar() { super(); setSize(450, 350); fonts = new Font[FontNames.length]; for (int k = 0; k < FontNames.length; k++) fonts[k] = new Font(FontNames[k], Font.PLAIN, 12); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; addWindowListener(wndCloser); createToolBar(); setVisible(true); } protected void createToolBar() { ImageIcon iconNew = new ImageIcon("file_new.gif"); Action actionNew = new AbstractAction("New", iconNew) { public void actionPerformed(ActionEvent e) { ; } }; ImageIcon iconOpen = new ImageIcon("file_open.gif"); Action actionOpen = new AbstractAction("Open...", iconOpen) { public void actionPerformed(ActionEvent e) { } }; ImageIcon iconSave = new ImageIcon("file_save.gif"); Action actionSave = new AbstractAction("Save...", iconSave) { public void actionPerformed(ActionEvent e) { } }; Action actionExit = new AbstractAction("Exit") { public void actionPerformed(ActionEvent e) { System.exit(0); } }; toolBar = new JToolBar(); JButton bNew = new SmallButton(actionNew, "New text"); toolBar.add(bNew); JButton bOpen = new SmallButton(actionOpen, "Open text file"); toolBar.add(bOpen); JButton bSave = new SmallButton(actionSave, "Save text file"); toolBar.add(bSave); ActionListener fontListener = new ActionListener() { public void actionPerformed(ActionEvent e) { } }; toolBar.addSeparator(); cbFonts = new JComboBox(FontNames); cbFonts.setMaximumSize(cbFonts.getPreferredSize()); cbFonts.setToolTipText("Available fonts"); ActionListener lst = new ActionListener() { public void actionPerformed(ActionEvent e) { int index = cbFonts.getSelectedIndex(); if (index < 0) return; } }; cbFonts.addActionListener(lst); toolBar.add(cbFonts); toolBar.addSeparator(); ImageIcon img1 = new ImageIcon("font_bold1.gif"); ImageIcon img2 = new ImageIcon("font_bold2.gif"); bBold = new SmallToggleButton(false, img1, img2, "Bold font"); lst = new ActionListener() { public void actionPerformed(ActionEvent e) { } }; bBold.addActionListener(lst); toolBar.add(bBold); img1 = new ImageIcon("font_italic1.gif"); img2 = new ImageIcon("font_italic2.gif"); bItalic = new SmallToggleButton(false, img1, img2, "Italic font"); lst = new ActionListener() { public void actionPerformed(ActionEvent e) { } }; bItalic.addActionListener(lst); toolBar.add(bItalic); getContentPane().add(toolBar, BorderLayout.NORTH); } public static void main(String[] a){ new SimpleToolbar(); } } class SmallButton extends JButton implements MouseListener { protected Border m_raised; protected Border m_lowered; protected Border m_inactive; public SmallButton(Action act, String tip) { super((Icon) act.getValue(Action.SMALL_ICON)); m_raised = new BevelBorder(BevelBorder.RAISED); m_lowered = new BevelBorder(BevelBorder.LOWERED); m_inactive = new EmptyBorder(2, 2, 2, 2); setBorder(m_inactive); setMargin(new Insets(1, 1, 1, 1)); setToolTipText(tip); addActionListener(act); addMouseListener(this); setRequestFocusEnabled(false); } public float getAlignmentY() { return 0.5f; } public void mousePressed(MouseEvent e) { setBorder(m_lowered); } public void mouseReleased(MouseEvent e) { setBorder(m_inactive); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { setBorder(m_raised); } public void mouseExited(MouseEvent e) { setBorder(m_inactive); } } class SmallToggleButton extends JToggleButton implements ItemListener { protected Border raised; protected Border lowered; public SmallToggleButton(boolean selected, ImageIcon imgUnselected, ImageIcon imgSelected, String tip) { super(imgUnselected, selected); setHorizontalAlignment(CENTER); setBorderPainted(true); raised = new BevelBorder(BevelBorder.RAISED); lowered = new BevelBorder(BevelBorder.LOWERED); setBorder(selected ? lowered : raised); setMargin(new Insets(1, 1, 1, 1)); setToolTipText(tip); setRequestFocusEnabled(false); setSelectedIcon(imgSelected); addItemListener(this); } public float getAlignmentY() { return 0.5f; } public void itemStateChanged(ItemEvent e) { setBorder(isSelected() ? lowered : raised); } }