IBRA
IBRA

Reputation: 1792

Hide tab header in java

I have Tabbed Pane with three tabs. How to hide the header of these tabs to prevent the user from pressing on them?

I used to do it in c# by creating a class inherited from tab component class and then override the method to hide the tabs, but recently i switch to java and i searched a lot without reaching any result.

Upvotes: 1

Views: 3735

Answers (2)

berachad ayoub
berachad ayoub

Reputation: 9

Yes, Here is an clean example, it works for me :

    JPanel panelOutils =  new JPanel();
    panelOutils.add(ongletOutils); 
    JScrollPane scrollOutils = new JScrollPane(panelOutils,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    JTabbedPane jTabbedPaneOutils = new JTabbedPane();
    // With Tab header 
    jTabbedPaneOutils.add(scrollOutils);        
    this.addTab("Outils", jTabbedPaneOutils);

    // Delete tab header  
    this.addTab("Outils", jTabbedPaneOutils.add(scrollOutils));

Upvotes: 0

user1983527
user1983527

Reputation: 304

try this

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
  public void buildGUI()
  {
    JTabbedPane tp = new JTabbedPane();
    tp.addTab("A",getPanel("A"));
    tp.addTab("B",getPanel("B"));
    tp.addTab("C",getPanel("C"));
    //Control Header space area..
    final boolean showTabsHeader = false;
    tp.setUI(new javax.swing.plaf.metal.MetalTabbedPaneUI(){
        @Override
        protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight) {
            if (showTabsHeader) {
                return super.calculateTabAreaHeight(tabPlacement, horizRunCount, maxTabHeight);
            } else {
                return 0;
            }
        }
      protected void paintTabArea(Graphics g,int tabPlacement,int selectedIndex){}
    });

    JFrame f = new JFrame();
    f.getContentPane().add(tp);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
  public JPanel getPanel(String tabText)
  {
    JPanel p = new JPanel();
    p.add(new JLabel(tabText));
    return p;
  }
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable(){
      public void run(){
        new Testing().buildGUI();
      }
    });
  }
}

Upvotes: 5

Related Questions