Programming an interface in Java

In summary: In Action!"); } } //************************************************************************* //Main Function //************************************************************************* public static void main(String[] args) { StoreUI display = new StoreUI(); }In summary, the conversation discusses the creation of a program for a bookstore, with a focus on implementing a user interface. The program will consist of different tabs for inventory, reports, and cashier sections. Each tab will have a console output screen, an input field, and a status bar. The issue at hand is getting values from the input field into the methods that require the information. The conversation also includes the code for the user interface, including the creation of menus and tabs. The program can be locked and unlocked, with the
  • #1
Lancelot59
646
1
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?

Here is the UI so far:

Code:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
import java.lang.*;

public class StoreUI 	extends JFrame
						implements ActionListener
{
	String STORE_NAME = "NAME HERE";
	
	//Main Container Components
	private JPanel UIPanel;
	
	//Menus
	private JMenuBar topMenu;	//Menu bar
	JTabbedPane tabBar;			//Tab bar
	
	//Cards For Menus
	private JPanel card0;	//Lock screen
	private JPanel card1;	//Container for tabs
	private JPanel card2;	//Inventory tab
	private JPanel card3;	//Reports tab
	private JPanel card4;	//Cashier tab
	private JPanel card5;
	
	//Card 2 Components
	JTextArea console2OUT;			//Console screen for card 2
	JTextArea consoleStatus2OUT;	//Console status for card 2
	JTextArea console2IND;			//Console input display for card 2
	JTextField console2IN;			//Console input for card 2
	
	//Card 3 Components
	JTextArea console3OUT;			//Console screen for card 3
	JTextArea consoleStatus3OUT;	//Console status for card 3
	JTextArea console3IND;			//Console input display for card 3
	JTextField console3IN;			//Console input for card 3
	
	//Card 4 Components
	JTextArea console4OUT;			//Console screen for card 4
	JTextArea consoleStatus4OUT;	//Console status for card 4
	JTextArea console4IND;			//Console input display for card 4
	JTextField console4IN;			//Console input for card 4
	
	private JMenu menu1;			//Main menu
	private JMenu menu2;
	
	
	//Operators For Display
	
	
	private JButton unlockButton = new JButton("UNLOCK");
		
	//Control Constants
	int MAIN_WIDTH = 800;			//Width of window
	int MAIN_HEIGHT = 620;			//Height of window
	
	//*************************************************************************
	//Constructor
	//*************************************************************************
	public StoreUI()
	{
		//super("STORE_NAME System");
		UIPanel = new JPanel(new CardLayout());
																																			
		this.getContentPane();
		setSize(MAIN_WIDTH, MAIN_HEIGHT);
		setTitle(STORE_NAME + " System - LOCKED");
		
		setLayout(new CardLayout());
		
		try
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
			//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			//UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
			SwingUtilities.updateComponentTreeUI(StoreUI.this);
		}
		catch (Exception f) {/*theText.setText("YOU SUCK!");*/};
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		createMenuBar();
		
		createCard0();
		createCard1();

		add(card0);
		add(card1);
		
		card0.setVisible(true);
		card1.setVisible(false);
		
		
		setJMenuBar(topMenu);
		
		
		setVisible(true);
		
		
		
	}
	
	//*************************************************************************
	//MenuBar Builder
	//*************************************************************************
	private void createMenuBar()
	{
		topMenu = new JMenuBar();
		
		createMenu1();
		topMenu.add(menu1);
		topMenu.setVisible(true);
	}
	
	//*************************************************************************
	//Menu 1 Builder
	//*************************************************************************
	private void createMenu1()
	{
		menu1 = new JMenu("File");
		JMenuItem item;
		
		menu1.setBackground(Color.LIGHT_GRAY);
		
		item = new JMenuItem("Lock");
		item.addActionListener(new MenuListener());
		menu1.add(item);
		/*
		item = new JMenuItem("Unlock");
		item.addActionListener(new MenuListener());
		menu1.add(item);
		*/
		item = new JMenuItem("Exit");
		item.addActionListener(new MenuListener());
		menu1.add(item);
		
	}
	
	//*************************************************************************
	//Tab Panel Builder
	//*************************************************************************
	private void createTabPanel()
	{
		tabBar = new JTabbedPane();
		
		tabBar.setSize(this.getWidth(), 20);
		tabBar.setBackground(Color.LIGHT_GRAY);
				
		createCard2();
		createCard3();
		createCard4();
		
		tabBar.add("Inventory", card2);
		tabBar.add("Reports", card3);
		tabBar.add("Cashier", card4);
	}
	
	//*************************************************************************
	//Card 0 Builder
	//*************************************************************************
	private void createCard0()
	{
		card0 = new JPanel(new BorderLayout());
		JLabel MainMessage = new JLabel("TEST");
		
		card0.setSize(this.getContentPane().getWidth(),this.getContentPane().getHeight());
		
		card0.add(MainMessage, BorderLayout.NORTH);
		
		
		unlockButton.setSize(20, 60);
		unlockButton.setActionCommand("Unlock");
		unlockButton.addActionListener(new ButtonListener());
		card0.add(unlockButton, BorderLayout.CENTER);
		//card0.setBackground(Color.LIGHT_GRAY);
	}
	
	
	//*************************************************************************
	//Card 1 Builder
	//*************************************************************************
	private void createCard1()
	{
		card1 = new JPanel(new BorderLayout());
		card1.setSize(this.getContentPane().getWidth(),this.getContentPane().getHeight());
		
		createTabPanel();
		card1.add(tabBar);
		card1.setVisible(true);
		
		
	}
	
	//*************************************************************************
	//Card 2 Builder
	//*************************************************************************
	private void createCard2()
	{
		card2 = new JPanel();
		card2.setLayout(new GridBagLayout());
		card2.setSize(this.getWidth(), this.getHeight());
		GridBagConstraints c = new GridBagConstraints();
		
		console2OUT =  new JTextArea();
			console2OUT.setEditable(false);
			console2OUT.setPreferredSize(new Dimension(760,280));
			
			console2OUT.setText("Console Output");	
			
		console2IND = new JTextArea();
			console2IND.setEditable(false);
			console2IND.setPreferredSize(new Dimension(760,60));
			
			console2IND.setBackground(Color.LIGHT_GRAY);
			
		
		console2IN = new JTextField();
			console2IN.setPreferredSize(new Dimension(760,60));
			console2IN.setActionCommand("textFieldString");
			console2IN.addActionListener(this);
			
			
		consoleStatus2OUT = new JTextArea();
			consoleStatus2OUT.setPreferredSize(new Dimension(760,40));
			consoleStatus2OUT.setEditable(false);
			consoleStatus2OUT.setRows(2);
			consoleStatus2OUT.setBackground(Color.green);
			consoleStatus2OUT.setText("Status Bar");	
		
		c.gridx = 0;
		c.gridy = 0;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card2.add(new JLabel("Inventory Control"));
		
		c.gridx = 0;
		c.gridy = 1;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(10,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		
		card2.add(console2OUT,c);
		
		c.gridx = 0;
		c.gridy = 2;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,10,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card2.add(console2IND,c);
		
		c.gridx = 0;
		c.gridy = 3;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,10,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card2.add(console2IN,c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.ipadx = 0;
		c.ipady = 0;
		c.insets = new Insets(0,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card2.add(consoleStatus2OUT,c);
		//card2.add(console2INcont,;
		
		card2.setVisible(true);
	}
	
	//*************************************************************************
	//Card 3 Builder
	//*************************************************************************
	private void createCard3()
	{
		card3 = new JPanel();
		card3.setLayout(new GridBagLayout());
		card3.setSize(this.getWidth(), this.getHeight());
		GridBagConstraints c = new GridBagConstraints();
		
		console3OUT =  new JTextArea();
			console3OUT.setEditable(false);
			console3OUT.setPreferredSize(new Dimension(760,280));
			
			console3OUT.setText("Console Output");	
			
		console3IND = new JTextArea();
			console3IND.setEditable(false);
			console3IND.setPreferredSize(new Dimension(760,60));
			
			console3IND.setBackground(Color.LIGHT_GRAY);
			
		
		console3IN = new JTextField();
			console3IN.setPreferredSize(new Dimension(760,60));
			
			
		consoleStatus3OUT = new JTextArea();
			consoleStatus3OUT.setPreferredSize(new Dimension(760,40));
			consoleStatus3OUT.setEditable(false);
			consoleStatus3OUT.setRows(2);
			consoleStatus3OUT.setBackground(Color.green);
			consoleStatus3OUT.setText("Status Bar");
		
		c.gridx = 0;
		c.gridy = 0;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card3.add(new JLabel("Reports Control"));
		
		c.gridx = 0;
		c.gridy = 1;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(10,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		
		card3.add(console3OUT,c);
		
		c.gridx = 0;
		c.gridy = 2;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,10,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card3.add(console3IND,c);
		
		c.gridx = 0;
		c.gridy = 3;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,10,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card3.add(console3IN,c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.ipadx = 0;
		c.ipady = 0;
		c.insets = new Insets(0,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card3.add(consoleStatus3OUT,c);
		
		card3.setVisible(true);
	}
	
	//*************************************************************************
	//Card 4 Builder
	//*************************************************************************
	private void createCard4()
	{
		card4 = new JPanel();
		card4.setLayout(new GridBagLayout());
		card4.setSize(this.getWidth(), this.getHeight());
		GridBagConstraints c = new GridBagConstraints();
		
		console4OUT =  new JTextArea();
			console4OUT.setEditable(false);
			console4OUT.setPreferredSize(new Dimension(760,280));
			
			console4OUT.setText("Console Output");
			
		console4IND = new JTextArea();
			console4IND.setEditable(false);
			console4IND.setPreferredSize(new Dimension(760,60));
			
			console4IND.setBackground(Color.LIGHT_GRAY);
			
		
		console4IN = new JTextField();
			console4IN.setPreferredSize(new Dimension(760,60));
			
			
		consoleStatus4OUT = new JTextArea();
			consoleStatus4OUT.setPreferredSize(new Dimension(760,40));
			consoleStatus4OUT.setEditable(false);
			consoleStatus4OUT.setRows(2);
			consoleStatus4OUT.setBackground(Color.green);
			consoleStatus4OUT.setText("Status Bar");		
		
		c.gridx = 0;
		c.gridy = 0;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card4.add(new JLabel("Cashier Control"));
		
		c.gridx = 0;
		c.gridy = 1;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(10,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		
		card4.add(console4OUT,c);
		
		c.gridx = 0;
		c.gridy = 2;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,10,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card4.add(console4IND,c);
		
		c.gridx = 0;
		c.gridy = 3;	
		c.ipadx = 0;
		c.ipady = 6;
		c.insets = new Insets(0,0,10,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card4.add(console4IN,c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.ipadx = 0;
		c.ipady = 0;
		c.insets = new Insets(0,0,0,0);
		c.fill = GridBagConstraints.HORIZONTAL;
		card4.add(consoleStatus4OUT,c);
		
		card4.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		//Put stuff in here
	}

	//*************************************************************************
	//MenuListener Class For Menu Options
	//*************************************************************************	
	private class MenuListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String actionCommand = e.getActionCommand();
			
			if(actionCommand.equals("Exit"))
		            System.exit(0);
			else if(actionCommand.equals("Lock"));
					lock();
				
		}
	}
	
	//*************************************************************************
	//ButtonListener For Button Control
	//*************************************************************************
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == unlockButton)
			{
				unlock();
			}
		}
	}
	
	
	//*************************************************************************
	//Locks UI
	//*************************************************************************
	private void lock()
	{
		if(card1.isVisible())
		{
			card0.setVisible(true);
			card1.setVisible(false);
			setTitle(STORE_NAME + " System - LOCKED");
		}
	}
	
	
	//*************************************************************************
	//Unlocks UI
	//*************************************************************************
	private void unlock()
	{
		if(card0.isVisible())
		{
			card0.setVisible(false);
			card1.setVisible(true);
			setTitle(STORE_NAME + " System");
		}

	}
}
 
Last edited:
Physics news on Phys.org
  • #2
Lancelot59 said:
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.
 
  • #3
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 going to want to come back to this problem later in the summer.
 
Last edited:
  • #4
Lancelot59 said:
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 going to 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.
 
  • #5
I meant using the Java console. The system output that you access by going System.out.printf(""); for example. Is there a command that will clear it? Or do I just need to let everything stack up?
 
  • #6
  • #7
Eww...windows. I'm using Eclipse under the Ubuntu Karmic distro.
 
  • #8
New question: how can I make it wait for a condition to change.

In C I would go while(boolean value); do stuff. I'm doing the same thing here with an action listener setting the boolean value and it isn't working.
 
  • #9
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

Code:
  ...
  public void createMyGui() {
    ...
    JButton button = new JButton("Press me");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        System.out.println("Button pressed");
      }
    });
    ...
  }

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.
 
  • #10
Yeah, I have to say that the prof I have is great if you can pick the material up on your own, but it doesn't help with style at all. I'll have to learn how to do it properly over the summer.
 

1. What is an interface in Java?

An interface in Java is a type that defines a set of method signatures without providing any implementation. It acts as a contract that any class implementing the interface must follow by implementing all of the methods defined in the interface.

2. How do you declare an interface in Java?

To declare an interface in Java, you use the interface keyword followed by the name of the interface. For example: public interface MyInterface { // interface body } Note that interfaces can also extend other interfaces using the extends keyword.

3. Can an interface have variables and methods?

An interface can have variables, but they must be declared as public static final and are implicitly final and static. An interface can also have methods, but they must be declared as public abstract and are implicitly abstract. Starting from Java 8, interfaces can also have default and static methods with an implementation.

4. How do you implement an interface in Java?

To implement an interface in Java, you use the implements keyword followed by the name of the interface(s) you want to implement. For example: public class MyImplementation implements MyInterface { // class body } Note that a class can implement multiple interfaces by separating them with a comma.

5. What is the difference between abstract classes and interfaces in Java?

The main difference between abstract classes and interfaces in Java is that abstract classes can have both abstract and non-abstract methods, while interfaces can only have abstract methods. Additionally, a class can only extend one abstract class, but it can implement multiple interfaces. Another difference is that abstract classes can have constructors, while interfaces cannot.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top