T Dahl

Ranch Hand
+ Follow
since Oct 07, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
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 T Dahl

Thanks!

Darryl Burke wrote:Not sure about the local variable and instance field part, but setValue(getValue()) certainly isn't a no-op. Read the documentation for getValue() and you'll immediately spot why.

JFormattedTextField#getValue() wrote:... this may not return the current value.


I did read that a few times but you made me read it a few more. I think this is meant to say "..not the currently edited value". I.e. the value that was displayed after the last commit but before any further editing. I also thought that setValue would set the exact same value.

Now, I looked up the source. I am always afraid of being intimidated when I do that . Apparently the setValue does have kind of side effects:
- It sets a new Formatter
- It resets a flag which tells if the current value has been edited.
- It fires a property change. I suppose that is done to update the displayed value.
In the sample code I suppose at least the two latter side effects are desired.

I think the purpose of the ftf.setValue(ftf.getValue()) is to cancel the editing and reset the value to whatever it was before editing started. However cancelCellEditing() seems to do the job as well in my tests.

Feel free to correct my assumptions!
8 years ago
I am trying to learn about cell editors in JTable (swing). I am now studying this example: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableFTFEditDemoProject/src/components/IntegerEditor.java

There are a few things I don't understand. I'd be happy if you could help me better understand this example.

The first line of getCellEditorValue() is:

This is declaring the local variable ftf which happens to have the same name and type as a property of the class. Not only is the name and type the same but I believe it is actually referencing the same object. If so this line seems redundant. I tried a simplified example in the debugger and i still think it is referencing the same object. Am I right? Why is it done that way?

Almost at the end of userSaysRevert() I find the following line:

This looks like i noop but I assume it's done for some side effect. What happens here?
8 years ago
3 hours isn't much for learning XPath

In general, worry more about clarity and maintainability than quantity of code.
XPath is tailor made for XML. It's powerful and easy to read ones you know it. It integrates very well with XSLT and reasonably well with other languages such as Java.

I'll give you some ideas rather than solutions. You navigate the document tree in a similar way to how you would navigate a file tree. E.g. /Game/Round/Door. Counting your rounds could be done like this:
Listing the names of all the first doors:

The [1] makes sure it is only the first door. If you wanted all doors of the second Round:


As a Java programmer you may be used to thinking in Procedures. In XPath everything is about thinking functions. As a Java programmer you might be tempted to iterate over all the Doors, printing "First door:" then doing an xpath constructed in Java with an index you maintain in Java. That's just clumsy. Instead you could use the XPath function concat in the return expression. Much easier to read (and in this case less code). I leave it to you to figure out the details.

For simple retrieval as in your case it is hard to beat XPath. If you are going to work a lot with XML take the effort to learn XPath. You have to get used to indexes starting from 1 - not 0

I don't do much XML in Java, but when I do I first think XOM for structured data (as in your case) and DOM for mixed content. DOM is almost but not entirely compatible with the XDM model used by XPath.

Rob Spoor wrote:You can save yourself the work of manually encoding the file. The JDK's bin folder contains the native3ascii tool which can do the hard work for you.


Thank you! That seems to be the proper tool for a situation like the one I had. Btw. it is spelled native2ascii
8 years ago

Rob Spoor wrote:Unless you specify values inside the Java code...


But that's what I did. I used literals containing non-english characters to go in XML element names. E.g.


If you do specify values inside the Java code, you can use \uXXXX to inside the code to represent the Unicode characters. These can appear anywhere. For instance, the following is valid Java source code:
That's because \u0061 is the Unicode representation of the letter a, and the Unicode translation occurs before compiling. In other words, the compiler turns that cl\u0061ss and replaces it with class.


cool!

