Tablaschen mit Schliesskreuz

kirashet

Mitglied
Hallo Community,

ihr kennt bestimmt diese Tablaschen bei NetBeans, die rechts oben dieses Schliesskreuz haben. Weiss jemand, wie ich das selber bauen kann?
Bitte fragt nicht nach, wieso ich das brauche, ich brauche es einfach :)

gruß
kirashet
 
Moin,
meinst Du die Titel von Tabs einer JTabbedPane? Müssen die rechts sein oder kann es evtl. auch links sein?
 
Nein, die Tablaschen von NetBeans haben rechts ein Schliesskreuz, quasi einen Button, der das "TabFenster" schliesst. So etwas suche ich. Die Frage könnte auch lauten: Eigene Komponenten in die Tablasche hinzufügen.
 
Original geschrieben von kirashet
Nein, die Tablaschen von NetBeans haben rechts ein Schliesskreuz, quasi einen Button, der das "TabFenster" schliesst. So etwas suche ich. Die Frage könnte auch lauten: Eigene Komponenten in die Tablasche hinzufügen.

JPanel: links JLabel, rechts JButton
Allerdings kann ich mit Tablasche immer noch nix anfangen, kenne kein Netbeans.
 
Original geschrieben von kirashet
Sagt die die Komponente JTabbedPane etwas?

Grml, siehe meine erste Antwort. ;)
Darauf zielte meine Frage mit links/rechts ab. Bei der JTabbedPane kann man für jedes Tab ein Icon hinterlegen. Dadurch könnte man probieren, diese Icon als unterliegende Componente beim Mausklick zu identifizieren und dann entsprechend zu reagieren. Allerdings ist das Icon immer links afaik.
 
Ich hab's schon, danke für deine Bemühungen. Ich poste mal die Lösung:

Code:
/*
 * JTabbedPaneWithCloseIcons.java
 *
 * Created on 16. August 2004, 14:21
 */

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * A JTabbedPane which has a close ('X') icon on each tab.
 *
 * To add a tab, use the method addTab(String, Component)
 *
 * To have an extra icon on each tab (e.g. like in JBuilder, showing the file type) use
 * the method addTab(String, Component, Icon). Only clicking the 'X' closes the tab.
 */
public class JTabbedPaneWithCloseIcons extends JTabbedPane implements MouseListener {
  public JTabbedPaneWithCloseIcons() {
    super();
    addMouseListener(this);
  }

  public void addTab(String title, Component component) {
    this.addTab(title, component, null);
  }

  public void addTab(String title, Component component, Icon extraIcon) {
    super.addTab(title, new CloseTabIcon(extraIcon), component);
  }

  public void mouseClicked(MouseEvent e) {
    int tabNumber=getUI().tabForCoordinate(this, e.getX(), e.getY());
    if (tabNumber < 0) return;
    Rectangle rect=((CloseTabIcon)getIconAt(tabNumber)).getBounds();
    if (rect.contains(e.getX(), e.getY())) {
      //the tab is being closed
      //this.removeTabAt(tabNumber);
        javax.swing.JOptionPane.showMessageDialog( null, "Hier kommt message", "NIX", JOptionPane.INFORMATION_MESSAGE );
    }
  }

  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
}

/**
 * The class which generates the 'X' icon for the tabs. The constructor
 * accepts an icon which is extra to the 'X' icon, so you can have tabs
 * like in JBuilder. This value is null if no extra icon is required.
 */
class CloseTabIcon implements Icon {
  private int x_pos;
  private int y_pos;
  private int width;
  private int height;
  private Icon fileIcon;

  public CloseTabIcon(Icon fileIcon) {
    this.fileIcon=fileIcon;
    width=16;
    height=16;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    this.x_pos=x;
    this.y_pos=y;

    Color col=g.getColor();

    g.setColor(Color.black);
    int y_p=y+2;
    g.drawLine(x+1, y_p, x+12, y_p);
    g.drawLine(x+1, y_p+13, x+12, y_p+13);
    g.drawLine(x, y_p+1, x, y_p+12);
    g.drawLine(x+13, y_p+1, x+13, y_p+12);
    g.drawLine(x+3, y_p+3, x+10, y_p+10);
    g.drawLine(x+3, y_p+4, x+9, y_p+10);
    g.drawLine(x+4, y_p+3, x+10, y_p+9);
    g.drawLine(x+10, y_p+3, x+3, y_p+10);
    g.drawLine(x+10, y_p+4, x+4, y_p+10);
    g.drawLine(x+9, y_p+3, x+3, y_p+9);
    g.setColor(col);
    if (fileIcon != null) {
      fileIcon.paintIcon(c, g, x+width, y_p);
    }
  }

  public int getIconWidth() {
    return width + (fileIcon != null? fileIcon.getIconWidth() : 0);
  }

  public int getIconHeight() {
    return height;
  }

  public Rectangle getBounds() {
    return new Rectangle(x_pos, y_pos, width, height);
  }
}
 
Zurück