Java - eventos en un JTabbedPane

 
Vista:

eventos en un JTabbedPane

Publicado por Edgar (64 intervenciones) el 25/03/2002 00:00:17
Mi pregunta es como puedo agregar un icono a una pestaña y que este icono pueda tener un ActionListener para que al pulsarlo se pueda cerrar la pestaña en la que esta actualmente el foco, o como poner un JPopupMenu con esa funcion en uno de sus elementos.

NOTA: El popup solo deve afectar a la pestaña indicada.

Muchas gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:eventos en un JTabbedPane

Publicado por marta (5 intervenciones) el 09/02/2004 19:05:12
De Momento solo se como añadir un icono
que es de la siguiente manera

JEditorPane editPane = new JEditorPane();
editPane.setPage(url);
ImageIcon icon = new ImageIcon("imagenes/cut.gif");
//jTabbedPane1.add(editPane, fileName2);
jTabbedPane1.addTab(fileName2,icon,editPane,fileName);

Cuando averigüe lo de cerrarla pestaña ya te avisare.
Y si tu lo sabes antes espero que me ayudes a mi
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:eventos en un JTabbedPane

Publicado por Kevin Orduz (1 intervención) el 03/07/2017 00:14:22
En este vídeo explican como hacerlo
y esta es la clase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package Clases;
 
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JTabbedPane;
 
public class ClosableTabbedPane extends JTabbedPane
{
  private static final long serialVersionUID = 1L;
  private TabCloseUI closeUI = new TabCloseUI(this);
 
  public void paint(Graphics g) {
    super.paint(g);
    this.closeUI.paint(g);
  }
 
  public void addTab(String title, Component component) {
    super.addTab(title + "  ", component);
  }
 
  public String getTabTitleAt(int index)
  {
    return super.getTitleAt(index).trim();
  }
 
  public boolean tabAboutToClose(int tabIndex)
  {
    return true;
  }
 
  private class TabCloseUI
    implements MouseListener, MouseMotionListener
  {
    private ClosableTabbedPane tabbedPane;
    private int closeX = 0; private int closeY = 0; private int meX = 0; private int meY = 0;
    private int selectedTab;
    private final int width = 8; private final int height = 8;
    private Rectangle rectangle = new Rectangle(0, 0, 8, 8);
 
    private TabCloseUI() {
    }
    public TabCloseUI(ClosableTabbedPane pane) {
      this.tabbedPane = pane;
      this.tabbedPane.addMouseMotionListener(this);
      this.tabbedPane.addMouseListener(this);
    }
    public void mouseEntered(MouseEvent me) {
    }
    public void mouseExited(MouseEvent me) {
    }
    public void mousePressed(MouseEvent me) {
    }
    public void mouseClicked(MouseEvent me) {
    }
    public void mouseDragged(MouseEvent me) {  }
    public void mouseReleased(MouseEvent me) { if (closeUnderMouse(me.getX(), me.getY())) {
        boolean isToCloseTab = ClosableTabbedPane.this.tabAboutToClose(this.selectedTab);
        if ((isToCloseTab) && (this.selectedTab > -1)) {
          this.tabbedPane.removeTabAt(this.selectedTab);
        }
        this.selectedTab = this.tabbedPane.getSelectedIndex();
      } }
 
    public void mouseMoved(MouseEvent me)
    {
      this.meX = me.getX();
      this.meY = me.getY();
      if (mouseOverTab(this.meX, this.meY)) {
        controlCursor();
        this.tabbedPane.repaint();
      }
    }
 
    private void controlCursor() {
      if (this.tabbedPane.getTabCount() > 0)
        if (closeUnderMouse(this.meX, this.meY)) {
          this.tabbedPane.setCursor(new Cursor(12));
          if (this.selectedTab > -1)
            this.tabbedPane.setToolTipTextAt(this.selectedTab, "Close " + this.tabbedPane.getTitleAt(this.selectedTab));
        }
        else {
          this.tabbedPane.setCursor(new Cursor(0));
          if (this.selectedTab > -1)
            this.tabbedPane.setToolTipTextAt(this.selectedTab, "");
        }
    }
 
    private boolean closeUnderMouse(int x, int y) {
      this.rectangle.x = this.closeX;
      this.rectangle.y = this.closeY;
      return this.rectangle.contains(x, y);
    }
 
    public void paint(Graphics g)
    {
      int tabCount = this.tabbedPane.getTabCount();
      for (int j = 0; j < tabCount; j++)
        if (this.tabbedPane.getComponent(j).isShowing()) {
          int x = this.tabbedPane.getBoundsAt(j).x + this.tabbedPane.getBoundsAt(j).width - 8 - 5;
          int y = this.tabbedPane.getBoundsAt(j).y + 5;
          drawClose(g, x, y);
          break;
        }
      if (mouseOverTab(this.meX, this.meY))
        drawClose(g, this.closeX, this.closeY);
    }
 
    private void drawClose(Graphics g, int x, int y)
    {
      if ((this.tabbedPane != null) && (this.tabbedPane.getTabCount() > 0)) {
        Graphics2D g2 = (Graphics2D)g;
        drawColored(g2, isUnderMouse(x, y) ? Color.RED : Color.WHITE, x, y);
      }
    }
 
    private void drawColored(Graphics2D g2, Color color, int x, int y) {
      g2.setStroke(new BasicStroke(5.0F, 1, 1));
      g2.setColor(Color.BLACK);
      g2.drawLine(x, y, x + 8, y + 8);
      g2.drawLine(x + 8, y, x, y + 8);
      g2.setColor(color);
      g2.setStroke(new BasicStroke(3.0F, 1, 1));
      g2.drawLine(x, y, x + 8, y + 8);
      g2.drawLine(x + 8, y, x, y + 8);
    }
 
    private boolean isUnderMouse(int x, int y)
    {
      if ((Math.abs(x - this.meX) < 8) && (Math.abs(y - this.meY) < 8))
        return true;
      return false;
    }
 
    private boolean mouseOverTab(int x, int y) {
      int tabCount = this.tabbedPane.getTabCount();
      for (int j = 0; j < tabCount; j++)
        if (this.tabbedPane.getBoundsAt(j).contains(this.meX, this.meY)) {
          this.selectedTab = j;
          this.closeX = (this.tabbedPane.getBoundsAt(j).x + this.tabbedPane.getBoundsAt(j).width - 8 - 5);
          this.closeY = (this.tabbedPane.getBoundsAt(j).y + 5);
          return true;
        }
      return false;
    }
  }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar