Ola pessoal, gostaria de criar uma janela "Swing" em que eu pudesse criar e fechar as abas, como um navegado. Onde eu consegui até criar as abas.
O meu problema é que não consigo fechar essas abas. Vejam:
Arquivo Principal
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import net.miginfocom.swing.MigLayout;
public class Start {
private JFrame frmScanfaaSistema;
private TabbedPaneImagens tabbedPaneImagens;
public Start() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmScanfaaSistema = new JFrame();
frmScanfaaSistema.setTitle("SCANFaa - Sistema de Calculo Num\u00E9rico da FAA");
frmScanfaaSistema.setBounds(100, 100, 800, 600);
frmScanfaaSistema.setResizable(true);
criarMenu();
frmScanfaaSistema.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmScanfaaSistema.getContentPane().setLayout(new MigLayout("", "[]", "[]"));
this.tabbedPaneImagens = new TabbedPaneImagens();
frmScanfaaSistema.getContentPane().add(this.tabbedPaneImagens, "w 100%, h 100%");
}
private void abrirArquivo() {
this.tabbedPaneImagens.addTab();
}
private void sair() {
int i = this.tabbedPaneImagens.getTabCount();
boolean fim = false;
while (this.tabbedPaneImagens.getTabCount() > 0 && !fim) {
this.tabbedPaneImagens.remove(0);
if (i != this.tabbedPaneImagens.getTabCount()) {
i = tabbedPaneImagens.getTabCount();
} else {
fim = true;
}
}
if (i == 0) {
System.exit(0);
}
}
// Métodos que criam Menu
private JMenuBar criarMenu(){
JMenuBar menuBar = new JMenuBar();
frmScanfaaSistema.setJMenuBar(menuBar);
JMenu mnArquivo = new JMenu("Arquivo");
menuBar.add(mnArquivo);
JMenuItem mntmNovoGrfico = new JMenuItem("Novo Gr\u00E1fico");
mntmNovoGrfico.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
abrirArquivo();
}
});
mntmNovoGrfico.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
mnArquivo.add(mntmNovoGrfico);
JMenuItem mntmSair = new JMenuItem("Sair");
mntmSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sair();
}
});
mntmSair.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
mnArquivo.add(mntmSair);
JMenu mnMetodos = new JMenu("Metodos");
menuBar.add(mnMetodos);
JMenuItem mntmDicotomia = new JMenuItem("Dicotomia");
mntmDicotomia.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK));
mnMetodos.add(mntmDicotomia);
JMenu mnAjuda = new JMenu("Ajuda");
menuBar.add(mnAjuda);
JMenuItem mntmSobre = new JMenuItem("Sobre ");
mntmSobre.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
mnAjuda.add(mntmSobre);
JMenuItem mntmCrditos = new JMenuItem("Cr\u00E9ditos");
mnAjuda.add(mntmCrditos);
return menuBar;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Start window = new Start();
window.frmScanfaaSistema.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TabbedPaneImagens getTabbedPaneImagens() {
return tabbedPaneImagens;
}
public void setTabbedPaneImagens(TabbedPaneImagens tabbedPaneImagens) {
this.tabbedPaneImagens = tabbedPaneImagens;
}
}
TabbedPane.java
package br.com.AngloAmericano.SCANFaa.view;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import net.miginfocom.swing.MigLayout;
public class TabbedPane extends JTabbedPane {
private static final long serialVersionUID = 1L;
private Color cor;
public TabbedPane() {
super();
this.cor = new Color(0, 0, 0);
this.setVisible(false);
}
public void addTab() {
ImageIcon imageIcon = new ImageIcon();
final JLabel label = new JLabel();
label.setIcon(imageIcon);
final JPanel painel = new JPanel();
painel.setLayout(new MigLayout());
painel.add(label);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseMoved(e);
}
});
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
super.mouseClicked(e);
}
});
int i = this.getTabCount() +1;
JScrollPane scrollPane = new JScrollPane(painel);
this.addTab( "Função "+ i, scrollPane);
int index = this.getTabCount() - 1;
this.setTabComponentAt(index, new PainelBotaoFechar(this));
this.setSelectedIndex(index);
this.setVisible(true);
}
@Override
public void remove(int i) {
if (getTabCount() == 0) {
this.setVisible(false);
}
}
public Color getCor() {
return this.cor;
}
}
PainelBotãoFechar.java
package br.com.AngloAmericano.SCANFaa.view;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;
public class PainelBotaoFechar extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JTabbedPane pane;
private JButton botao;
public PainelBotaoFechar(final JTabbedPane pane) {
super(new MigLayout("", "0[]0[]0", "0[]0"));
if (pane == null) {
throw new NullPointerException("TabbedPane é null");
}
this.pane = pane;
setOpaque(false);
JLabel labelTitulo = new JLabel() {
public String getText() {
int i = pane.indexOfTabComponent(PainelBotaoFechar.this);
if (i != -1) {
return pane.getTitleAt(i);
}
return null;
}
};
add(labelTitulo);
labelTitulo.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
botao = new BotaoFechar();
add(botao);
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
private class BotaoFechar extends JButton implements ActionListener {
public BotaoFechar() {
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("Fechar");
setUI(new BasicButtonUI());
setContentAreaFilled(false);
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
setRolloverEnabled(true);
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int i = pane.indexOfTabComponent(PainelBotaoFechar.this);
if (i != -1) {
System.out.println(i);
pane.remove(i);
}
}
@Override
public void updateUI() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
if (getModel().isPressed()) {
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(2));
if (getModel().isRollover()) {
g2.setColor(Color.RED);
int delta = 5;
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
g2.dispose();
}
}
}
}
Pessoal, talvez seja um problema bobo, mais infelizmente não estou conseguindo resolver;
Desde já agradeço a ajuda de todos