Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Fehler
Dec 14, 2004

.
I'm working on my first Java GUI application with SWT right now, so please excuse the presumably stupid questions.

1) I need to create dialogs from a thread and handle the return values. I think I can use Display.getDefault().syncExec(new Runnable() {...}); for that, but how would I get the dialog's return value from the Runnable?

2) Are there premade functions for displaying dialogs and message boxes or do I have to write them myself?

3) Would users just need the JRE to run my program or does SWT require additional software to be installed?

Adbot
ADBOT LOVES YOU

Fehler
Dec 14, 2004

.
Thanks, but I thought Swing and SWT were different things. Can I actually use Swing classes together with SWT?

Fehler
Dec 14, 2004

.
It seems like the whole bundling of native software for SWT would be a bit annoying, so do you think I should just move to Swing? It's not like I put much thought into what to use when I started...

Are there any good Swing tutorials out there that explain the basics for somebody who knows nothing about Java GUIs?

Also, is it correct that Swing is the one with that ugly blue-silverish GUI? Any way around that?

Fehler
Dec 14, 2004

.
I have a JTable with two columns and I want their widths to be distributed 9:1, i.e. if the whole table is 100px wide, column1 should be 90px and column2 10px.

After some searching it looked like these lines could help, but they don't seem to have any effect:
jTable1.getColumnModel().getColumn(0).setPreferredWidth(9);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(1);

What am I doing wrong?


Also, it seems like when I restart the program, it will automatically resize the columns to the way they were in the last session. Why does this happen? Could it be the reason why the two lines don't work?

Fehler
Dec 14, 2004

.

epswing posted:

Here's how I set my column widths:

code:
table.getColumn("User").setMaxWidth(75);
table.getColumn("Start Time").setMinWidth(130);
table.getColumn("End Time").setMinWidth(130);
Great, thanks!


Does anybody here have experience with the Apache Commons FTPClient class? When I try to cancel an upload with abort(), the program just freezes and waits for the upload to complete. Afterwards abort() returns true, though.

Fehler
Dec 14, 2004

.
To rephrase my question, can anybody recommend a free Java FTP client class that supports upload progress monitoring and can abort uploads? Apache's client can let me monitor the progress, but aborting doesn't seem to work...

Fehler
Dec 14, 2004

.
You might be looking for the concat() method. Try something like this:

code:
var a = new Array(scalar); //create new array for scalar
a = a.concat(array); //append array

functionToInvoke(a, ['ouput_div'] );

Fehler
Dec 14, 2004

.

syphon^2 posted:

That worked! Thanks!

How come alert seemed to display my data properly, but when supplying it in the same manner for functionToInvoke, it failed? Maybe something to do with how CGI::Ajax works?
I think alert() just can't display multidimensional arrays properly, so [1, [2, 3]] would look like [1, 2, 3].

Fehler
Dec 14, 2004

.
Try making intervalID global, i.e. add "var intervalID;" at the top outside the function.

Fehler
Dec 14, 2004

.
How do I save an XML document I created with org.w3c.dom.Document to a file?

Fehler
Dec 14, 2004

.
Looks like it works, thanks! I guess something like doc.save(path) would have been too easy...

Fehler
Dec 14, 2004

.
Speaking of executing commands, is there any way to do something like "mailto:foo@bar.com?subject=1" in Java?

When I do Runtime.getRuntime().exec('mailto:foo@bar.com?subject=1'); I get this exception: "java.io.IOException: Cannot run program "mailto:foo@bar.com?subject=1": CreateProcess error=2"

Fehler
Dec 14, 2004

.
Is there any function in Java that lets me check whether a host name has a DNS entry, like PHP's CheckDNSrr function?

Fehler
Dec 14, 2004

.
Any idea if there's a way at least to see if a host name is resolvable? The only thing I found was java.net.InetSocketAddress, but it looks like that will also open a socket, which is a bit overkill for my needs.

Fehler
Dec 14, 2004

.

zootm posted:

I think the libraries just use the OS stuff transparently, and there isn't a direct way to do that. I imagine there's about a billion libraries available for it though.

Edit: Actually it looks like this might let you access stuff through JNDI, although the one "review" of it I saw didn't seem to rate it highly. This library looks a bit more direct, and is allegedly used by quite a few projects.
dnsjava looks good, thanks for the links!

Fehler
Dec 14, 2004

.
I have a Java program that starts three threads. These threads all run infinite (while true) loops that selects a few hundred rows from a MySQL database, do a few things with them and insert them back into the table.

This works fine for a while, but the memory usage rises and rises, and after approximately two hours, Java runs out of heap space and crashes.

From the Netbeans Profiler's output, it looks like most of the memory is used by int[] arrays that are becoming bigger and bigger as the program runs. I have already commented out all the code that was responsible for the calculations and the inserting back into the table, but the problem still seems to exist.

Does anybody here have an idea what might create these arrays and how to get rid of them? I don't use int[] anywhere in my code so it must be some part of Java or a library.

Fehler
Dec 14, 2004

.

zootm posted:

Profilers can usually track where allocations happen as well as what was allocated, I'd investigate that. Is it possible that you're keeping one massive transaction open, or just re-using something that's supposed to be discarded? I'd guess it's somewhere in the database connector but I've really no idea I'm afraid.
Great, I didn't even know the Profiler could do that!

