wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes SWINGS Over Use of Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "SWINGS Over Use of Vectors" Watch "SWINGS Over Use of Vectors" New topic
Author

SWINGS Over Use of Vectors

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have read that when SWING was released initially, because of what was available, it uses Vectors like crazy for components like JComboBox, JTree, JList, etc.
Will it ever be possble for Sun to re-write or at least extend the SWING API to allow for better performing Collection classes?
Or is it so ground in the base classes of SWING there by AWT, that this can never happen without a completely seperate SWING API (ie, SWING2). Which I wouldn't be totally against


GenRocket - Experts at Building Test Data
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Gregg,
Searching for "Vector" in source code of javax.swing and sub packages reveals more than 100 uses of Vector (But actually few are not using them at all, merely importing)
Many have private access so Sun can easily & safely replace such private Vector members.
For the protected Vectors as found in DefaultMutableTreeNode definitely Sun can't change the existing API for backward compatibility. In my opinion if such Vectors are not causing any performance issues then leave it as it is but if it causing, as in case of DefaultMutableTreeNode then they should provide with a version of MutableTreeNode handling data with List interface.
Actually we should file a suggestion. I have lots of suggestions i'll compile them and will post to Sun when i'll be free in the future
Let's Hope that Sun will look into this matter


The best teams have no specialists, only general contributors with special skills
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Gregg,
Sorry, i forgot the best solution to this problem and if it can be done then all the above suggestions should be ignored :-
In 1.4's version of Hashtable is actually (amazingly 2222) faster that HashMap (Recently there was this discussion in Performance forum) So sun can do similar and improve performance of Vector
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

They could, but will they....
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
As Vector is a subtype of Collection it wouldn't be a big deal to change the existing API to take Collection types instead of Vector types. This would be backward and forward compatible. Isn't that the point of OO-Programming?
What concerns the performance issue with JTable: the DefaultTableModel works with Vectors (unless I'm confusing something), while there exists a publication by sun people suggesting the use of HashMap for a good performance...
Chantal
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Chantel,
Let me tell u why i think there will be backward compatibility probs. Let's take example of DefaultMutableTreeNode this class has Vector as it's data member to store the child nodes. This member is protceted. According to sun's compatibility the class files which runs on earlier versions say 1.3 should also run on next version i.e. 1.4
Now if sun changes the Vector data member to List interface and the new implementation being ArrayList then there may probs with some client 1.3 class files as some clients may directly typecasting the data member to Vector which will result in ClassCastException
Right???
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Chental,
The previous example of type casting is not the best to explain. Let's see one more :-
We will create A.java which will have protected Vector member and declare method to it for adding and printing data. (the methods r for testing/edu purpose only so don't get into design issues) Then we will create a subclass of A called B which will override that method.
---------------------------
import java.util.*;
public class A
{
protected List data = new ArrayList();
public static void main(String[] args)
{
new A().addAndPrintData();
}
public void addAndPrintData()
{
for(int i = 0; i < 5; i++){
data.add(i + "");
}

System.out.println(data);
}
}
-------------------
import java.util.*;
public class B extends A
{
public static void main(String[] args)
{
new B().addAndPrintData();
}
public void addAndPrintData()
{
for(int i = 0; i < 15; i++){
data.add(i + "");
}
System.out.println(data);
}
}
------------------------
***********************
Now will compile both and then modify the data member Vector of A to point to List and compile A.java (and not B.java) and then we will run B
********************
List data = new ArrayList();
----------------------
Upon running we get foll error :-
Exception in thread "main" java.lang.NoSuchFieldError: data
at B.addAndPrintData(B.java:13)
at B.main(B.java:7)
-----------------------
Upon looking at B.class we can see the reason it expects Vector.
Well this may be very basic or simple to few here but am still in learning phase. :-)
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Ashish,
I understand your point but I don't agree. Obviously class B is not strictly object oriented as it should be. With object oriented classes the implementation logic is encapsulated. you would not refer to a field variable of another class but instead use a method to modify it or use an private instance field to mask the variable of the upper class.
what I wonder is - many people complain about swing and java being complex, containing bugs etc. but in the end everyone wants to hold on to old implementations (! - not APIs), urging sun to support old and new implementations in parallel. no wonder they can't maintain the whole framework as well as it would be best. I'd rather remodify my program just to know that it runs with a better, well supported java framework. and if everything is encapsulated as it should be after all, this should not be a problem.
one comment on your example: recompiling class B should solve the problem, you shouldn't even have to touch the code - isn't this a low price for a better software?
chantal
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Chantal Ackermann:
hi Ashish,
what I wonder is - many people complain about swing and java being complex, containing bugs etc. but in the end everyone wants to hold on to old implementations (! - not APIs), urging sun to support old and new implementations in parallel. no wonder they can't maintain the whole framework as well as it would be best. I'd rather remodify my program just to know that it runs with a better, well supported java framework. and if everything is encapsulated as it should be after all, this should not be a problem.
...isn't this a low price for a better software?
chantal

I agree completely with that.
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Perhaps Vectors are being used because their data elements can be serialized - hence useful when you are creating a visual java bean.
Just a thought...
[ March 04, 2003: Message edited by: Bhupinder Dhillon ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

How often do you use SWING components for a JAVA BEAN?
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
I've never worked on Swing but my impression is that's what JavaBeans were originally designed for visual beans! All these companies which make GUI development tools do use them heavily.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

With the definition of a JAVA BEAN that I have read and used, I see no reason to put a SWING Component inside a BEAN. Now, they may be creating a reusable component and use the JAVA BEAN term very loosly, but by deffinition, I don't see it happening.
But I don't want this topic to get away from the investigation of whether SUN will ever do anything about it.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
just for the records - any collection can contain serializable objects. this is not some special treat of Vector. the one obvious difference between Vector and ArrayList/LinkedList is its synchronization. but with the methods provided by Collections it is easy to create synchronized ArrayLists/LinkedLists according to programm needs.
Chantal
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Chental,
Sorry if it's 2 late. I was out of town
I understand your point but I don't agree. Obviously class B is not strictly object oriented as it should be. With object oriented classes the implementation logic is encapsulated. you would not refer to a field variable of another class but instead use a method to modify it or use an private instance field to mask the variable of the upper class.

I agree with this pt. But what i was trying to do is to simulate the situation. Unfortunately in swing's encapsulation principle and "always program to interface" principle has not been followed strictly. Hence we see
protected Vector as data member.
one comment on your example: recompiling class B should solve the problem, you shouldn't even have to touch the code - isn't this a low price for a better software?

According to Sun's Binary compatibility :- except for the noted incompatibilities, class files built with version 1.3 compilers will run correctly in the Java 2 SDK, v1.4.
I know that recompiling B.java should solve the probs but that would violate above stated Sun's binary compatibility. Cause the B.java compiled under JDK1.4 would expect Vector but running the same class file in JDK 1.5 (assuming that Sun changes the impl to List interface) without recompiling would generate error.
So let's hope that Sun improves the performance of Vector in their next release like they have done in case of Hashtables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SWINGS Over Use of Vectors
 
Similar Threads
exception while populating jtable from vectors
Q 4 H.Schildt (5): getters/setters instead of new classes.
Can a Vector be synchronized AGAIN????
java.util package and the Collection API
Implementing MultiDimensional Vectors