tags:

views:

272

answers:

3

I have created JToolBar (Java Swing). I have set a Background image on frame which contains JToolBar. I want my JToolBar to be transparent so that the image kept on frame should be visible. I am using setOpaque(false) but it is not having any effect on my toolbar.. Has anyone delt with this before? My Code is as follows

import javax.swing.ImageIcon; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;

public class NewJFrame extends javax.swing.JFrame { static NewJFrame dialog = null;

/** Creates new form NewJFrame */
public NewJFrame() {
    initComponents();
    jToolBar1.setOpaque(false);
}

/** 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() {

    jToolBar1 = new javax.swing.JToolBar();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jToolBar1.setRollover(true);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(275, Short.MAX_VALUE))
    );

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {



        public void run() {

            try {
                try {
                    // Set cross-platform Java L&F (also called "Metal")
                    UIManager.setLookAndFeel(UIManager.
                            getSystemLookAndFeelClassName());

                } catch (UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ImagePanel panel = new ImagePanel(new ImageIcon("image").getImage());
            dialog = new NewJFrame();
            dialog.getContentPane().add(panel);
            dialog.setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JToolBar jToolBar1;
// End of variables declaration

}

A: 

There was a bug with JToolBar ignoring setOpaque and background image http://bugs.sun.com/bugdatabase/view%5Fbug.do?bug%5Fid=6418117

They say it was fixed in 1.6. What version are you using?

What if you set the same image as background for JToolBar? Would two backgrounds overlap?

tulskiy
Thanks for replying Piligrim. I am using 1.6. I am able to set background image for JToolBar but not able to make it transparent. If I set the same frame image to the toolbar, it get distorted because the size of the image is equivalent to frame size whereas toolbar is smaller. So what should i do?
Nilesh
A: 

Works fine for me using the Metal LAF with JDK 6 on XP.

If you need more help post your SSCCE showing the problem.

camickr
+1  A: 

JToolBar creates JButtons to contain the actions you set on it. By default, JButtons are opaque. To get the effect you're describing, you need to override JToolBar.createActionComponent.

For example:

    jToolBar1 = new javax.swing.JToolBar() {
        @Override
        protected JButton createActionComponent(Action a) {
            JButton jb = super.createActionComponent(a);
            jb.setOpaque(false);
            return jb;
        }
    };

Note: YMMV depending on the LAF in use.

Devon_C_Miller
Thank you so much... it works now
Nilesh