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
supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Question about ConcurrentModificationException, but may be unexpected:

Say, I have a list, and go over it with one iterator that duplicates entries, and with another iterator that selectively deletes entries. I imagine that 'undetermined behavior' here would be not knowing whether there has been duplication or deletion. If so, I think that could be desired behavior for some hypothetical case. If I'm correct, I could just catch the exception and continue. Or am I incorrect and there could be some other consequences?

Adbot
ADBOT LOVES YOU

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I would consider my program to function correctly if it continued working despite all (whether by catching runtime exceptions or missing some glitches), and didn't break my computer. :) Or run out of memory by accident... Oh, I just will have to test it out. Have to. :chef:

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
If you can keep a secret, I had this as part of an idea of artificial evolution while commuting. /secret.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I'd like to have a component in my interface that would be almost like JTree, except horizontal. Sort of. Actually JTree is my fall-back idea, but the ideal situation is a JList (or JTable?) in which rows can be grouped, and the grouping indicated by an editable label positioned to the side and center of the group. Sorry, I've looked around for something to illustrate this with but haven't found anything useful quickly.

And I'd dearly wish to preserve standard component functionality, instead of having to go up from textfields and labels. Is there a way?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Oh drat, I hope it works with eclipse projects well. Also, I guess it's good thing that I worked on saving window state manually. Also, maybe my Ubuntu computer has gotten better at handling Netbeans (or Netbeans takes less memory now).

Anyway, thanks.

Edit: Okay, what's it called and how do I get it?

supermikhail fucked around with this message at 16:13 on Aug 24, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I'm trying to make it so I would type in a textfield, and get the key event, but without anything actually happening in the textfield, so I could set the text programmatically. My only lead thus far is the KeyEventDispatcher interface, but I'm not sure it's what I want, and even if it were I don't know how to connect it to the textfield. Any help?

Edit: Nevermind, Component's processKeyEvent works splendidly.

supermikhail fucked around with this message at 10:10 on Aug 30, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I'm trying to cheat and highlight part of a JLabel with html, but having very sporadic luck so far (it doesn't help that my experience with html is almost non-existent). bgcolor sets what it says to what I want, but it needs a legitimate tag to go with, and everything I've tried newlines the rest of the text (at least <div> which shouldn't). Is there a non-violent solution to this?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Dammit, that's taken some suffering. But thanks... I thought <div> did the same thing! :(

For anyone interested, what I did is

code:
if (s.contains(searchTerm)) {
	s = s.replaceAll(searchTerm, "<span bgcolor=#99CCFF>"
		+ searchTerm + "</span>");
	s = "<html>" + s + "</html>";
}
Edit: unbroke the table, plus it would be better to add <html> after replacing, I figure.

supermikhail fucked around with this message at 20:20 on Sep 2, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
So is there any kind of at least small consensus on whether using GUI builders is good or not? I look at my raw (and bleeding) code and a sperg in me is biting to rewrite it all from scatch to make it pretty but I don't know if I should stay in Eclipse or move to Netbeans.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Okay, even if it's a personal project, I can't be sure I'd keep on using NetBeans if I switched to it, because last time it ate all my memory. Sooo...

Is there a good way to determine the optimal size of a component (without a GUI builder) when it's fed changing strings (such as stretch the component from one side of the monitor to the other), that doesn't involve trial and error?

Also, I've made a singleton dialog (a static instance through a static method), because under Ubuntu when I switch from my Java app then go back it only brings up the main window from under all the other applications (the dialog is non-modal). But when I close the dialog now, it's naturally kept in the memory, which is not exactly a resource-hog, but looks kind of unprofessional. Can I do anything?

Edit: Goddamit. I haven't tested anything outside Eclipse in ages, and it turns out under Ubuntu when you open a jar the running directory is the home directory, but I need the one where the jar is... Any hints?

supermikhail fucked around with this message at 06:33 on Sep 13, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Okay, I guess I'll have to go the easy road, because Java has clearly defeated me here. dispose doesn't work on my dialog because it keeps all the info (and even somehow manages to keep a Swing timer running.)

Also, the dialog doesn't appear in the window switch list, but that's an Ubuntu thing, then. Maybe it doesn't like that it's a dialog, and only wants to watch frames, or dialogs aren't expected to be non-modal, or it doesn't care about separate windows but only applications. Whatever.

