Sunday, 16 October 2011

Java: Core GUI Concepts III - Example Program (The easy-ish way: Functionality)

I feel that I should note this GUI builder isn't part of the textbook. I just looked at the generating of code and thought "Fuck this, there's gotta be a better way!" And then there was! Also, post on Sunday since I had that day-off earlier in the week.

Anyway, now that we have the visuals more or less done, we should work on functionality. First off, we want to get out of the design tab, and move into the code tab, like so:




Once there, we want to scroll to the bottom of the code, to where the variables are declared. Note that stuff with a grey background when not highlighted is automatically generated code that cannot be changed! Everything else so far is automatically generated, too, but you can tinker with it. Let's change that. First, from the first incarnation of our program, we know we need 2 strings to hold different messages, and one more to hold the current message, if you want. I'll be doing this without, just because I can.
TL;DR: Add variables to end of file. You can't touch grey background normally.

Also, I have my own update method, since its easier to type update than jLabel1.setText. Note that you can change variable names on the design panel with the right click menu.


Now that we have this, we'll probably need to change the label to use the variable instead of the "Hello!" string we have. Right click the label in the design tab, hit customize code. Now, this part it grey background, but there's a drop down menu which allows you to customize the code. Replace the string with a variable.
TL;DR: Right click label, hit customize code, look at picture below


Okay, that's sorted. We have one more step, and that's making our button functional! Double click it in design, and Netbeans should take you right to the code. If not, right click and go to Events, then Action. Its in here that you can specify different events, like mouse hover, or key presses.


Now we more or less use the code we used in the first example, adjusting for not using a current message variable.
This gets confusing without a set message, so fuck that! And I'm not going back to fix the screen caps, let this be a test of the attention you're paying! In our variables, add "String message = hi;", and change the custom label code to use "message". So yeah NOW we can pretty much paste the functionality right from the first program.

Or, if you prefer the standard if-else block:


I'll personally use this method more often, unless I'm pressed to compress. The code. It's just easier to think about, more intuitive. Anyway, here's our end result:


Ta-da! Potential issues include: You can re-size the window, but that'll re-size the button itself. Perhaps an issue with anchoring.

Anyway, that's over with, tomorrow we can get back to following the book, if there's no major questions. Which are totally welcome. Also, any specific (small-time) programs you want me to get working on ahead of time? Practice is good, you know!




Comment, follow, subscribe, share etc, and see you tomorrow!


And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 

7 comments:

  1. I love buttons, but just like a friend. Don't get me wrong, they are perfectly fine, but i'm not into buttons, i'd rather go with women.

    ReplyDelete
  2. I've heard buttons are racist!

    ReplyDelete
  3. I love this blog post! So easy to read and an interesting one at that. I look forward to your upcoming posts! Keep up the good work! Followed

    ReplyDelete
  4. lol, your tortoise method of learning is faster than mine, but keep it up, so helpful :D

    ReplyDelete
  5. Don't worry, your blog still pertains to my interests. I've wanted to learn more about coding for a while now.

    ReplyDelete
  6. I don't get the Hasidic Plumber! Girls with buttons are best!

    ReplyDelete