0

This problem is not necessarily specific to JTabbedPane, but this is where I came across it. The idea is that you have a list of priorities Priority[] according to which you would like to see the output tabs to be rendered. Each tab can have many objects associated with a given priority, and I want to generate the tab as the first object is detected for it. The objects are generated by external service and they may come in any order. So, let's say you have

[Low, Medium, Warning, Alert, Crash]

as your list of priorities. Now, this is what I want to generate

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

the RHS is the order of tabs and the LHS is the coming priorities. Any ideas how I can dynamically achieve this?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Bober02
  • 15,034
  • 31
  • 92
  • 178

2 Answers2

2

You can call the insertTab method on your JTabbedPane to insert your new tab at the specified position.

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

So you can loop through each tab and insert it at the specified index based on the priority.

maloney
  • 1,633
  • 3
  • 26
  • 49
1

Something like this:

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>());

...

Incoming data:

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
...
Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • yes, I get that all, how do I find the index at which to place it? that is the whole trick I am missing – Bober02 Dec 20 '12 at 17:06
  • I think you should refine your question then to ask for the index. There are several way, one easy way is to use the title of the tabs. r just store that info in a separate data structure. Updating my answer – Mattias Isegran Bergander Dec 20 '12 at 17:18