The jar I was referring to is the one that your application ends up in. And it calls the wrong place home here.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
In fact I tried stopping the timer on window closing, but it didn't do any good. Neither does setting it to null, and I don't have any anonymous listeners (although, everything is implemented by the dialog itself).

I'll remake it as a window then, thanks for the info.

The project properties thing I still can't figure out, and I don't want to sound like a wuss, but I'm leaning towards putting the location of changeable files into user's hands (via preferences). It's not necessarily less coding, but at least it's something I can work with.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
The dispose doc says that you only need to call setVisible(true) on a disposed window to set it back the way it was, so I'm assuming it's the... Goddamit! Could it be because I add the dialog as an eventlistener to a completely separate object? Ugh. I'm so tired of creating and deleting the windowlistener over and over today. :downs:

rhag posted:

Regarding jar: so you want to load preference files? That you initally was thinking on placing in the jar itself? If that's so, you can load any resource on the classpath using getResourceAsStream(resourcename) from your classloader. Something like: this.getClass().getClassLoader().getResourceAsStream("/my/path/to/the/resource"); . If the path starts with a / it will be absolute to the top of the classpath (your jar or bin folder). Otherwise it will be relative to whatever package "this" is in.

That looks tempting. Maybe I'll reconsider.

What's more intuitive - to tell [my future self] that to carry over saved data I need to copy the application folder, or to check my preferences to see where the data is saved and copy that?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."

rhag posted:

This looks a bit weird. Why would anyone care what happens with the dialog? If they do, why don't they add their own listener to the dialog to be notified when that something is happening?
You know, you may be right. I didn't exactly understand what you said, but trying to formulate a coherent justification of my code led me to believe that maybe I don't need transient eventlisteners in my class that is simply a piece of data. I keep forgetting what I'm using them for at all, and where.

rhag posted:

ve. Personally I would store paths and stuff in the user's preference folder. You don't know if you can write the folder where the application resides, and you don't know where the user would like to store their data (could be /tmp, could be /home, could be an usb ... could be whatever other weird mounted path where they have enough space).
And preferences ... let the JVM take care of that. In windows they're store in the registry, in *NIX-es are store in the user's home folder.
I have my own nitpicky reasons for wanting to manually handle window positioning, resizing, etc. information storage (which is what I mainly mean by preferences here). Like remembering where a window should go if it's un-maximized again, having been closed and opened.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I'm baack!

I have a JComboBox where I store all sorts of strings, amond them "new string", which, when the user chooses it, brings up a dialog asking to enter the string. However, even if the dialog blocks all the other interaction with the parent window, the dropdown from the combobox still behaves as if it's active (although I obviously don't get any events from it). This is a cosmetic thing but a bit annoying and distracting. How should I go about solving it?

Hint: it could do something with the possible fact that the dropdown list is a new window. I guess there's no other choice but to write a custom component?

supermikhail fucked around with this message at 18:05 on Sep 16, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
God, that's so cool! And it just goes and does what you want it to...

I get it - in one thread Swing does what it's supposed to do - hide the list on click - and in another thread my code runs. Well, there goes the mystery.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I've got writer's block.

This application that I want to store preferences for, I just realized that I have to start with something - I have to have a default path where I would store all the non-default paths. I could slap it right in the home folder like the amateur I am, but maybe there's some trick that adult apps use? I'm also worried that if I slap the file there I might overwrite another one that accidentally has the same name but doesn't belong to my program.

Brethren (and sistren), what say ye?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."

TheresaJayne posted:

How about you have code in your app that on startup looks in current folder for the file,
if it doesnt exist create it

As an example look at minecraft server, when you first run it will check for the ops.txt file and create it if it is not there.

The perfectionist says that the executable file might be in the home folder, in which case the default paths file will be there, too, and we're back at the start (could overwrite something important)... Dammit. Although, I'll gladly look up to Minecraft for good coding practices (after all, it brought Java into mainstream gaming :rolleyes:).

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
YES!

So I think I've finally pinned down the directory that the running jar is in. It involves StackOverflow's notes in the margins and
code:
ClassLoader.getSystemClassLoader().getResource(".").toURI()
Anything funny about that? (as in, dangerous to the health of computers close to me)

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I'm trying to use the SwingX JXTreeTable, but hopefully even if no one here is familiar with it my case is more generic. That is, I'm observing some random repainting behavior. At first, when I simply added a node to the root, it would repaint once, a random time after opening the window and then ignore me. Then I added repaint() to where I add nodes in the code. Now it repaints fine, but with the closed root node (at least the root handle appears at the appropriate time). Once I expand the root node, it stops updating the UI (showing only the nodes that had been added up to that moment), even if I invoke the seemingly pertinent method valueForPathChanged. Anyone have any ideas why this might be happening?

