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