Programming an interface in Java

For my final project I need to code a program to handle a bookstore. Because the Java console isn't meant for stuff like this I decided to make my own UI.

The inventory, reports, and cashier section each get their own tab.

Each tab has a console output screen, a box showing what you just entered, the input field, and a status bar which will display what's going on.

The issue is that I don't know how to get values from the JTextField into the methods that need the info.

For example, the menu for the inventory section will need you to pick an option by entering a number in between 1 and 5(ish). How can I actually get the method that asks for that number to go to the JTextField and wait for the user to hit enter?

How can I actually get the method that asks for that number to go to the JTextField and wait for the user to hit enter?

You need to learn how to do event-oriented control-flow which is heavily used by Swing (and pretty much any other modern GUI framework).

Here is the UI so far ...

From a brief look at this code it seems you have added some code lines (many which will not compile) to an existing skeleton without really knowing why or how it is supposed to work. If so, it would be like doing mathematics by adding variables and expressions to equations a bit by random and hope it will all give sense in the end. It very, very rarely does.

If you really want to use Swing , may I suggest that you give the Swing tutorials a visit on http://java.sun.com/docs/books/tutorial/uiswing/ or pick up a book on the subject. GUI programming in general and Swing programming in particular is no trivial matter even if simple examples look trivial. If you don't understand what you are doing you most likely going to do it wrong.

If you are not ready for Swing, I would recommend that you leave out Swing and go for a simple console application. There is no reason to burn yourself on something complicated if you can make your project using simpler stuff that you know how to do.

It compiles and runs. I didn't just copy the code, I understand what each command in that list does. All I did was look at the examples on the java website to see how they were used and re-applied them.

I guess it's a bit complicated to make my own input stream...so I guess I'll just have to spam the console. I don't have enough time left to debug the UI. Is there a command I can use to clear the console? In C++ I used System("cls");. Is there anything equivalent to that in Java?

Also out of curiosity, how could I make the system I wanted to make work? I'm gonna want to come back to this problem later in the summer.

It compiles and runs. I didn't just copy the code, I understand what each command in that list does. All I did was look at the examples on the java website to see how they were used and re-applied them.

OK, I see now that your use of MenuListener is not a reference to the Swing MenuListener interface but to your own inner class of same name (which I missed on my first scan of your code). By the way, it is recommendable to use class names that are different from standard API names to avoid confusing readers of your code (including yourself later).

I guess it's a bit complicated to make my own input stream...so I guess I'll just have to spam the console. I don't have enough time left to debug the UI. Is there a command I can use to clear the console? In C++ I used System("cls");. Is there anything equivalent to that in Java?

If you by "console" mean your JTextArea, then you can clear the content of those by setting their text property to the empty string, e.g. text.setText(""). And if you by "input stream" mean you want to "read" your JTextArea or JTextField then you can get the same text property mention before using text.getText().

Also out of curiosity, how could I make the system I wanted to make work? I'm gonna want to come back to this problem later in the summer.

I would still recommend that you take one step at a time and follow some tutorials or get a good book about Swing programming so you can get to learn the Swing API. The GUI you seem to aim for is not terrible complicated to construct once you get the basics in Swing covered.

And you are of course alway welcome to ask specific questions on this forum.

As mentioned earlier, the Swing API uses an event-oriented approach, which for Swing means that you register a listener instance on some GUI element and this listener will then have its methods invoked by the event dispatcher thread (EDT) when an appropriate event occur. Note, that code in event methods must complete quickly to avoid having the GUI become unresponsive (prolonged tasks must be handed off to worker threads or similar).

For instance, if you have a button and wish to perform something when the button is pressed you register an ActionListener instance (using the addActionListener method on the button) which implements (or delegates to) the desired behavior its actionPerformed method. There are many ways you can choose to do it, but a short example that uses a so-called anonymous inner class could be

In your code, there is a definition of an inner class ButtonListener which has an instance added to your unlockButton. If you want to extend that pattern, you can add another instance to another button and add an "else if"-branch to the body of that actionPerfomed method that do what you want (I'm personally not a big fan of this particular pattern - I like the anonymous inner class better for short implementations as it keeps relevant code in one spot).

And, while you probably are getting tired of me saying this, I would once again like to point you to the tutorials. Your questions and code indicate you really need to get some fundamentals covered in a way that is not easily done in a forum like this.