StrikerJ posted:

I'm trying to write a method (method1) that draws a Swing window with a JTextField. And when the user enters something in the textfield and presses 'enter', the method returns that text that was written by the user.

My problem is that when I set an ActionListener on the JTextField (as in 'mytextfield.addActionListener()'), that listener will call another method (method2) when 'enter' key is pressed. If I do a 'mytextfield.getText()' in that method, the value I want to return is in the wrong method (method2 instead of method1).

I'm sure there is something simple I miss there, being new to object oriented programming. Thanks for any help.

EDIT: I guess I could store the value in a class wide variable. But I still don't see how method1 would know when to return that value since it won't know that method2 has gotten the value and stored it.

You might want to think about doing that with a custom (or regular) dialog instead http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html (http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input in particular).

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Should I resume narrating my struggles with a treetable here, or is there a better place for it? (Now I'm trying a different implementation in a different IDE, and it's doing exactly the same thing.)

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
What's the best way to preselest items in a JList if I have one large Collection and another one with elements selected from the first? That is,

Collection one: {item1, item2, item3, item4}; Collection two: {item2, item3};

JList:
-----
item1
item2
item3
item4
-----

And also maybe what is the best type of Collection to use here? Right now I'm just using the Collection interface in the dialog I'm doing (with TreeSet underneath because I thought it would be neat to have unique sorted elements by default). And maybe hints as to how to make the dialog not crap, the dialog used for editing a column in a table, because I want to have multiple choice in the cells. Please ask me clarifying questions!

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Okay, but now I'm worried that the custom (:)) is to move to a table when you need to add a checkbox (e: or maybe I'm imagining). Also, the primary functions of this program mostly involve "contains", so that's going to be my justification for TreeSet?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Well, looks like this http://stackoverflow.com/questions/17951886/cannot-add-checkbox-to-the-jlist is where I got my info on lists and tables. JLists really don't have the setCellEditor method. :eng101: And I'll go with TreeSet - its sorted property just appeals to me (and it's apparently one of the only two out-of-the-box implementations of SortedSet, the other being synchronized).

Thanks for your help!

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
So I've GUI-built (Netbeans) a
code:
                           split-pane containing 
                         /                        \
         panel containing                          scrollpane containing
           /      \                                        |
        stuff    scrollpane containing              panel containing
                           |                               |
                         JList                           stuff
I've come to the conclusion that this setup may only be in some way useful with the purpose of bringing about the end of the world (when you need that last drop of enthropy), because there's no way to make it behave reasonably. Opinions?

Note: it looks pretty cool if you don't actually touch anything.

Edit: Dang it - enthropy goes up, order goes down!

supermikhail fucked around with this message at 23:06 on Jan 30, 2014

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Well... :downs:

Yes, entropy (for some reason it looked to me much better with an "h"). The idea was that my design behaves so chaotically, it would be really handy if you wanted to increase entropy just a little bit.

Basically, the problem kind of was that with everything resizable within a scrollpane, if you grow the scrollpane the contents expand, and there is no way to make them shrink, because the scrollpane just creates the scrollbars. Also I think I broke the GUI builder a bit by changing the spaces manually (as in, entering the number) too many times... Oh, yeah, it's only for horizontal resizing that it's broken, but I fixed it by turning it off.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I have need of the following clever algorithm:

There is an ArrayList of Entries, wherein each Entry has associated with it a Date which can also be null. I'd like the "oldest" entry from the list, or, if there are several entries with the same Date, a random one from them. Except that they also can belong to another Skipping Collection, in which case look for another entry unless there are only entries belonging to that Skipping Collection. Whew...

I'm thinking about sorting by Date, but how to pick a random one then, especially with nulls (which, I think, should be randomly interspersed with non-skipped entries)?

For clarity, I guess:

code:
class Entry{

    Date date;

    public Entry(){ Date = null; } //created with null, only later set to a particular date; at least right now

    public void setDate(Date date){ this.date = date; }

}

HashMap<Entry, Integer> skippedEntries; //entries are skipped for a number of "turns"

public Entry getNextEntry(ArrayList<Entry> entries){

    ?

}

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I'm expecting around 30 entries tops.

baka kaba posted:

Couldn't you implement a Comparator that compares by date, but if the dates are equal it randomly returns higher or lower?

That's an idea. But... well, I'll have to implement both equals and compareTo, and they're either going to be inconsistent or unintuitive. But it reminds me that I can make a comparator to simplify the program, make Collections find the entry with the oldest date by min (I think)... or, yeah, sort it, then frequency (e: I now know it won't work, something else then), then get a random based on that frequency.

I get to play with so many cool toys. :downs:

Thanks for the ideas, guys (|| gals).

supermikhail fucked around with this message at 21:38 on Feb 9, 2014

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I've realized something, and now I'm completely confused. By the way,

Max Facetime posted:

Java code:
  static class Entry {
    Date date;
  }

  static Entry find(List<Entry> entries, Map<Entry, Integer> skippedUntilTurn) {
    ToIntFunction<Entry> skippedComeAfter =
      (Entry entry) -> skippedUntilTurn.getOrDefault(entry, 0);
    ToLongFunction<Entry> olderComeFirst =
      (Entry entry) -> entry.date.getTime();
    Comparator<Entry> entryOrder =
      Comparator.comparingInt(skippedComeAfter).thenComparingLong(
        olderComeFirst);
    Predicate<Entry> keepEntriesWithDates = (Entry entry) -> entry.date != null;

    return entries
      .parallelStream().sorted(entryOrder).filter(keepEntriesWithDates)
      .findFirst().orElseThrow(NoSuchElementException::new);
  }

:psypop: Is Java 8 this crazy with operators, or is it just pseudocode?

Anyway. I remembered that the exact Date is not relevant for me; and how to select a random entry from several that are near each other in date... that's a whole other bag of worms.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I should have been clearer in my previous post. It's (practically) impossible to have two identical dates (except nulls), so the original question doesn't apply anymore. Instead, I'm trying to come up with a method that would retrieve the entries by approximate date (and random if they are nearby). But I don't want to manually set the margin of approximation. What would happen if I just took some percentage of random entries and selected the oldest from them? Maybe that's a question for the Math thread.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."

baka kaba posted:

Wouldn't that skew all your choices to the older side of the distribution, and make the newer ones never come up at all? Since they'd always be ignored in favour of the older picks

I'm still not entirely sure what you want to do - like this thing about not manually setting the margin of approximation... what does determine it? It might help to lay out a step by step example of what you want to happen

So the entries that do come up can either be updated to "now" (as the date), or "skipped". In any case they are in a different place the next time. This could for example be useful when you have many physical exercises to work on, and want to work on them uniformly. I've found that simple randomness can leave periods between reappearances of entries that are too long, and I'm trying to compensate for it. (The example is, by the way, just an example.) Basically, I'd like the method to watch out that entries do not get too old, but the definition of too old is problematic, considering that it should be relative to all the entries.

Hope this explanation helps.

So, as step-by-step as I know: from the collection get an entry randomly, or, if some are especially old, get one of those -> option a) move the entry to the newest end of the collection; option b) set the entry aside for a while, then (or when you run out of other entries) insert it back into the oldest entries -> repeat until tired.

Maybe a solution lies in this reworking of my problem.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
If you know a more elegant solution, share!
code:
        Graphics graphics = getGraphics();
        FontMetrics metrics = graphics.getFontMetrics(taskL.getFont());

        int textWidth = metrics.stringWidth(text);

        int viewWidth = taskScrollpane.getViewport().getWidth();

        if (textWidth > viewWidth) {
            while (textWidth > viewWidth) {
                text = text.substring(0, text.length() - 1);
                textWidth = metrics.stringWidth(text + "...");
            }
            text = text + "...";
        }
        return text;
Context: In the effort to prevent the label taskL breaking the layout, I've put it in the taskScrollPane and the latter goes through the preceding code when resizing, and it also happens when I change the text.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Oh, no. (Maybe my Java is broken.)

The thing is thus:

JPanel ( JScrollpane ( JLabel ) )

