Java - Como aplicar Herencia de una jtoolbar en varios jinternalframe

   
Vista:
Imágen de perfil de Gustavo

Como aplicar Herencia de una jtoolbar en varios jinternalframe

Publicado por Gustavo (1 intervención) el 01/11/2014 02:07:54
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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);
  }
}
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