0

I´ve got a JTabbedPane with JTextArea. An methode creates another tab an adds it to the JTabbedPane. Now I want to implement, that the new created tab gets another color untill it is opened the first time, like in a Chatroom to show that theres a new message from a specific user. I don´t really know how do implement this. I thried to use a while-loop, but it didn´t work

String name = "...";
JTabbedPane tabs = new JTabbedPane();
JTextArea textarea = new JTextArea();
textarea.setEditable(false);
textarea.setLineWrap(true);
JScrollPane jScrollPane = new JScrollPane(textarea);
jScrollPane.setPreferredSize(new Dimension(300, 300));
tabs.add(name, jScrollPane);
tabs.setBackgroundAt(tabs.indexOfTab(name),Color.GREEN);
        while(true){
            if(tabs.getSelectedIndex() == tabs.indexOfTab(name)){
                tabs.setBackgroundAt(tabs.indexOfTab(name),Color.GRAY);
                break;
            }
        }

1 Answers1

2

Starting from this example, the following changes produce the effects illustrated below. Each tab starts as Color.lightGray, and a ChangeListener changes the background color to Color.red.darker() the first time each tab is selected. You can so something similar in a listener to your application's data model; several approaches are examined here.

public TabColors() {
    for (int i = 0; i < MAX; i++) {
        Color color = Color.lightGray;
        pane.add("Tab " + String.valueOf(i), new TabContent(i, color));
        pane.setBackgroundAt(i, color);
    }
    pane.setSelectedIndex(-1);
    pane.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            pane.setBackgroundAt(pane.getSelectedIndex(), Color.red.darker());
        }
    });
    this.add(pane);
}

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • What is pane? Is it a JTabbedPane? Because I cant use a ChangeListener for a JTabbedPane –  Jul 11 '16 at 08:02
  • _Starting from this [example](http://stackoverflow.com/a/8752166/230513)_, yes. Depending on your application's data model, you may want to use a different listener. – trashgod Jul 11 '16 at 08:14
  • And which one should I use? –  Jul 11 '16 at 08:39
  • That depends on your application's data model. What listeners does it support? – trashgod Jul 11 '16 at 08:42
  • It is an JTabbedPane with scrollable Textareas. The tabs have got JPanels with the name of the tab and a close Button –  Jul 11 '16 at 08:51
  • That's the view; how does your application's data model notify view(s) of new data? – trashgod Jul 11 '16 at 08:53