And I'm using the scrollpane to find out if the panel is about to be pierced by the label. For some reason if I stuffed a long text in the label before that, the panel resized out of the frame which didn't (and in any case I don't think resizing the frame for changing text is practical). So I guess I could just try to set the size of the label directly and it would do the magic without touching the fonts themselves.

Or maybe it's just Netbeans.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Well, call me weird, but I don't want multiline text. I think design would be better for it. Anyway, labels really put ellipses in automatically, except you have to do some uncommon things to the properties. That'll sure clear some clutter. Thanks for your help.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I feel that the probable inefficiency of this code is going to haunt me, so here is a bunch of it. If anyone could be bothered to take a look and post a comment, I would be very grateful.
code:
    public Task getNextInCategories(Collection<String> categories) {        
        ArrayList<Task> temp = getRemainingInCategories(categories); //method not shown but it's fairly reasonable

        //some fairly reasonable code

        //refer to the methods below
        Date recentDate = getMostRecentCompletionDate(temp);
        Date oldestDate = getOldestCompletionDate(temp);

        //First I convert dates into calendars because they allow to add days
        //The idea is basically to find out if the top of the list is too far from the bottom of the list (too far here being the arbitrary two days)

        Calendar oldestPlusDayCalendar = Calendar.getInstance();
        oldestPlusDayCalendar.setTimeInMillis(oldestDate.getTime());
        oldestPlusDayCalendar.add(Calendar.DATE, +2);

        Calendar recentCalendar = Calendar.getInstance();
        recentCalendar.setTimeInMillis(recentDate.getTime());

        Random rand = new Random();

        if (oldestPlusDayCalendar.compareTo(recentCalendar) >= 0) {
            nextTask = temp.get(rand.nextInt(temp.size()));
        } else {
            TaskByDateComparator comparator = new TaskByDateComparator();
            Collections.sort(temp, comparator);
            Date oldestPlusDay = new Date(oldestPlusDayCalendar.getTimeInMillis());

            //look for the method "lastIndex" below
            int lastOldIndex = lastIndex(oldestPlusDay, temp);
            if (lastOldIndex == 0) {
                nextTask = temp.get(0);
            } else {
                List<Task> subList = temp.subList(0, lastOldIndex);
                nextTask = subList.get(rand.nextInt(subList.size()));
            }
        }

        //some fairly reasonable code

        return nextTask;
    }

    private Date getMostRecentCompletionDate(Collection<Task> tasks) {
        Task t = Collections.max(tasks, new TaskByDateComparator());
        return t.getLastTimeDone();
    }

    private Date getOldestCompletionDate(Collection<Task> tasks) {
        Task t = Collections.min(tasks, new TaskByDateComparator());
        return t.getLastTimeDone();
    }

    private int lastIndex(Date date, List<Task> sortedTasks) {
        Task t;
        for (int i = 0; i < sortedTasks.size(); i++) {
            t = sortedTasks.get(i);
            if (t.getLastTimeDone().compareTo(date) > 0) {
                return i;
            }
        }
        return -1;
    }
Dang, I'm still calling it "PlusDay" even though it is "PlusTwo". Also, maybe it'd be better to store dates as longs? I hope that's not too much code. :ohdear:

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."

Brain Candy posted:

Use JodaTime.

Nice. That's going to make the code much clearer, if nothing else.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I have a textfield whose input I'd like to verify, except there's quite a few top-level focusable components (which makes the InputVerifier useless) (like, the textfield is in a panel that's next to a list). FocusLost doesn't work either. Any hints or tricks?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Oh, right. The only condition I'd like to monitor is that the textfield shouldn't be empty on losing focus when the user selects another entry from the list. I don't need the program to panic each time I completely rewrite the text.

I guess if nothing comes up, I could store the list's row on focusLost, check if it's empty and return to it.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I had a dialog in the previous version, and that seemed too slow there. The entries can have a lot of information which could be hard to display in the list, but right now I can arrow down the list pretty fast and see the information in a side-pane. Most of the editing is pretty secure, only the textfield is a bit of a snag.

Adbot
ADBOT LOVES YOU

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I wrote a thing:
code:
@Override
    protected void processKeyEvent(KeyEvent e) {
        if (e.getID() == KeyEvent.KEY_PRESSED && isEditable()) {
            int keyCode = e.getKeyCode();
            String keyText = KeyEvent.getKeyText(keyCode);
            switch (keyText) {
                case "Left":
                case "Right":
                case "Home":
                case "End":
                    super.processKeyEvent(e);
                    return;
            }
[...]
For this overcomplicated program I'm making I couldn't find the keycodes for numpad left and right, but I discovered that they have the same text as the regular arrows. So, this is my best idea right now. I suspect just reading the keycodes wouldn't work if I moved to a different computer (arrow right is 39 here and numpad right is 227).

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