2012-12-20 203 views
0

这个问题不一定是特定于JTabbedPane,但这是我碰到它的地方。这个想法是,你有一个优先级列表Priority[]根据你想看到输出制表符被渲染。每个选项卡可以有多个与给定优先级关联的对象,并且我希望在为其检测到对象的第一个时生成选项卡。这些对象是由外部服务生成的,它们可以以任何顺序出现。所以,假设您有JTabbedPane - 保持标签顺序

[Low, Medium, Warning, Alert, Crash] 

作为您的优先级列表。现在,这是我想要生成的

[] -> [] 
[Alert] -> [Alert] 
[Alert, Alert] -> [Alert] 
[Alert, Alert, Low] -> [Low, Alert] 
[Alert, Alert, Low, Medium] -> [Low, Medium, Alert] 

RHS是制表符的顺序,LHS是未来的优先事项。任何想法如何我可以动态地实现这一点?

回答

1

事情是这样的:

List<String> priorities = Arrays.asList("Low", "Medium", "Warning", "Alert", "Crash"); 
Map<List<String>> data = new LinkedHashMap<List<String>>(); 
for (String priority : priorities) 
    data.put(priority, new ArrayList<String>()); 

... 

传入数据:

String priority = ...; 

List<String> list = data.get(priority); 
list.add("my new data"); 

if (list.size() == 1) { 
    //insert tab 
    int index = 0; 
    int priorityIndex = priorities.indexOf(priority); 
    while (priorityIndex > priorities.indexOf(tabbedPaned.getTitleAt(index))) { 
    index++; 
    } 
    tabbedPane.insertTab(priority, ... , index); //includes index where to insert the tab 
} 

//update tabs 
... 
+0

是的,我明白了,我如何找到放置它的索引?这是我失踪的整个招数 – Bober02

+0

我认为你应该优化你的问题,然后索要索引。有几种方法,一种简单的方法是使用标签的标题。只是将该信息存储在单独的数据结构中。更新我的回答 –

+0

现在索引@ Bober02 –

2

您可以调用JTabbedPane上的insertTab方法将新选项卡插入指定的位置。

insertTab(title, icon, panel, title, index); 

因此,您可以遍历每个选项卡并根据优先级将其插入到指定索引处。

+0

什么叫 “循环通过标签” 是什么意思? – Bober02

+0

不确定您的要求是什么,但是可以随时添加标签或将它们全部一次性添加,例如当页面加载? – maloney