Search code examples
javaswingjtabbedpaneimageicon

Adding buttons into tabs


I have tabs in a tabbed pane that use a custom class called FilePanel and that is what makes this a little confusing. It is for saving and loading files hence the class File Panel. I want to add a little button of an icon to the right in each of the tabs. when clicked it should close that specific tab.

File Panel Class:

package texteditor;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.io.File.*;
import java.awt.*;
import javax.swing.filechooser.FileNameExtensionFilter;

class FilePanel extends JPanel {

   private File file;
   private JTextArea textArea;
   private String name;


   public FilePanel(File file) throws FileNotFoundException, IOException {
      this.file = file;
      setLayout(new BorderLayout());

      textArea = new JTextArea();
      JScrollPane scroll = new JScrollPane(textArea);
      add(scroll, BorderLayout.CENTER);

      textArea.read(new FileReader(file.getAbsolutePath()), null);
      name = file.getName();


   }

   public FilePanel()  {

       try
       {
      setLayout(new BorderLayout());

      textArea = new JTextArea();
      JScrollPane scroll = new JScrollPane(textArea);
      add(scroll, BorderLayout.CENTER);

      textArea.read(new FileReader(file.getAbsolutePath()), null);
      name = file.getName();


       }
       catch(Exception e)
       {
           System.out.println(e);
       }

   }

   public File getFile() {
      return file;
   }

   public JTextArea getTextArea() {
      return textArea;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}

Adding new frame method:

private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {                                       
       increment++; 

       try
       {
          FilePanel p = new FilePanel();
          p.setName("File " + increment);

          //code im testing goes here and I just remove the 3 you see below.
          tabbedPane.add(p);
          tabbedPane.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
          tabbedPane.setSelectedComponent(p);

       }
       catch(Exception e)
       {
           System.out.println(e);
       }
    }  

Code that I am trying but to no avail:

Icon icon = new ImageIcon(getClass().getResource("redclose.png"));
         JButton butt = new JButton();
         butt.setIcon(icon);
         butt.setIconTextGap(5);
         butt.setHorizontalTextPosition(SwingConstants.RIGHT);
         tabbedPane.setTabComponentAt(0, butt);

This works for just the icon itself:

Icon icon = new ImageIcon(getClass().getResource("redclose.png"));
          tabbedPane.setIconAt(tabbedPane.getSelectedIndex(), icon);

Solution

  • Here is a quick example of what the closable tab would look like:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.border.EmptyBorder;
    
    public class ClosableTab extends JPanel implements ActionListener{
    
        private final JTabbedPane pane;
        private final int index;
    
        private JButton closeButton;
    
        private JLabel titleLabel;
    
        public ClosableTab(final JTabbedPane pane, final int index){
            super(new BorderLayout());
            setOpaque(false);
    
            this.pane = pane;
            this.index = index;
    
            titleLabel = new JLabel(pane.getTitleAt(index));
            titleLabel.setHorizontalAlignment(JLabel.CENTER);
    
            closeButton = new JButton("X");
            closeButton.setBorder(new EmptyBorder(-5, 2, -5, 0));
            closeButton.setOpaque(false);
            closeButton.setContentAreaFilled(false);
            closeButton.setFont(closeButton.getFont().deriveFont(20F));
            closeButton.setForeground(Color.RED);
            closeButton.addActionListener(this);
    
            add(titleLabel, BorderLayout.CENTER);
            add(closeButton, BorderLayout.EAST);
    
            setPreferredSize(getPreferredSize());
        }
    
        public void actionPerformed(ActionEvent e){
            final Object source = e.getSource();
            if(source.equals(closeButton)){
                pane.remove(pane.indexOfTabComponent(this));
                pane.revalidate();
                pane.repaint();
            }
        }
    
        public void apply(){
            pane.setTabComponentAt(index, this);
            pane.revalidate();
            pane.repaint();
        }
    
        public static void apply(final JTabbedPane pane, final int index){
            new ClosableTab(pane, index).apply();
        }
    
    }
    

    After you add your FilePanel, try invoking ClosableTab.apply(tabbedPane, tabbedPane.getTabCount()-1);