David Lu

Greenhorn
+ Follow
since Aug 01, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David Lu

Thanks for the advice. After posting my reply, I realized myself what a bad idea that was.
Upon further reflection on my code, it is becoming apparent that I should synchronize the methods and not just the set. Thanks again.
Here is a description of the problem.
I have a list of objects (Strings) in a HashSet. This list can be very long (can contain thousands of entries). Several identical threads can be concurrently running (one for each client) who may ask to add an entry to the set (public static void addToList(String x)) or may want to check to see if a note already exists (public static boolean doesExist(String x)). As far as the adds are concerned, I don't want other adds or checks going on at the same time. Now that I think about it, simultaneous checks against the list can occur simultaneously so I suppose the best answer is to only synchronize the add method (and not the check method, nor the HashSet). What do you think?
-David
I have a HashSet that will contain many objects in it. However, several threads will be able add to the set or check to see if it contains a particular object. Obviously, I need to do some sort of synchronization.
Question: Is it better to synchronize the methods that act on the set, or synchronize the HashSet (Collections.synchronizedSet(new HashSet()), or use some other object that is already synchronized?
-David
I agree with Frank. If your code is not multi-threaded and there is no need to have synchronization, use the ArrayList! It is A LOT faster.
Aside from synchronization, the only benefit I've found to using a Vector is that many components such as JList, JComboBox, and JTable can take a vector of data in the Constructor to initialize the component.
However, if you are using an ArrayList, these components can also take an array of Objects which can be obtained from the ArrayList using the toArray() method.
20 years ago
Yes. There may be better ways to do this, but I just created by own renderer for the tree and added a boolean to the node's object (NodeInfo). I also used an icon that looks like a selected checkbox (selected.gif) and one that looks like an unselected checkbox (uinselected.gif). The advantage of doing this is that you can create tri-state checkboxes by simply having another icon and some logic to know when to use it (i.e., several items are highlighted with some being selected as some not).
class MyRenderer extends DefaultTreeCellRenderer
{
public MyRenderer() {}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
Object tempNode = node.getUserObject();
if (tempNode instanceof String)
{
return this;
}
NodeInfo nodeInfo = (NodeInfo)(node.getUserObject());
if (nodeInfo.getSelected())
{
setIcon(new ImageIcon("selected.gif"));
}
else
{
setIcon(new ImageIcon("unselected.gif"));
}
return this;
}
}
20 years ago
Swing works well using the Java plug-in as mentioned earlier. We user the plug-in supporting the 1.3 JRE and we think it is faster than the 1.2.2 (and more stable).
If you have not used the plug-in before, beware that your web page must have a bunch of extra code in it (more than the simple APPLET tag). Sun's web site on the plug-in has some good examples. I've not had success with their HTML converter, but writing your own HTML is pretty straight forward.
20 years ago
Is it possible to drag and drop from a JTable in one applet to a JTable in another Applet?
20 years ago
This is what I did. In the place where my class files are, I created an "images" directory and placed all my gif images there. In my code, I used the following line:
ImageIcon ii = new ImageIcon(TreeItems.class.getResource("images/image.gif), "image.gif");
Where TreeItems was the name of my class and image.gif is the name of my gif image.
Then, I jar up everything. What happens here is that I am getting the path within the jar file to my images/image.gif file based on where it finds my TreeItems class. Note that the images directory must be in the same directory as the TreeItems class.
You don't need to create the images directory (I do it to keep my class files and image files seperate). Hope this helps.
20 years ago
At the Colorado Software Summit, it was mentioned to use StringBuffers to concatenate strings instead of simply using the String class (I understand that Strings are first converted to String buffers anyway when concatenation occurs).
At what point does it start paying off to do this? Concatenating 2 strings? 3 strings? 4 strings? etc...
Also, if you have a long string that uses concatenation (simply to improve the readability of the code), will the compiler "optimize" it for you, or should you still use String buffers. For example:
String query = "select first, last, address, telephone, email"
+ " from employee"
+ " where country = 'US'"
+ " order by last, first";
Note: None of the strings parts are dynamic, all of them are static.
David Lu
21 years ago
Does anyone know how a browser can determine if the Java Plug-in is installed and then what version is installed? We have an applet that requires a minimum of the 1.2.2 JRE. If someone doesn't have the plug in, I want to direct them to a page with instructions on how to install the plug-in. However, if they already have the 1.2.2 JRE, I just want to let them know that the 1.3 is available. I saw some JavaScript that might be helpful, but I needed the JRE plug-in's ActiveX name.
21 years ago