Thanks to your suggestion I found out that the java.net.IDN conversion class uses pretty large amounts of memory for every single conversion and doesn't free them up afterward. Now I just need to find out how to stop that...

Fehler
Dec 14, 2004

.

Startacus posted:

When I add the trim method to the functions I get a null pointer exception at that line. My returns don't have new lines, all that's in the two get function is "return variableName".
Then you probably have some objects in there where the name and/or the grade is not set (null). Also, the variables themselves might have newlines in them, depending on where you get their values from.

Fehler
Dec 14, 2004

.
Does it make a difference whether you request full permissions for your app in the JNLP file?

Fehler
Dec 14, 2004

.
Does anybody know where I could find sample code for comparing two WAV audio streams in Java? I need a function that returns some kind of "score" telling me how well the two streams match. From what I found on Google it looks like I need to do a Fourier Transformation, but I'm not really sure how to implement that and how to use it.

Fehler
Dec 14, 2004

.
I'm writing a Java application right now where several clients (around 40) play a simple game together. I'm going to start work on the server-client communication now, but before I start writing horrible code, I thought I'd ask here:

I guess the simple straightforward approach would be to write a server thread with a while(true)-loop with socket.accept() and then start a new thread for every new connection. Those threads would then wait for messages from the clients somehow (input stream) and notify the server thread by calling a special method, which would then loop over all connected client threads and call a method in all client threads to write to the socket.

But is this really the way to do this? Would it even work? Or are there ways in Java which would make this simpler and easier to maintain?

Fehler
Dec 14, 2004

.
Thanks for the link, but I'm not sure if will really help me. The way I understand the method, instead of keeping the connections to the clients open all the time, I would make the clients poll the server regularly. Wouldn't that cause even more load, since every client would have to send a request to the server every few 100 ms?

Fehler
Dec 14, 2004

.

Contra Duck posted:

It'll look a little different to that example, it'll probably require two threads, but the general principle is the same. You'll have a Selector object and a thread that is listening for connections and adding SocketChannels to the Selector. Then you'll have a second thread that asks the Selector for any Channels that are ready to be read. The SelectionKey determines the operation you're looking for, in that example it's set to ACCEPT but if you use READ then it'll return all the channels ready to be read.
Thanks, but what I don't really get about non-blocking sockets is how I do the reading and writing. From what I read in the docs the only way is to read/write strings encoded as ByteBuffers using the methods from the SocketChannel. But the docs also say that it's not always guaranteed that the entire string will actually be sent/received in one call, and I'm not really sure how to work with that.

Also, I was using serialization with ObjectStreams in order to transfer a special class between client and server which then encoded a message ID and the data. Would I really have to manually encode these objects as byte arrays and then decode them again on the other end in order to make them work here? Is that even feasible?

I think I might just try using threads for now, since I would really like to avoid writing thousands of lines of low-level code just to avoid using threads.

In that case I'd have another question though - in order to do this with threads I would have to open a socket in the client, constantly check whether there is any new data to read or write and then call the appropriate functions on the stream. I kinda have an idea how to do the writing part, but how do I determine whether there is any new data? I thought InputStream.available() would do that, but apparently not.

Fehler
Dec 14, 2004

.
I would like to make a Java program extensible by allowing people to write their own classes (derived from a pre-defined class) which I would then load in my program. That way, I'm hoping to make it possible to add certain features without having to recompile the entire program, or even having access to the code.

So basically I'm looking for something like a DLL equivalent in Java. I think I read about something like this once, but I'm unsure on the details. Is there even such a thing in Java? How hard would it be to implement? What are some good starting points?

Fehler
Dec 14, 2004

.
Thanks for the suggestions with the XML, but since it will always just be one single class that needs to be loaded, I think I should be fine with a simpler approach.

My concept so far is that the user puts a file extensionName.jar in a special folder and in my program for every .jar in the folder, I try to load a class called "org.me.extension." + extensionName.

Here is my code for that (file is a File object):
code:
try {
	URL jarURL = new File("jar:file://" + file.getAbsolutePath() + "!/").toURI().toURL();
	String className = "org.me.extensions."+file.getName().replace(".jar", "");
	log.info("Loading "+className+" from "+jarURL.toString());

	ClassLoader urlCl = URLClassLoader.newInstance(new URL[] {jarURL});
	Class theClass = urlCl.loadClass(className);
} catch (ClassNotFoundException ex) {
	log.log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
	log.log(Level.SEVERE, null, ex);
}
Unfortunately, I get an exception right now for this:

java.lang.ClassNotFoundException: org.me.extensions.TestClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

Any ideas why? I don't even know how I would go about debugging this...

Adbot
ADBOT LOVES YOU

Fehler
Dec 14, 2004

.
I'm trying to design a Swing form with auto-resizing components in the Netbeans GUI Designer.

My current version works fine if I enlarge the form (i.e. all the components nicely resize with it), but if I make it smaller beyond a certain threshold, the components stay at a fixed size and the right side is just clipped.

It seems like something is preventing the components from getting smaller, but I cannot for the life of me figure out what. All components and (as far as I can see) all components inside them have horizontal Auto-Resize enabled, but they still seem to get stuck at some point.

Any ideas what might be happening here and what else I could try?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply