- #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:
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: