0

Consider two classes:

public class Test
{
    private boolean skip = false;
    private JLabel label = "";

    public boolean isSkipped()
    {
        return skip;
    }

    public void setSkipped(boolean skip)
    {
        this.skip = skip;
    }

    public void setLabel(String string)
    {
        label.setText(string);
    }
}

and MainWindow class that we assume that has all fields and methods required for simple frame to be displayed. In this class we also have:

private JTabbedPane tabPane = new JTabbedPane();
private ArrayList<Test> test = new ArrayList<>();

for(int i = 0; i < 30; i++)
{
    test.add(new Test());
}

JPanel checkBoxTab = new JPanel();
JCheckBox checkBox = new JCheckBox();

checkBox.addItemListener(new ItemListener()
{
    @Override
    public void itemStateChanged(ItemEvent e)
    {
        if(e.getStateChange() == ItemEvent.SELECTED)
        {
             test.get(?).setSkipped(true);
        }

        if(e.getStateChange() == ItemEvent.DESELECTED)
        {
             test.get(?).setSkipped(false);
        }

        for(int j = 0, k = 1; j < test.size(); j++)
        {
             if(test.get(j).isSkipped())
             {
                   continue;
             }

             test.get(j).setLabel("" + k);
             k++;
         }
   }
});

   checkBox.setOpaque(false);
   checkBoxTab.add(checkBox);
   checkBoxTab.setOpaque(false);

for(int i = 0; i < test.size(); i++)
{
   tabPane.addTab("", test.get(i));
   tabPane.setTabComponentAt(i, checkBoxTab);
}

My question is: how do I replace that ? sign with index of tab that has the chackbox that was selected? I need it to be found dynamically during program run. The purpose of this code is to find out which tab was selected to be skipped, skip it without assigning next number of k to a label and assign next number of k to label in next tab (assuming it's not marked to be skipped, and if it is - repeat previous process). I tried to understand solution in here Close the clicked tab, not the currently selected tab JTabbedPane but I don't really see how could I implement the "tab content" thing. Depicting previous we have:

tabCount: 0 1 2 3 4 5 6 7 8
skip:     f t f t t f f f t
label:    1   2     3 4 5

EDIT sorry there was a typo in MainWindow class

Community
  • 1
  • 1
Colonder
  • 1,556
  • 3
  • 20
  • 40
  • What relationship does the `JCheckBox` have to the tab? Are they contained in the tab header? The tab itself? Or are the separate? – MadProgrammer May 01 '16 at 23:36
  • tab header like in here in the section Tabs with custom components https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html – Colonder May 01 '16 at 23:39
  • Conceptually, you could do something like [this](http://stackoverflow.com/questions/11553112/how-to-add-close-button-to-a-jtabbedpane-tab/11553266#11553266) – MadProgrammer May 01 '16 at 23:53
  • Hmm, gonna try that tommorow, thanks – Colonder May 02 '16 at 00:00

1 Answers1

0

With help of Mad Programmer's comment I managed to implement what I wanted

checkBox.addItemListener(new ItemListener()
{
    @Override
    public void itemStateChanged(ItemEvent e)
    {
        if(e.getStateChange() == ItemEvent.SELECTED)
        {
            test.get(tabbedPane.indexOfTab(tabName)).setSkipped(true);
        }

        if(e.getStateChange() == ItemEvent.DESELECTED)
        {
            test.get(tabbedPane.indexOfTab(tabName)).setSkipped(false);
        }

        for(int j = 0, k = 1; j < test.size(); j++)
        {
            if(test.get(j).isSkipped())
            {
                continue;
            }

            test.get(j).setLabel("" + k);
            k++;
        }
    }
});
Colonder
  • 1,556
  • 3
  • 20
  • 40