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
carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Slime posted:

So I've got an object which contains an object which contains a 2D array of objects that each have an object that has a vector that the top object needs to get stuff from. It's kinda lovely, any recommendations on handling this in a way that doesn't suck?

Edit: I suppose this can be considered a general OO question, but I'm doing it in Java, so...yeah.


You'll probably have to be more specific to get a better answer. Is it just that the code seems messy when you do those accesses? Is performance or memory an issue? Do each of these objects just exist to store the next object you mention or do they do other important things?

You could amortize those accesses a little by giving the top object a vector of vectors and going through each object and getting references to what you need and stuffing them in there, but I'm not sure what that would really accomplish in terms of memory use or complexity reduction, just make the code a little cleaner when you go to look through the vectors.

Adbot
ADBOT LOVES YOU

Zorro KingOfEngland
May 7, 2008

I recently inherited a webapp that uses Spring, and every so often in the logs I see messages like this:

code:
- INFO Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@334142ac: startup date [Thu Apr 16 13:22:51 CDT 2015]; parent: Root WebApplicationContext
- INFO Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c10acf7: defining beans 
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.annotation.internalPersistenceAnnotationProcessor,atmosphereResourceImpl]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1da9b274
I know next to nothing about Spring, but if anyone can point me in the direction of a good resource to try to figure out what causes these messages (or read up on Spring configuration in general) I would greatly appreciate it.

Slime
Jan 3, 2007

carry on then posted:

You'll probably have to be more specific to get a better answer. Is it just that the code seems messy when you do those accesses? Is performance or memory an issue? Do each of these objects just exist to store the next object you mention or do they do other important things?

You could amortize those accesses a little by giving the top object a vector of vectors and going through each object and getting references to what you need and stuffing them in there, but I'm not sure what that would really accomplish in terms of memory use or complexity reduction, just make the code a little cleaner when you go to look through the vectors.

The objects do other things than just store the next object, yeah. It's mostly just a case of the code seem messy and hard to read, if it's affecting performance or memory use I haven't seen any real problems yet. Since the list of variables I'm accessing is pretty small, I might just write a method in the top object that basically acts as shorthand. That would at least improve user readability, even if it doesn't truly tidy up the code.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Slime posted:

The objects do other things than just store the next object, yeah. It's mostly just a case of the code seem messy and hard to read, if it's affecting performance or memory use I haven't seen any real problems yet. Since the list of variables I'm accessing is pretty small, I might just write a method in the top object that basically acts as shorthand. That would at least improve user readability, even if it doesn't truly tidy up the code.

That's about all you can do, short of restructuring everything based on how you're using those objects, but that might be worse overall if it makes some other common task messier in exchange.

Slime
Jan 3, 2007

carry on then posted:

That's about all you can do, short of restructuring everything based on how you're using those objects, but that might be worse overall if it makes some other common task messier in exchange.

Yeah, I thought that'd be the case. Restructuring everything really isn't worth the time, and like you said it'll probably just introduce problems elsewhere. It just bothered me because it seems like a kludge solution. There's no such thing as perfectly structured code, you're always going to run into places where your structure is making something kinda messy.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Slime posted:

Yeah, I thought that'd be the case. Restructuring everything really isn't worth the time, and like you said it'll probably just introduce problems elsewhere. It just bothered me because it seems like a kludge solution. There's no such thing as perfectly structured code, you're always going to run into places where your structure is making something kinda messy.

This is where "don't prematurely optimize" starts to make sense. It seems like a problem to you as you go, but it's probably best to write it out, and look back and see whether you have performance or memory problems, or the code is excessively buggy, and then start looking for ways to address those issues. You'll probably find it's not that big of a deal after all and your first pass is fine, or you might discover a better solution altogether.

Spiritus Nox
Sep 2, 2011

I have an extra credit assignment that involves rewriting a server I've already implemented via thread pools and selectors with Asynchronous IO. My professor intimated that I'd want to take a look at AsynchronousSocketChannels - but our textbook doesn't mention them at all. Anyone have any recommendations on where to find a quick-and-dirty introduction to network IO with Asynchronous Sockets and how it differs from selector-based IO?

brand engager
Mar 23, 2011

VegasShirtGuy posted:

Did you change the socket to non-blocking at some point while troubleshooting? If so put it back to blocking mode.

Like Janitor Prime said you also need to update the GUI from the event dispatch thread. So in your ReadThreaded class instead of calling displayString() directly you'd call SwingUtilities.invokeLater() with a different Runnable object that calls displayString(). It might seem like overkill, and the program may even seem to work without that but at some point you will see weird behavior like parts of the GUI not updating, or text being cut off or something like that.

I think I figured out why it was happening. When I changed the reader to being in its own thread I never changed the way it is called. The original setup was a timer that called an actionperformed method to read from the stream ever 200ms. I had changed this to start an instance of the reader thread instead, but without checking if the old thread was done. So it was probably a shitton of threads all trying to use the same Scanner object. :v: I changed it to have only one thread started and that thread just loops forever with a sleep(200) at the end and the problem stopped.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Spiritus Nox posted:

I have an extra credit assignment that involves rewriting a server I've already implemented via thread pools and selectors with Asynchronous IO. My professor intimated that I'd want to take a look at AsynchronousSocketChannels - but our textbook doesn't mention them at all. Anyone have any recommendations on where to find a quick-and-dirty introduction to network IO with Asynchronous Sockets and how it differs from selector-based IO?

Good news for you; there are resources on the internet now, not just in books!

pigdog
Apr 23, 2004

by Smythe

Zorro KingOfEngland posted:

I recently inherited a webapp that uses Spring, and every so often in the logs I see messages like this:

code:
- INFO Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@334142ac: startup date [Thu Apr 16 13:22:51 CDT 2015]; parent: Root WebApplicationContext
- INFO Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c10acf7: defining beans 
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.annotation.internalPersistenceAnnotationProcessor,atmosphereResourceImpl]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1da9b274
I know next to nothing about Spring, but if anyone can point me in the direction of a good resource to try to figure out what causes these messages (or read up on Spring configuration in general) I would greatly appreciate it.
I believe it's about the part of Spring which handles annotation-based Spring configuration. I.e. it looks in the codebase, under <context:component-scan>, for @Component, @Service, @Autowired and such and configures Spring accordingly. This stuff is normal on startup, and possibly whenever you redeploy.

Zorro KingOfEngland
May 7, 2008

It seems to be happening at random times throughout the webapp's lifecycle, not just at startup and redeploy.

I think I've narrowed it down to that atmosphereResourceImpl bean, but nothing concrete yet. It doesn't seem to be affecting anything so I'm letting it be for now.

JimboMaloi
Oct 10, 2007

Can you post what atmosphereResourceImpl looks like (anonymized if need be)? If there's something in your bean that's forcing the application context to refresh I would expect it to be pretty evident in the code.

headlight
Nov 4, 2003

Can anyone recommend what I should be looking at to start programming windows applications in Java (Swing or JavaFX)? I want to build a file explorer and mass renamer that is standalone and looks just like a Windows program.

As background, I'm a novice programmer that has so been working on command line programs in Linux with lots of XML processing. When I last looked at Java GUI making for windows in 2002 everything was ugly and slow, so hopefully things have moved on...

So far I've figured out I should first start using an IDE (netbeans I guess?), and second pick JavaFX or Swing to work on a GUI. I don't need fancy graphics (but keen for it to have a 'Windows' look and feel), so maybe JavaFX isn't worthwhile if I'm going to rely on lots of stackoverflow answers to help me struggle through. However if its much easier than Swing and better if starting from scratch I could look at that.

Any pointers welcome.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

headlight posted:

Can anyone recommend what I should be looking at to start programming windows applications in Java (Swing or JavaFX)? I want to build a file explorer and mass renamer that is standalone and looks just like a Windows program.

As background, I'm a novice programmer that has so been working on command line programs in Linux with lots of XML processing. When I last looked at Java GUI making for windows in 2002 everything was ugly and slow, so hopefully things have moved on...

So far I've figured out I should first start using an IDE (netbeans I guess?), and second pick JavaFX or Swing to work on a GUI. I don't need fancy graphics (but keen for it to have a 'Windows' look and feel), so maybe JavaFX isn't worthwhile if I'm going to rely on lots of stackoverflow answers to help me struggle through. However if its much easier than Swing and better if starting from scratch I could look at that.

Any pointers welcome.

Either use Eclipse with Windowbuilder (https://eclipse.org/windowbuilder/) or use Netbeans so that you can use Swing with a nice WSYWIG interface. You don't want to manually code all those positioning values and stuff. I personally prefer Eclipse over Netbeans any day.

I wouldn't bother with JavaFX if you don't need to be rendering custom stuff. Keep it simple; sounds like you just want basic Windows windows and dialogues, right? Whats your plan here for the application?

Open up the designer, drop in a form and some buttons and stuff, and then you can double click on them or right click on them and add code for how to handle them, change their properties, etc. You can hop back and forth between the designer view (what the components look like and how they're arranged) and the actual source code view, which will include some autogeneration to make it super easy for you.

Alternatively for windows just use C# and WinForms (and like Visual Studio Express or something), C# and Java are practically the same thing and WinForms will give you a really nice Windows looking application with very little effort, just like the WSYWIG editors I described above. That said you can do Swing ones and it'll work just fine, but the interface will look a little more Java than standard Windows style. No biggie.

TheresaJayne
Jul 1, 2011

headlight posted:

Can anyone recommend what I should be looking at to start programming windows applications in Java (Swing or JavaFX)? I want to build a file explorer and mass renamer that is standalone and looks just like a Windows program.

As background, I'm a novice programmer that has so been working on command line programs in Linux with lots of XML processing. When I last looked at Java GUI making for windows in 2002 everything was ugly and slow, so hopefully things have moved on...

So far I've figured out I should first start using an IDE (netbeans I guess?), and second pick JavaFX or Swing to work on a GUI. I don't need fancy graphics (but keen for it to have a 'Windows' look and feel), so maybe JavaFX isn't worthwhile if I'm going to rely on lots of stackoverflow answers to help me struggle through. However if its much easier than Swing and better if starting from scratch I could look at that.

Any pointers welcome.

DO NOT USE NETBEANS!!!!

intelij idea community edition or eclipse luna at a push... but not the horror that is NETBEANS!

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I'm going to push back on you two fuckers and say USE NETBEANS instead, eclipse sucks and while I've been using InteliJ for 3 months I'm still not sure it's better than NetBeans.

Seriously it doesn't matter, they all will suite your needs so pick one based on any arbitrary criteria you like (looks or ease of installation) and just go with it. NetBeans WYSIWYG is dope

headlight
Nov 4, 2003




Thanks all - sorry I didn't meant to start a which IDE is best discussion, any will do, but this is all most helpful. I'll guess I'll start with Swing and go from there.

Thanks again!

Spatial
Nov 15, 2007

Zaphod42 posted:

You don't want to manually code all those positioning values and stuff.
You do if you want anything worthwhile. But use MigLayout so it's nice and easy to code.

Bleusilences
Jun 23, 2004

Be careful for what you wish for.

Thanks guys UI got 99% in the last assignment and the teacher commented on my nice try/catch :D

Volguus
Mar 3, 2009

Zaphod42 posted:

Either use Eclipse with Windowbuilder (https://eclipse.org/windowbuilder/) or use Netbeans so that you can use Swing with a nice WSYWIG interface. You don't want to manually code all those positioning values and stuff. I personally prefer Eclipse over Netbeans any day.

I wouldn't bother with JavaFX if you don't need to be rendering custom stuff. Keep it simple; sounds like you just want basic Windows windows and dialogues, right? Whats your plan here for the application?

Open up the designer, drop in a form and some buttons and stuff, and then you can double click on them or right click on them and add code for how to handle them, change their properties, etc. You can hop back and forth between the designer view (what the components look like and how they're arranged) and the actual source code view, which will include some autogeneration to make it super easy for you.

Alternatively for windows just use C# and WinForms (and like Visual Studio Express or something), C# and Java are practically the same thing and WinForms will give you a really nice Windows looking application with very little effort, just like the WSYWIG editors I described above. That said you can do Swing ones and it'll work just fine, but the interface will look a little more Java than standard Windows style. No biggie.

Java (with Swing and JavaFX) is one language where the usage of a UI builder actually harms productivity (a lot). Windows resource files ... yea, sure, you don't wanna touch that with a 10m pole. But Java, one person earlier brought up MigLayout. It's great and it does a good job. However, if you take 5 minutes to learn GridBagLayout (honestly, doesn't take more than 5 minutes using the tutorial) you will find that your more complex UI's are very easily implemented (in a maintainable manner) using it. Of course, for simpler requirements, other layouts may be more appropriate, you should get familiar with them. Using a UI builder is almost always a recipe for getting a lovely and unmaintainable code at the end.

And yeah, try out IntelliJ. Then go and use proper IDEs, such as eclipse and netbeans. They have the audacity to ask for money for intellij. The nerve ...

CarrKnight
May 24, 2013
On the topic, does anybody here unit tests swing? I'd like to know what program you use, but also how to structure gui unit tests (i find it so hard to isolate the gui)

JimboMaloi
Oct 10, 2007

Don't bother trying to unit test your GUI. Try to get as much of your logic out of the GUI as you can (ideally everything not specific to the construction and arrangement of your Swing components) and test a layer down, then cover your presentation layer during end-to-end/functional/whatever-you-call-it testing. I'd go so far as to say that any sort of automated testing of an AWT/Swing or SWT app is of questionable value since the tooling that I've seen is all hugely dependent on implementation details so you're likely to end up spending a lot of time rewriting the tests.

CarrKnight
May 24, 2013

quote:

so you're likely to end up spending a lot of time rewriting the tests.
I know for a fact that's true

quote:

Try to get as much of your logic out of the GUI as you can (ideally everything not specific to the construction and arrangement of your Swing components
You are right, unfortunately in my field we tend to use old libraries that make this hard. But yeah, I try my best.

TheresaJayne
Jul 1, 2011

CarrKnight posted:

On the topic, does anybody here unit tests swing? I'd like to know what program you use, but also how to structure gui unit tests (i find it so hard to isolate the gui)

Unit Tests are supposed to test your business logic with some simple interface tests for DB and such.

If you are programming a MVC model you start testing at the controller and stop at the DAO.

If you work for a big corporation you will probably have to test from View Bean down to raw DB code.

JimboMaloi
Oct 10, 2007

CarrKnight posted:

You are right, unfortunately in my field we tend to use old libraries that make this hard. But yeah, I try my best.

That's super unfortunate. Are you ending up with a bunch anonymous SwingWorkers inside your Swing classes? Are you running your business logic on the EDT?

CarrKnight
May 24, 2013

JimboMaloi posted:

That's super unfortunate. Are you ending up with a bunch anonymous SwingWorkers inside your Swing classes? Are you running your business logic on the EDT?

I am not running the business logic on the EDT, but that might be maybe the only good thing I can say about the libraries I use.

Elias_Maluco
Aug 23, 2007
I need to sleep
Simple stupid question: Im trying to use SimpleDateFormat to parse a timestamp like this: ""2015-03-11T00:00:00-0300"" (inside a jackson ObjectMapper, to parse a JSON response from a webservice).

What is the correct format to use? Ive tried "yyyy-MM-dd'T'HH:mm:ssZ" and "yyyy-MM-dd'T'HH:mm:ss-Z" and other combinations and nothing seems to work.

EDIT: nevermind it: I was trying to parse it to a DateTime, parsing it for a Date works (using "yyyy-MM-dd'T'HH:mm:ssZ")

Elias_Maluco fucked around with this message at 01:02 on Apr 29, 2015

hooah
Feb 6, 2006
WTF?
How the hell do I get a jar GUI to actually be responsive when I run it on Windows 8.1? I didn't make the file, so I can't really edit it (Stanford's NER tagger, for the record). It starts up OK and I can click a menu, but as soon as it opens that menu, nothing else responds except the close button.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Java GUIs aren't really known for their responsiveness, but that almost sounds like a bug of some sort. Are you able to try it on another computer to see if it behaves the same? (I'm not able to try it myself right now.)

hooah
Feb 6, 2006
WTF?
Yeah, I can run it fine on the school's server (Windows Server 2012, I believe).

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

You could try launching it from the command line and seeing what gets printed when you click the menu, that might give you some insight.

Other than that the only thing to check is that you have the right version of the JRE (I see the software you're using requires Java 8,) and that you have the latest updates for it from Oracle.

Also, it's kind of a one-in-a-million chance, but why not try deleting your copy and redownloading it? Occasionally things do get corrupted which is why some people provide the MD5 hash of their binaries so you can be absolutely sure no bits got flipped.

hooah
Feb 6, 2006
WTF?

carry on then posted:

You could try launching it from the command line and seeing what gets printed when you click the menu, that might give you some insight.

Nothing is printed. It seems to fork the command, since the prompt looks ready to accept another command.

quote:

Other than that the only thing to check is that you have the right version of the JRE (I see the software you're using requires Java 8,) and that you have the latest updates for it from Oracle.

java -version returns 1.8.0_45, which is Java 8, correct?

quote:

Also, it's kind of a one-in-a-million chance, but why not try deleting your copy and redownloading it? Occasionally things do get corrupted which is why some people provide the MD5 hash of their binaries so you can be absolutely sure no bits got flipped.

I tried this, too, and no luck.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.
Can you run other Java JARs okay? Something sounds wrong with your JRE.

hooah
Feb 6, 2006
WTF?

Zaphod42 posted:

Can you run other Java JARs okay? Something sounds wrong with your JRE.

I'm not sure; I don't think I have any other JARs just sitting around. Do you have an example?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

hooah posted:

I'm not sure; I don't think I have any other JARs just sitting around. Do you have an example?

http://sourceforge.net/projects/portecle/

hooah
Feb 6, 2006
WTF?
That works fine until I open a secondary dialog. The Open dialog box was completely empty and unresponsive, and the rest of the program was unresponsive after closing that window. I tried Options, which I was able to see the first time I tried it, but just tried again and got a blank window. Sounds like my JRE is hosed up, but I feel like I've reinstalled it in the past for this same problem. Is there a good set of instructions for "really, do a full clean uninstall" reinstallation anywhere?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

I didn't really see anything specific to Java when I went looking, but you might try something like Revo Uninstaller http://www.revouninstaller.com/revo_uninstaller_free_download.html to run after the standard uninstaller.

hooah
Feb 6, 2006
WTF?
Just reinstalled, similar behavior, but I see the content of secondary windows more often.

M31
Jun 12, 2012

Elias_Maluco posted:

Simple stupid question: Im trying to use SimpleDateFormat to parse a timestamp like this: ""2015-03-11T00:00:00-0300"" (inside a jackson ObjectMapper, to parse a JSON response from a webservice).

What is the correct format to use? Ive tried "yyyy-MM-dd'T'HH:mm:ssZ" and "yyyy-MM-dd'T'HH:mm:ss-Z" and other combinations and nothing seems to work.

EDIT: nevermind it: I was trying to parse it to a DateTime, parsing it for a Date works (using "yyyy-MM-dd'T'HH:mm:ssZ")

That is a ISO8601 date time, and SimpleDateFormat can not parse them according to the specification. Jackson has a ISO8601 parser, which is included in jackson-databind ISO8601Utils. You should probably be using databinding anyway:

code:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
JsonNode node = mapper.readTree(json);
JsonNode dateField = node.get("someDate");
Date date = mapper.convertValue(dateField, Date.class);
Also, use Joda Time.

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

M31 posted:

Also, use Joda Time.

Is Joda Time still necessary with Java 8? I've just been using the built in stuff now.

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