I am trying to dynamically add tabs to a JTabbedPane i do this with the following code:
private tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
public void newTab(String tab){
JPanel panel1 = new JPanel();
JTextArea tArea = new JTextArea();
panel1.add(tArea);
tab.add(tab, panel1);
}
The problem is that I need the text area name to be viewable through out the class so i can append to it later and there it should be able to lots of tabs so each text area should have a different name.
Any Ideas?
You should use a List
of JTextArea
s.
private final List<JTextArea> areas = new ArrayList<JTextArea>();
public void newTab(String tab){
JPanel p = new JPanel();
JTextArea tArea = new JTextArea();
p.add(tArea);
tab.add(tab, p);
areas.add(tArea);
}
or even a Map
(only if the Tab titles are unique).
private final Map<String, JTextArea> areas = new HashMap<String, JTextArea>();
public void newTab(String tab){
JPanel p = new JPanel();
JTextArea tArea = new JTextArea();
p.add(tArea);
tab.add(tab, p);
areas.put(tab, tArea);
}
You have a name clash issue
public void newTab(String tab){ // <-- tab declared as String here
JPanel panel1 = new JPanel();
JTextArea tArea = new JTextArea();
panel1.add(tArea);
tab.add(tab, panel1); // <-- tab (String) doesn't have an add method - error~~
}
Try something like:
public void newTab(String tabName){
JPanel panel1 = new JPanel();
JTextArea tArea = new JTextArea();
panel1.add(tArea);
tab.add(tabName, panel1); // <-- Now we know which tab we're talking about ;)
}
Instead
Once you have that resolved, the suggested use of Map
by @mercutio should work fine
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.