My first JPanel ...

Hey everyone. I just got started on adding GUIs to my program. It's a little exciting but really confusing for right now. So here is the scenario:

I've already made a .java file that holds an array that holds a calendar. It can generate arrays for each month through an algorithm after it read in information that the user enters, such as the day, month, and year. Now, regarding the Java GUI, I have a textbox, and two combo boxes. The textbox will hold the day that the user will type in such as "26" and one combo box will hold all the months and the other combo box holds the years from 2000-2010. I also have an uneditable TextArea that will hold a printed calendar array that I have already created.

My questions are: First, how do I set the TextArea to show my created array? And, how can I make it so the array changes whenever the user clicks a different month/year in the comboboxes? These may be easy, but really the GUI world is completely foreign to me!

I appreciate the help.

[1017 byte] By [ApRiXa] at [2007-11-27 6:15:53]
# 1
check this tutorial out. This might help a bit. http://java.sun.com/docs/books/tutorial/uiswing/learn/index.html
pberardi1a at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 2

Ah, thanks! So now I get that it's not TOO hard, I merely have to first create a method that occurs when a certain event is triggered. That doesn't sound too difficult. (I hope I'm right on that explanation, haha.)

However, is there anywhere I can find a list of different events or something? Because I need to have an event that is called when the user presses the enter key while inside a TextField.

ApRiXa at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 3
Look in the API for the Component you are working with and look at the methods it has: addXListener. Remember to look at the inherited methods list. Then read about the Listeners and the Actions they respond to.
floundera at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 4

Okay! I think I am almost getting this. Thanks for all the help!

However, I have ONE last question, sorry! In my textbook, I always see Swing examples like this :

public class FirstWindow extends JFrame

{

public static final int WIDTH = 300;

public static final int HEIGHT = 200;

public FirstWindow()

{

super();

//more code

}

}

Can I ask what the Super() does? It always includes it in the code, but it doesn't say what it does.

ApRiXa at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 5
It calls the constructor of the "super" class, in this case a JFrame
Navy_Codera at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 6
However, it is not necessary to include that. Java will implicitly call super even if it is not in your code. Also, if you do include it, it MUST be the first line in your constructor.
floundera at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 7
Okay, thanks everyone! I think I'm comfortable enough to get started now, Cheers.
ApRiXa at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 8
Have fun!
Navy_Codera at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 9

Hi again! I seem to have run into another mess.

I have a combobox and I want each item in that combobox's list to return a specific number when selected. Right now, the combobox's list holds all the month names. And I want it to return "0" when the user clicks "January", and return "1" when the user clicks "February" and so on. Could I do this with a Switch statement of some sort? I don't really know how I would accomplish this.

The method ComboBox.getSelectedItem() returns an object unfortunately, so I can't really do anything with that, or can I? All help is appreciated! Thanks.

ApRiXa at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 10

> The method ComboBox.getSelectedItem() returns

> an object unfortunately, so I can't really do

> anything with that, or can I? All help is

> appreciated! Thanks.

Sure you can, with a little planning. Design your own class, and put objects of that class into the list. (ComboBox? Are you letting your users enter their own month names?) The class's toString() method should return the name of the month, because that's what you want to show up in the GUI. You should provide a different method, say getMonthNumber(), to return the month number.

DrClapa at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 11

I must have explained my problem wrong since the solution to this problem didn't have to be THAT in-depth :) But thanks anyway! You actually did let me think of something on my own...

I completely forgot about the toString method! Thanks for reminding me : I have come up with this code and I found that it compiles so I must be heading somewhere in the right direction :

(jComboBox1.getSelectedItem()).toString();

All the items in my combobox are exactly month names. So if I convert them to Strings they will hold the month name that I am looking for. I used an if else if statement in the method getMonthNumber() to set the appropriate month values. Great!

I'm sorry for asking so much questions, but how can I have an event be triggered by pressing the enter key while on a textfield? So far I have private void jTextField1KeyPressed(java.awt.event.KeyEvent evt)

as the event's name (this was created by Matisse) I don't know if that means the enter key or what it means actually...

Message was edited by:

ApRiX

ApRiXa at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...
# 12
I think this is what you want.(String) jComboBox1.getSelectedItem();
floundera at 2007-7-12 17:27:02 > top of Java-index,Java Essentials,New To Java...