mkunkel
mkunkel

Reputation: 263

Adding to 2D tabbed JTabbedPane

I am trying to create a 2dimesional JTabbedPane. I found a small example at coderanch to create the JTabbedPane in 2 dimensions. What I would like to do now is add a figure to each tab from an external class after creation of the table. Meaning, I create this table, then place the figures I want inside the table (maybe user does not know needed order beforehand, just knows number of figures to use). But I cannot seem to get the tab to update. I was hoping someone could show me what I am doing incorrectly. Here is a MWE for adding text (instead of a figure).

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class DCTabbed2DPane {
  private Dimension screensize;
  private JFrame frame;
  private JTabbedPane topTabbedPane;
  private JTabbedPane[] leftTabbedPane;
  private int rows;
  private int cols;

  private String frameName;

public DCTabbed2DPane(String frameName, int rows, int cols) {
    this.frameName = frameName;
    this.rows = rows;
    this.cols = cols;
    init();
}

private void setScreenSize() {
    screensize = Toolkit.getDefaultToolkit().getScreenSize();
}

private void setJFrame() {
    frame = new JFrame(frameName);
    frame.setSize((int) (screensize.getHeight() * .75 * 1.618), (int) (screensize.getHeight() * .75));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void setJTabbedPane() {
    topTabbedPane = new JTabbedPane(JTabbedPane.TOP);
    leftTabbedPane = new JTabbedPane[cols];

}

private void setTabs() {
    for (int i = 0; i < cols; i++) {
        leftTabbedPane[i] = new JTabbedPane(JTabbedPane.LEFT);
        for (int j = 0; j < rows; j++) {
            leftTabbedPane[i].addTab("Super Layer " + (j + 1), new JLabel("Sector " + (i + 1) + " - " + (j + 1)));
        }
        topTabbedPane.addTab("Sector " + (i + 1), leftTabbedPane[i]);
    }
    frame.add(topTabbedPane);
    topTabbedPane.setSelectedIndex(-1);
}

private void init() {
    setScreenSize();
    setJFrame();
    setJTabbedPane();
    setTabs();
}

public void addCanvasToPane(Integer row, String str) {
    leftTabbedPane[row].addTab("", new JLabel(str));
    frame.add(topTabbedPane);
    frame.revalidate();
    frame.repaint();
}

public void showFrame() {

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
    int rows = 6;
    int cols = 6;

    DCTabbed2DPane test = new DCTabbed2DPane("PooperDooper", rows, cols);
    for (int j = 0; j < cols; j++) {
        test.addCanvasToPane(j, "Name " + j);
    }
    test.showFrame();

}

}

Upvotes: 0

Views: 152

Answers (1)

Fevly Pallar
Fevly Pallar

Reputation: 3109

It's either you want the figure on the tab's navigation, like

 leftTabbedPane[i] = new JTabbedPane(JTabbedPane.LEFT);
            for (int j = 0; j < rows; j++) {
                leftTabbedPane[i].addTab("Super Layer " + (j + 1),new ImageIcon(getClass().getResource("Images/map.png")),  new JLabel("Sector " + (i + 1) + " - " + (j + 1)));
            }

enter image description here

Or like this one

leftTabbedPane[i] = new JTabbedPane(JTabbedPane.LEFT);
            for (int j = 0; j < rows; j++) {
                leftTabbedPane[i].addTab("Super Layer " + (j + 1), new MyLabel("Images/Horse.png","").getLabel());         
            }

where MyLabel is

public class MyLabel {
    JLabel label;
    String path;
    String text;

    public MyLabel(String path, String text) {
        this.label = new JLabel();
        this.path = path;
        this.text = text;      
    }
    public void setUpImage(String path) {
        this.label.setIcon(new ImageIcon(getClass().getResource(path)));
    }

    public JLabel getLabel() {
      setUpImage(this.path);
        return this.label;
    }

}

enter image description here

Upvotes: 1

Related Questions