Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTabPane Problem  RSS feed

 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have done bit of coding for a tabPane with "x" mark at each pane to close the pane once its clicked as in all ides..
The code is as follows
----------------------------------------------------------------------------
package com;

import javax.swing.*;

import javax.swing.plaf.basic.BasicButtonUI;

import java.awt.*;

import java.awt.event.*;



/**

* Component to be used as tabComponent;

* Contains a JLabel to show the text and

* a JButton to close the tab it belongs to

*/

public class ButtonTabComponent extends JPanel {

private final JTabbedPane pane;



public ButtonTabComponent(String title, final JTabbedPane pane)

{

//unset default FlowLayout' gaps

super(new FlowLayout(FlowLayout.LEFT, 0, 0));

if (pane == null) {

throw new NullPointerException("TabbedPane is null");

}

this.pane = pane;

setOpaque(false);



//make JLabel read titles from JTabbedPane

JLabel label = new JLabel(title);



add(label);

//add more space between the label and the button

label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

//tab button

JButton button = new TabButton();

add(button);

//add more space to the top of the component

setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));

}



private class TabButton extends JButton implements ActionListener {

public TabButton() {

int size = 17;

setPreferredSize(new Dimension(size, size));

setToolTipText("close this tab");

//Make the button looks the same for all Laf's

setUI(new BasicButtonUI());

//Make it transparent

setContentAreaFilled(false);

//No need to be focusable

setFocusable(false);

setBorder(BorderFactory.createEtchedBorder());

setBorderPainted(false);

//Making nice rollover effect

//we use the same listener for all buttons

addMouseListener(buttonMouseListener);

setRolloverEnabled(true);

//Close the proper tab by clicking the button

addActionListener(this);

}



public void actionPerformed(ActionEvent e) {

int i = pane.indexOfTabComponent(ButtonTabComponent.this);

if (i != -1) {

pane.remove(i);

}

}



//we don't want to update UI for this button

public void updateUI() {

}



//paint the cross

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g.create();

//shift the image for pressed buttons

if (getModel().isPressed()) {

g2.translate(1, 1);

}

g2.setStroke(new BasicStroke(2));

g2.setColor(Color.BLACK);

if (getModel().isRollover()) {

g2.setColor(Color.MAGENTA);

}

int delta = 6;

g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);

g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);

g2.dispose();

}

}



private final static MouseListener buttonMouseListener = new MouseAdapter() {

public void mouseEntered(MouseEvent e) {

Component component = e.getComponent();

if (component instanceof AbstractButton) {

AbstractButton button = (AbstractButton) component;

button.setBorderPainted(true);

}

}



public void mouseExited(MouseEvent e) {

Component component = e.getComponent();

if (component instanceof AbstractButton) {

AbstractButton button = (AbstractButton) component;

button.setBorderPainted(false);

}

}

};

}


----------------------------------------------------------------------------
Here is the main program..
----------------------------------------------------------------------------

package com;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


public class yo
{
public static void main(String args[])
{
JFrame frame=new JFrame();
JTabbedPane tab=new JTabbedPane();
tab.add("yooo",new JPanel());
tab.setTabComponentAt(0,new ButtonTabComponent("yo",tab));
tab.add("yooo32",new JPanel());
tab.setTabComponentAt(1,new ButtonTabComponent("aba",tab));
frame.setLayout(new BorderLayout());
frame.add(tab,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}

}
---------------------------------------------------------------------------
Now coming to the problem. The above code works fine with out any problem. But when i say tab.setTabComponentAt(tab.getTabCount(),new ButtonTabComponent("sometitle",tab)); its not painting properly. I am not able to figure out the problem
Actually i am creating a panel(which is kinda editor) dynamically opening the code on fire of an event. So if i put tab.setTabComponentAt(tab.getTabCount(),new ButtonTabComponent("sometitle",tab));

Its not rendering properly. Please let me know where am i going wrong..

Thank you
 
sammaiah kyatham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use getSelectedIndex() method of Tabbed pane to know the selected tab index.
and then use remove(int index) to remove that specific tab...

Call revalidate() method after UI change.

Thanks,
Sam
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.. there was a small problem i have sorted it out..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!