|
|
||
Alexander Potochkin's BlogNovember 2005 ArchivesDebugging Swing - is it really difficult ?Posted by alexfromsun on November 23, 2005 at 01:57 AM | Permalink | Comments (33)Every experienced Swing developer knows that Swing components must be accessed from Event Dispatch Thread (EDT) only. Working with JComponents from any other thread may lead to unpredictable result. Consider the following code: import javax.swing.*; import java.awt.*; Running this code I get the desirable unselected JEdiorPane, ... sometimes
Sometimes I get it like this:
Working with Swing not from EDT, you might see your components and layouts distorted or not reflected the current state. Actually the main rule is It's been so many articles like this and this, so I suppose that most of Swing programmers know how it works and do the right thing. Our previous example might be fixed this way: public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGui();
}
});
}
The fact that every JComponent's listener is automatically invoked on EDT makes it even more easy. In my opinion, understanding that Swing is a single-threaded library is not difficult Funny thing, I took part in interviewing several java programmers who claimed to know Swing well, but at the same time some of them had no idea what EDT is. That's why I am really interested in your opinion, Set of questions
Any comments would be appreciated! I also posted new topic to javadesktop forum Thanks TabComponents in actionPosted by alexfromsun on November 09, 2005 at 10:51 AM | Permalink | Comments (7)It's been a lot of publications about adding "close" button to a tabbedPane, all solutions suggested are really inventive and not easy to find. But we are gonna talk about Mustang "tabComponents feature" because it is the most preferable and clear solution for JTabbedPane customizing. Since 1.6 three new methods added to JTabbedPane: we can put a component to a tab with help of get this component with and test if a component is a tabComponent for this JTabbedPane with With this three methods we can enrich a JTabbedPane with comboboxes, multiple labels or whatever we want. Please download source files and go on. First, let's check how we can add the most wanted “close” buttons to a JTabbedPane TabComponent with a “close” button(source files in the blog.test1 package)
Here how it looks:
The only new thing is the close button's actionListener:
public void actionPerformed(ActionEvent e) {
int i = pane.indexOfTabComponent(ButtonTabComponent.this);
if (i != -1) {
pane.remove(i);
}
}
When we click to this button we find the index of the tab this button belongs to and close the tab. The only possible problem is that this tabComponent doesn't respect actual JTabbedPane's titles so if we remove all tabComponents
Notice that actual tab's titles looks like tab1 not like tabComponent1. In the next example we'll easily create better tabComponent which can show the actual titles. JRadioButton as a tabComponent(source files in the blog.test2 package)
JRadioButton button = new JRadioButton() {
//This way we always have up-to-date tab's title
public String getText() {
int i = pane.indexOfTabComponent(this);
if (i != -1) {
return pane.getTitleAt(i);
}
return null;
}
};
our radioButton will alway show the actual tab's title. We only need to add one listener to radioButtons to select a tab when we select a JRadioButton Crazy tabComponent with two icons(source files in the blog.test3 package)
We find the acual icon just like we find titles for the previous example ConclusionTabComponents make it possible to customise JTabbedPane component quite easily, let me give you some recommendations
Thanks | ||
|
|