I have experimented more and I think I found a solution that works for me but may not be a general solution in similar cases. I changed the source file to UTF-8. As expected I got bunch of complaints. I then selected one of the characters and invoked the find/replace (ctrl-f in eclipse. The funny looking character appeared in the find frame. I put the right character (e.g. æ) in the replace frame and replaced all over the file. Repeat for each different character. Apparently this solved my problem. It is probably not the "right" way to do it...

Thank you for the reply! I actually learned something
8 years ago
Not sure if this is a Java question or an eclipse question. Perhaps a little of both.

What I want to do is to create an XML output file in UTF-8 format. The serializer allows me to specify UTF-8 so that part is taken care of. Some of the element names are constant literals in my program. My Java source file is stored in ISO-8859-1. For that reason some characters come out with the wrong representation. I believe that if my source code was in UTF-8 too my output would also be correct. It is not as easy as changing to UTf-8 in the file properties settings in eclipse. If I do I get a lot of "invalid character" messages. So my questions are:
- Will the Java compiler be happy with UTF-8 input?
- Is there an easy way to convert my ISO-8895-1 source file to UTF-8?
8 years ago

Winston Gutkowski wrote:

T Dahl wrote:when is the proper time to move to Java 7? I noticed it is still not available for general download.


It isn't?


I checked java.com earlier today and was still only offered the latest version of Java 6. I checked again now and guess what! Java 7 is out there. That probably means my main question is not as relevant any more.


Somehow I had the impression it should release last autumn. Anyway, it's possible to download both the JRE and JDK.
- Would it be risky to start developing with Java 7 now?
- Are there any known pitfalls?
- What about deploying applications written in Java 7?


Since you're plainly conversant with 6, what was your (or your company's) thinking regarding that?
For personal use: I'd regard any non-beta release as worth downloading.
For production code: Dunno. You might deploy it for non-critical systems first; particularly if you have a system (such as Solaris 10, or some of the latest Linux releases) that allow you to run them in a different VM (OS VM that is).

Winston

8 years ago
Not sure if this is off topic or not but,

when is the proper time to move to Java 7? I noticed it is still not available for general download. Somehow I had the impression it should release last autumn. Anyway, it's possible to download both the JRE and JDK.
- Would it be risky to start developing with Java 7 now?
- Are there any known pitfalls?
- What about deploying applications written in Java 7?
8 years ago
I just noticed the book promotion and I got this idea.

Say two or more beginner programmers or programmers with approximately the same background each bought the same book for study. Suppose they met regularly to exchange problems, discuss, help and motivate each other. This would be a kind of colloquium. The idea is to do this online. That should not be a foreign idea to any of the forum users.

We only need to assemble a sufficient group of people to buy the book at the same time and get to know each other. This could work inside the current forum, allowing other people to participate, or a separate forum could be created for each qualifying book. This would probably work best with books that have lots of exercises in them.
8 years ago
Thank you for the reply thomas!

Yes, there are a few reasons why I am thinking of using USB:
- It is very easy to use for a simple proof of concept demo (assuming I actually get the code to work on Android)
- I want to avoid installing anything on the PC if I can.
- The delay from activating the Android device to character accepted at the PC should be short and predictable. Probably not a big issue with a modern LAN but no issue at all with USB.
If successfully implemented on USB it should be easily portable to Bluetooth (which also uses HID).

What I have figured out so far:

There is USB support in Android. The class UsbAccessory represents the other device (e.g. a PC) as a host. However to do actual communication with the other device one needs to use UsbManager which according to the documentation supports host mode only. It is possible that my luck ran out there.

Android itself does not seem to have support for HID (Human Interface Device). I have found the official standard. It is non-trivial.
8 years ago
I'd like to create an app that is able to send (simulated) keystrokes to a PC over a USB connection.

Before I dive into the specs for USB and Human Interface Device (HID) I'd like to know if someone else has already created useful classes or other code to get me started.

To clarify:
The PC is to be the USB host, the android device being the slave.
The app will create the character to transmit to the PC as a keystroke (e.g. an 'A').
I'd like to invoke a method that will implement the transmission of the keystroke (given as a parameter to the method).
If such a method has already been written, let me know. Otherwise please point me to other useful code that may help me implement it.
8 years ago

Campbell Ritchie wrote:Not sure how you manage not greater than 255, however.


group() method of Matcher to extract each octet, then Integer.parseInt to check that the octet is in the right range.
9 years ago

g bag wrote:
And i wonder if domain names have underscores in them


Good question. The RFCs that define DNS allows any characters. Applications may be more restrictive.

BTW. The full DNS name may be a maximum of 255 octets with each component (between the dots) can be a maximum of 63 octets. I leave it as an exercise to the reader to enforce theese restrictions in a regex.
9 years ago

Rob Prime wrote:The part between the @ and the a-z / A-Z part needs reworking. You can have multiple subdomains; back at University I had an @student.tue.nl email address. So you want to quantify the [^\\s] and dot together as well: ([^\\s]+\\.)*. That * means that rob@localhost is still allowed.


[^\\s] will match a dot so multiple domain levels are matched. Good point about matching single level domains. Whether or not to allow that may be application dependent I guess. May be localhost should be your only single level domain allowed? E.g.
^[^\\s]+@(([^\\s].*\\.[a-zA-Z]+)|(localhost))$

If you do allow single level names in general, you should also consider allowing a wider range of characters than [a-zA-Z] for that name. Would you allow DonJuan@Mañana1 as an example? (not sure if this looks right on your screen)

Rob Prime wrote:
If I need to do any email address format validation I usually just use javax.mail.internet.InternetAddress


Another good point.
9 years ago