Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TabbedPane problem

Posted on 2010-09-13
10
405 Views
Last Modified: 2013-11-23
With the code below, the bottom border of the tabs appear to be occluded.
Occluded TabCan this be fixed?

package tabbedPaneTest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

/**
 * TabbedPane
 *
 */
public class TabbedPane extends JTabbedPane { 

	private static TabbedPane instance = null;

	private JPanel bluePanel;
	private JPanel redPanel;
	private JPanel yellowPanel;

	final private int YELLOW_INDEX = 0;
	final private int BLUE_INDEX = 1;
	final private int RED_INDEX = 2;

	private JPanel blueTabTitlePanel;
	private JPanel redTabTitlePanel;
	private JPanel yellowTabTitlePanel;
	
	private JLabel blueLabel;
	private JLabel redLabel; 
	private JLabel yellowLabel;

	
    /**
	 * singleton, call initialize() instead
	 */
	private TabbedPane() {
		setLayout();
	}

	/**
	 * singleton
	 * 
	 * @return
	 */
	public static TabbedPane getInstance()
	{
		if (instance == null) {
			instance = new TabbedPane();
		}
		return instance;
	}

	/** 
	 * set layout of tabs
	 */
	private void setLayout() {

		this.setTabPlacement(SwingConstants.LEFT); // set tabs on left side
		
		FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 5, 5);

		yellowPanel = new JPanel();
		yellowPanel.setName("Yellow");
		this.addTab(yellowPanel.getName(), yellowPanel);
		
		yellowTabTitlePanel = new JPanel();
		yellowTabTitlePanel.setLayout(flowLayout);
		yellowTabTitlePanel.setOpaque(true);
		yellowLabel = new JLabel(yellowPanel.getName());
		yellowLabel.setOpaque(true);
		yellowTabTitlePanel.add(yellowLabel);
		Dimension dimension = yellowTabTitlePanel.getPreferredSize();
		int width = dimension.width;
		int height = dimension.height;
		yellowTabTitlePanel.setPreferredSize(new Dimension(width, height));
		this.setTabComponentAt(YELLOW_INDEX, yellowTabTitlePanel);
		
		bluePanel = new JPanel();
		bluePanel.setName("Blue");
		this.addTab(bluePanel.getName(), bluePanel);
		
		blueTabTitlePanel = new JPanel();
		blueTabTitlePanel.setLayout(flowLayout);
		blueTabTitlePanel.setOpaque(true);
		blueLabel = new JLabel(bluePanel.getName());
		blueLabel.setOpaque(true);
		blueLabel.setBackground(Color.white); // first tab selected
		blueTabTitlePanel.setBackground(Color.white); // first tab selected
		blueTabTitlePanel.add(blueLabel);
		blueTabTitlePanel.setPreferredSize(new Dimension(width, height));
		this.setTabComponentAt(BLUE_INDEX, blueTabTitlePanel);

		redPanel = new JPanel();
		redPanel.setName("Red");
		this.addTab(redPanel.getName(), redPanel);
		
		redTabTitlePanel = new JPanel();
		redTabTitlePanel.setLayout(flowLayout);
		redTabTitlePanel.setOpaque(true);
		redLabel = new JLabel(redPanel.getName());
		redLabel.setOpaque(true);
		redTabTitlePanel.add(redLabel);
		redTabTitlePanel.setPreferredSize(new Dimension(width, height));
		this.setTabComponentAt(RED_INDEX, redTabTitlePanel);
		
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String args[]) {

		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		try {

			final JFrame frame = new JFrame();
			TabbedPane tabbedPane =  TabbedPane.getInstance();

			frame.getContentPane().add(new JScrollPane(tabbedPane));
			frame.setTitle("TabbedPane");
			frame.setPreferredSize(new Dimension(500, 500));
			frame.addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e) {
				}
			});

			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.setLocation(300, 200);
			frame.pack();
			frame.setVisible(true);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Open in new window

0
Comment
Question by:allelopath
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33667710
looks like your size of yuor panel is too high for the space available.
Do you actually need to paint the panel background?
0
 
LVL 1

Author Comment

by:allelopath
ID: 33668537
I've tried limiting the height of the panel, but that doesn't fix the problem.

In this example, the need for the panel is not apparent. In an actual application, I have an icon and text on the panel, the panel serving as the container for the 2 components.

0
 
LVL 92

Expert Comment

by:objects
ID: 33668551
why aren't you just using a JLabel
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 26

Expert Comment

by:ksivananth
ID: 33669935
you may want to override paintcomponent...
0
 
LVL 1

Author Comment

by:allelopath
ID: 33672138
The same thing happens if I use JLabel:
//this.setTabComponentAt(YELLOW_INDEX, yellowTabTitlePanel);
this.setTabComponentAt(YELLOW_INDEX, yellowLabel);

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 33672163
>>you may want to override paintcomponent...
Can you provide detail? Do you mean create a TabTilePanel class and override paintComponent in there? If so, what would I do in paintComponent?
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 33672333
try adding this code,

            try{
                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                  UIManager.getDefaults().put( "TabbedPane.tabInsets", new Insets( 1, 2, 3, 2 ) ) ;
            }
            catch (Exception e) {
                  e.printStackTrace();
            }
0
 
LVL 1

Author Comment

by:allelopath
ID: 33673814
ksivananth:
That works. Thank you.

This would affect all tabbed panes in an application, yes?
If so, is there a way to get to it affect only this one tabbed pane?

0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 500 total points
ID: 33679539
>>This would affect all tabbed panes in an application, yes?

Yes

>>If so, is there a way to get to it affect only this one tabbed pane?

you need to override the "tabInsets" property in the WindowsTabbedPaneUI( may differ based on the env you run ) and set this as the UI for your tabbedpane, for e.g., in windows,

                  TabbedPane tabbedPane =  TabbedPane.getInstance();
                  tabbedPane.setUI( new WindowsTabbedPaneUI(){
                        protected void installDefaults(){
                              super.installDefaults() ;
                              tabInsets = new Insets( 1, 1, 3, 1 ) ;
                        }
                  } ) ;
0
 
LVL 1

Author Closing Comment

by:allelopath
ID: 33681868
Thanks. good stuff.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 42
going to wrong jsp page 2 33
Why doesn't this text field show up on my Applet frame? 2 19
What browser will run Java? 7 59
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question