-1

I want to push/call the selected tab index value from the StateChanged method to another class or private method, How can I do this,

    private class TabSelect implements ChangeListener {
    @Override
    public void stateChanged(ChangeEvent e) {
        JTabbedPane source = (JTabbedPane) e.getSource();
        if (source.getSelectedComponent() != null) {
            source.getSelectedIndex();

        }

    }
}

I want to push this index value to the following method (or another public class in the same package). How to do this?

private JPanel CreateSlice() {

        JPanel Slice = new JPanel();
        Slice.setPreferredSize(new Dimension(550, 600));
        Slice.add(button);
        return Slice;

    }

This is the CreateSlice's function,

private class TabPlus implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        JPanel panel = CreateSlice();
        String title = "Slice " + String.valueOf(pane.getTabCount());
        pane.insertTab(title, null, panel, null, pane.getTabCount() - 1);
    }
}

2 Answers2

0
@Override
public void stateChanged(ChangeEvent e) {
    JTabbedPane source = (JTabbedPane) e.getSource();
    if (source.getSelectedComponent() != null) {
       int index =  source.getSelectedIndex();
       CreateSlice(index);
    }

}



public JPanel CreateSlice(int index) {
        //do whatever you want with index
        JPanel Slice = new JPanel();
        Slice.setPreferredSize(new Dimension(550, 600));
        Slice.add(button);
        return Slice;

    }

note that your CreateSlice method should be public

Damith
  • 740
  • 4
  • 12
  • But I am calling this CreateSlice to create panel in ActionListener method, if I set int index as parameter, then it shows error there, I am giving that part in my question, – nothingSpecial Mar 03 '16 at 09:25
0
int index =  source.getSelectedIndex(); // Save it to variable

createSlice(index); // pass it into new method, follow camelCase for bestPractice

 private JPanel createSlice(int index) {
   //your implementation
 }
RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24
  • But My CreateSlice is being used by ActionListener to create Panels. if I set int index as parameter, it shows error there. – nothingSpecial Mar 03 '16 at 09:31
  • so what do you want to do with your index, create other public method in the class . – RishiKesh Pathak Mar 03 '16 at 09:36
  • Thank you so much. I have a situation since I don't know how to do this, There's a addTab method to add tab using a plus button, this method is calling CreateSlice method to create tab panels. Now I want to create some components on those tab panels. These panels might have different components in different tabs. Could I explain you well ? How can I do this, any idea? – nothingSpecial Mar 03 '16 at 09:44