0

I am trying to set a background image for tabbed pane in java swing application but i can keep only colors how to go through with image ?

EDIT from comment

i want the image for the full tabbed pane. if i set image to label and keep it for full screen i cant keep any components on the label

manuell
  • 7,528
  • 5
  • 31
  • 58
Suresh
  • 501
  • 1
  • 5
  • 11

1 Answers1

1

Here's what you do using GUI Builder. Instead of using the drag and drop in the JFrame form to drag and drop JPanels for the JTabbedPane, do this.

  1. Create a JPanel form class
  2. Layout the JPanel with all the components you want.
  3. In the constructor add this to load the image. Use your own image path

    public class TabPanelOne extends javax.swing.JPanel {
        Image img;
        public TabPanelOne() {
            try {
                img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png"));
            } catch (IOException ex) {
                Logger.getLogger(TabPanelOne.class.getName()).log(Level.SEVERE, null, ex);
            }
            initComponents();
        }
    
  4. Override the paintComponent method of the JPanel and draw the image

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
    
  5. You should have an empty JTabbedPane in your JFrame form class. In the constructor, just add the JPanel form

    public class NewJFrame extends javax.swing.JFrame {
        public NewJFrame() {
            initComponents();
            jTabbedPane1.add("panel1", new TabPanelOne());
        }
    

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • The question is how to make transparent tabbed pane not a component or a panel inside it. – dacwe Feb 19 '14 at 07:57
  • @peeskillet how to add a image inside the panel i used this code its running but image is not shown ImageIcon icon=new ImageIcon("logo.png"); jLabel5.setIcon(icon); – Suresh Feb 19 '14 at 09:06
  • @Suresh **Don't** add a `JLabel`. You won't be able to add components on top of it. You need to paint the image onto the `JPanel` form. You need to follow the instructions above. Create separate `JPanel` form, **don't** drag an drop a `JPanel` on to the `JFrame` form. Do you know how to create a `JPanel` form? – Paul Samsotha Feb 19 '14 at 09:24
  • @Suresh look at your previous post. It shows how to load am image. You're making the same mistake your were before. – Paul Samsotha Feb 19 '14 at 09:26
  • @peeskillet i got to know now am trying with it – Suresh Feb 19 '14 at 09:27