JTabbedPane getSelectedIndex always -1

T

tomzhi

Somehow I can only get the SelectedIndex inside main method. I keep
getting "-1" when I try to get it from other method. I am not sure if
it's because I create the tabs inside a for loop in the method. Please
advise. Thanks



class {

declare jpanel
declare jtabbedpane


main{

for loop {
create jtabbedpane (name[], panel[]);

}

addlistener to submitButton;

System.out.println(jtabbedpane.getSelectedIndex()); //
this works
}




submitButtonActionListener {
System.out.println(jtabbedpane.getSelectedIndex()); //this
always return -1

}

}
 
N

Nigel Wade

tomzhi said:
Somehow I can only get the SelectedIndex inside main method. I keep
getting "-1" when I try to get it from other method. I am not sure if
it's because I create the tabs inside a for loop in the method. Please
advise. Thanks



class {

declare jpanel
declare jtabbedpane


main{

for loop {
create jtabbedpane (name[], panel[]);

}

addlistener to submitButton;

System.out.println(jtabbedpane.getSelectedIndex()); //
this works
}




submitButtonActionListener {
System.out.println(jtabbedpane.getSelectedIndex()); //this
always return -1

}

}

You need to post real code. The problem is in the code which you haven't shown
us. The best we can do is guess, which may well be a waste of both your and our
time.

However, given that you are creating jtabbedpane in a loop it's a fair bet that
there is more than one JTabbedPane. Which one of them do you think jtabbedpane
refers to in the action listener?
 
L

Lew

tomzhi said:
Somehow I can only get the SelectedIndex inside main method. I keep
getting "-1" when I try to get it from other method. I am not sure if
it's because I create the tabs inside a for loop in the method. Please
advise. Thanks

class {

declare jpanel
declare jtabbedpane

main{

for loop {
create jtabbedpane (name[], panel[]);
}

addlistener to submitButton;

System.out.println(jtabbedpane.getSelectedIndex()); //
this works
}

submitButtonActionListener {
System.out.println(jtabbedpane.getSelectedIndex()); //this
always return -1
}
}

Nigel said:
You need to post real code. The problem is in the code which you haven't shown
us.

Given that they haven't shown us any code at all, it's a fair bet that you're
right, Nigel.
 
T

tomzhi

This is a unit converter I am trying to create. Loadtable is class
that loads the data to create the dropdown list on different tab.
I used for loop to create arrays of panels, which becomes tabs. Also
arrays of buttons, dropdown list.

Now, I need to use submit button to call a function to calculate
the conversion factor. However, I can't define which tab the user has
currently selected. I can do it only within the main method, but not
outside. It would become -1 outside. Please help.


import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.*;

public class Main {
JTabbedPane tab = new JTabbedPane();

int i, index;
JComboBox[] box = new JComboBox [10];
JComboBox[] box_2 = new JComboBox [10];
JTextField[] text = new JTextField[10];
JTextField[] text_2 = new JTextField[10];
JPanel [] panel = new JPanel[10];
JButton[] exit = new JButton[10];
JButton[] submit = new JButton[10];




public Main() {
}

public static void main(String[] args) throws
FileNotFoundException, IOException {
JTabbedPane tab = new JTabbedPane();
int i;
JComboBox[] box = new JComboBox [10];
JComboBox[] box_2 = new JComboBox [10];
JTextField[] text = new JTextField[10];
JTextField[] text_2 = new JTextField[10];
JPanel [] panel = new JPanel[10];
JButton[] exit = new JButton[10];
JButton[] submit = new JButton[10];

String [] name = {"Distance",
"Area","Temperature","Speed","Volume",
"Density","Force","Energy","Frequency","Astronomical"};

loadtable x =new loadtable();
x.load();
for (i=0;i<10;i++) {
panel=new JPanel();
box= new JComboBox(x.getdata(name));
box_2=new JComboBox(x.getdata(name));
text= new JTextField();
text_2= new JTextField();
exit=new JButton("Exit");
submit= new JButton("Submit");
text.setColumns(10);
text_2.setColumns(10);
panel.add(text);
panel.add(box);
panel.add(text_2);
panel.add(box_2);
panel.add(submit,BorderLayout.PAGE_END);
panel.add(exit,BorderLayout.PAGE_END);
tab.add(name,panel);

}


JFrame frame = new JFrame("Unit Converter");
frame.add(tab);
frame.setSize(750,200);
frame.setLocation(200,200);
frame.setVisible(true);

for (i=0;i<10;i++) {
exit.addActionListener(new
java.awt.event.ActionListener () {
public void actionPerformed(java.awt.event.ActionEvent
evt){
exitActionPerformed(evt);
}
});


}

for (i=0;i<10;i++) {
exit.addActionListener(new
java.awt.event.ActionListener () {
public void actionPerformed(java.awt.event.ActionEvent
evt){
exitActionPerformed(evt);
}
});


}

for (i=0;i<10;i++) {
submit.addActionListener(new
java.awt.event.ActionListener () {
public void actionPerformed(java.awt.event.ActionEvent
evt){
submitActionPerformed(evt);
}
});


}


}
private static void
exitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private static void
submitActionPerformed(java.awt.event.ActionEvent evt) {
Main x = new Main();
System.out.println(x.tab.getSelectedIndex());


}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,808
Messages
2,569,684
Members
45,448
Latest member
DebraMurie

Latest Threads

Top