How to access the variables inside a class which has been called more than once as a tab in JTabbedPane (Java)
I am creating a java app.
I have one class q2a2 which is a jpanel whose design is shown as follows: -img-
Suppose if an item is selected from the combo-box and "Create Account" button is clicked. One tab is added to the jTabbedPane. every item has a common tab. so what i did is created one class and adding that everytime on button click. The class name is q2a2_add. This is a panel as well. The image for this is as follows...
After having some three items the app looks like
The code for this is:
public void addclass(int a) {
if(jTabbedPane1.getTabCount()<13) { //variable name of TabbedPane
String s=(String) mainCB.getItemAt(a); //mainCB is the variable name of main combobox
int dont=0;
for(int j=0;j<tabname.length;j++){ //just to ensure two accounts should not be same
if(s.equals(tabname[j])){
dont=1;
break;
}
}
if(dont==0){
for(int j=0;j<12;j++) {
if(index[j]==0){
q2a2_add subpanel2=new q2a2_add(this); //calling the second class
jTabbedPane1.add(s,subpanel2); //here adding panel
subpanel2.heading(s); // heading开发者_StackOverflow() method is defined in q2a2_add() which rename the jTextField to be same as argument s;
tabname[j]=s;
index[j]=1;
break;
}
}
}
else {
JOptionPane.showConfirmDialog(null, (String) mainCB.getItemAt(a)+" is already created","Information", JOptionPane.PLAIN_MESSAGE);
}
}
else {
JOptionPane.showConfirmDialog(null, "Account Overload. Delete wrong account and then create","Caution", JOptionPane.PLAIN_MESSAGE);
}
}
Now my question is. As seen in the function. everytime same class has been called and added. How can i access the various comboboxes and textboxes in different tabs. I want to store and opearate values entered by the user. Like for example- how to read inputs from Accounts Receivable, Accounts Payable and Office Supplies differently.
Please reply.
I would expose the functionality that you require within your q2a2_add
class. For instance, if you want to change the textbox value, add a function inside the q2a2_add
class called setTextBoxValue()
that takes a String
parameter. Inside that function, you can set the textbox value. The same goes for retrieving information from it. The only remaining problem is how to keep track of the different tabs. What I would recommend (which may simplify what you already have) is to create a HashMap which maps String
types to q2a2_add
types. Then, when you want to add a new tab panel, you can just check if the String
exists in the HashMap instead of searching through to check the titles. If it doesn't exist, you can add it to a HashMap
stored inside of your outer JPanel
class. Then, when you want to access the tabpanels, you can simply access them by string inside the HashMap
and get/set their properties as you please.
精彩评论