Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7023579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:43:44+00:00 2026-05-27T23:43:44+00:00

I’m trying to build a small inventory application in Java, that switches views (or

  • 0

I’m trying to build a small inventory application in Java, that switches views (or pages) based on the card layout. For the standard change the user will use the menu at the top of the app, and that works fine.

But, one one of my screens the user will enter an item ID number to check. If that ID number is not found in the database, the app should switch to the New Item page. This is where I’m at a loss. I’ve tried to change the currently viewed card but nothing seems to be getting it.

Forgive me if this is a basic question (as well as poorly written Java 🙂 ), I’m teaching myself Java as I write this app. Attached I’m adding part of the main class (InventoryTrackingSystem) and the GUI class (the one that is trying to change the view).

/***** The MAIN class **********/

package inventorytrackingsystem;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.rmi.*;

public class InventoryTrackingSystem implements ItemListener {
  JPanel mainPanel;
  JFrame frame;

  JPanel cards = new JPanel(new CardLayout());; //a panel that uses CardLayout

  final static String MAINPANEL = "Main";
  final static String CHECKITEMSPANEL = "Check Items";
  final static String NEWITEMPANEL = "New Item";
  final static String CHECKOUTITEMPANEL = "Check Out Item";
  final static String ITEMINFOPANEL = "Item Information";
  final static String LISTALLITEMSPANEL = "List All Items";
  JPanel comboBoxPane;
  private JComboBox cb;
  static String comboBoxItems[] = {MAINPANEL,CHECKITEMSPANEL,NEWITEMPANEL,CHECKOUTITEMPANEL,ITEMINFOPANEL,LISTALLITEMSPANEL};

  public static void main(String[] args) {
    InventoryTrackingSystem ITS = new InventoryTrackingSystem();
    /* Use an appropriate Look and Feel */
    try {
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
    } catch (IllegalAccessException ex) {
      ex.printStackTrace();
    } catch (InstantiationException ex) {
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);

    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
//    javax.swing.SwingUtilities.invokeLater(new Runnable() {
//      public void run() {
        ITS.createAndShowGUI();
//      }
//    });

  }

  public void addComponentToPane(Container pane){
    //Put the JComboBox in a JPanel to get a nicer look.
    comboBoxPane = new JPanel(); //use FlowLayout
    cb = new JComboBox(comboBoxItems);
    cb.setEditable(false);
    cb.addItemListener(this);
    comboBoxPane.add(cb);
    cb.setVisible(false);

    //Create the "cards".
    JPanel main = new guiBuilder().buildGui("main");
    JPanel checkItems = new guiBuilder().buildGui("checkItems");
    JPanel newItems = new guiBuilder().buildGui("newItems");
    JPanel checkOutItems = new guiBuilder().buildGui("checkOutItems");
    JPanel itemInfo = new guiBuilder().buildGui("itemInfo");
    JPanel listAllItems = new guiBuilder().buildGui("listAllItems");

    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(main, MAINPANEL);
    cards.add(checkItems, CHECKITEMSPANEL);
    cards.add(newItems, NEWITEMPANEL);
    cards.add(checkOutItems, CHECKOUTITEMPANEL);
    cards.add(itemInfo, ITEMINFOPANEL);
    cards.add(listAllItems, LISTALLITEMSPANEL);

    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
  }

  public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
  }

  /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private void createAndShowGUI() {
      //Create and set up the window.
      frame = new JFrame("Inventory Tracking System");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setLayout(new BorderLayout());
      Menu m1 = new Menu("Options");

      MenuItem mi1_0 = new MenuItem("Main Page");
        mi1_0.setActionCommand("main");
        mi1_0.addActionListener(new menuListener());

      MenuItem mi1_1 = new MenuItem("Check Item");
        mi1_1.setActionCommand("checkItem");
        mi1_1.addActionListener(new menuListener());
      MenuItem mi1_2 = new MenuItem("Add New Item");
        mi1_2.setActionCommand("addItem");
        mi1_2.addActionListener(new menuListener());
      MenuItem mi1_3 = new MenuItem("List All Items");
        mi1_3.setActionCommand("listAllItems");
        mi1_3.addActionListener(new menuListener());
      MenuItem mi1_4 = new MenuItem("Check Out Item");
        mi1_4.setActionCommand("checkOutItem");
        mi1_4.addActionListener(new menuListener());
      MenuItem mi1_5 = new MenuItem("Exit");
        mi1_5.setActionCommand("exit");
        mi1_5.addActionListener(new menuListener());

      Menu m2 = new Menu("Help");
      MenuItem mi2_0 = new MenuItem("About");
        mi2_0.setActionCommand("about");
        mi2_0.addActionListener(new menuListener());

      m1.add(mi1_0);
      m1.add(mi1_1);
      m1.add(mi1_2);
      m1.add(mi1_3);
      m1.add(mi1_4);
      m1.add(mi1_5);

      m2.add(mi2_0);

      MenuBar mb = new MenuBar();
      frame.setMenuBar(mb);
      mb.add(m1);
      mb.add(m2);

      //Create and set up the content pane.
      //InventoryTrackingSystem setGUI = new InventoryTrackingSystem();
      addComponentToPane(frame.getContentPane());

      //Display the window.
      frame.pack();
      frame.setVisible(true);

      frame.setSize(780, 830);

      frame.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent we){
          System.exit(0);
        }
        public void windowClosed(WindowEvent we){
          System.exit(0);
        }
      });

    }

    class menuListener implements ActionListener{
      public void actionPerformed(ActionEvent ev){
        String thisAction=ev.getActionCommand();

        if(thisAction.equals("main")){
          cb.setSelectedItem(MAINPANEL);
        }else if(thisAction.equals("checkItem")){
          //change GUI
          cb.setSelectedItem(CHECKITEMSPANEL);
        }else if(thisAction.equals("addItem")){
          //change GUI
          cb.setSelectedItem(NEWITEMPANEL);
        }else if(thisAction.equals("checkOutItem")){
          //change GUI
          cb.setSelectedItem(CHECKOUTITEMPANEL);
        }else if(thisAction.equals("listAllItems")){
          //change GUI
          cb.setSelectedItem(LISTALLITEMSPANEL);
        }else if(thisAction.equals("exit")){
          System.exit(0);
        }else if(thisAction.equals("about")){
          JOptionPane.showMessageDialog(frame, "About This App");
        }
      }
    }

  public void swapView(String s){
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, s);
  }
}

/***** The GUI class **********/

package inventorytrackingsystem;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Image.*;
import java.awt.image.BufferedImage.*;
import javax.imageio.*;
import com.sun.jimi.core.*;


public class guiBuilder {
  JLabel itemIdLabel;
  JTextField itemID;
  JButton checkIt;
  JButton getSignature;
  mySqlStuff sql=new mySqlStuff();

  public JPanel buildGui(String guiType){
    JPanel thisGUI;
    if(guiType.equals("main")){
      thisGUI=mainGUI();
    }else if(guiType.equals("checkItems")){
      thisGUI=checkItemsGUI();
    }else if(guiType.equals("newItems")){
      thisGUI=newItemsGUI();
    }else if(guiType.equals("checkOutItems")){
      thisGUI=checkOutItemsGUI();
    }else if(guiType.equals("itemInfo")){
      thisGUI=itemInfoGUI();
    }else if(guiType.equals("listAllItems")){
      thisGUI=listAllItemsGUI();
    }else{
      thisGUI=mainGUI();
    }

    return thisGUI;
  } /* close buildGui() Method */

  private JPanel mainGUI(){
    JPanel thisPanel = new JPanel();

    return thisPanel;
  }

  private JPanel checkItemsGUI(){
    JPanel thisPanel = new JPanel();

    JPanel itemSection=new JPanel();
    JPanel exitSection=new JPanel();

    itemIdLabel=new JLabel("Enter/Scan Item ID");
    itemID=new JTextField(4);
    itemID.addKeyListener(new myItemIdListener());

    checkIt=new JButton("Check Item");
    checkIt.addActionListener(new myItemCheckListener());

    itemSection.add(itemIdLabel);
    itemSection.add(itemID);
    itemSection.add(checkIt);

    JButton exitButton=new JButton("Exit");
    exitButton.addActionListener(new exitButtonListener());

    exitSection.add(exitButton);

    thisPanel.add(itemSection);
    thisPanel.add(exitSection);

    return thisPanel;
  }

  private JPanel newItemsGUI(){
    JPanel thisPanel = new JPanel();

    return thisPanel;
  }

  private JPanel checkOutItemsGUI(){
    JPanel thisPanel = new JPanel();

    return thisPanel;
  }

  private JPanel itemInfoGUI(){
    JPanel thisPanel = new JPanel();

    return thisPanel;
  }

  private JPanel listAllItemsGUI(){
    JPanel thisPanel = new JPanel();

    return thisPanel;
  }

  class myItemIdListener implements KeyListener{
    boolean keyGood=false;
    public void keyPressed(KeyEvent keyEvent){
      int keyCode=keyEvent.getKeyCode();
      if((keyCode>=48 && keyCode<=57) || (keyCode>=96 && keyCode<=105)){
        keyGood=true;
      }
    }
    public void keyReleased(KeyEvent keyEvent){
      int keyCode=keyEvent.getKeyCode();
      if((keyCode>=48 && keyCode<=57) || (keyCode>=96 && keyCode<=105)){
        printIt("Released",keyEvent);
      }else if(keyCode==10){
        checkIt.doClick();
      }
    }
    public void keyTyped(KeyEvent keyEvent){
      int keyCode=keyEvent.getKeyCode();
      if((keyCode>=48 && keyCode<=57) || (keyCode>=96 && keyCode<=105)){
        printIt("Typed",keyEvent);
      }
    }
    private void printIt(String title,KeyEvent keyEvent){
      int keyCode=keyEvent.getKeyCode();
      String keyText=KeyEvent.getKeyText(keyCode);
      String currentData;

      if(title.equals("Pressed")){
        keyGood=true;
      }else if(title.equals("Released")){
        System.out.println(title+ " -> "+keyCode+" / "+keyText);
      }else if(title.equals("Typed")){
        System.out.println(title+ " -> "+keyCode+" / "+keyText);
      }

      try{
        String text=itemID.getText();
        if(text.length()==4){
          checkIt.doClick();
        }else{
          System.out.println("currentlLength: "+itemID.getText().length());
        }
      }catch(Exception ex){
        ex.printStackTrace();
      }
    }
  }

/****  THIS IS WHERE THE SWAP VIEW IS CALLED  ****/
  class myItemCheckListener implements ActionListener{
    public void actionPerformed(ActionEvent ev){
      String itemNum=itemID.getText();
      itemID.setText("");
      System.out.println("Checking ID#: "+itemNum);
      ArrayList checkData=new ArrayList(sql.checkInventoryData(itemNum));
      if(checkData.get(0).toString().equals("[NULL]")){
        System.out.println("New Item -> "+checkData.get(0).toString());
        InventoryTrackingSystem ITS = new InventoryTrackingSystem();
        ITS.swapView("NEWITEMPANEL");
      }else{
        System.out.println("Item Exists -> "+checkData.get(0).toString());

      }
      System.out.println(checkData);
    }
  }
  class signaturePadButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent ev){
      signaturePadStuff signature = new signaturePadStuff();
      while(signature.imageFileMoved==false){
        // wait for the signature to be collected and the image moved to the server.
      }
      System.out.println(signature.newFileName);
    }
  }

  class exitButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent ev){
      System.exit(0);
    }
  }

  /** Returns an ImageIcon, or null if the path was invalid. */
  protected ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if(imgURL != null){
      return new ImageIcon(imgURL, description);
    }else{
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }

  class submitItemButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent ev){
      String errorMsg = "";
      String[] formResults = new String[25];

    }
  }

  class clearFormButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent ev){
      return;
    }
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:43:45+00:00Added an answer on May 27, 2026 at 11:43 pm

    With this code in your actionPerformed method…

    InventoryTrackingSystem ITS = new InventoryTrackingSystem();
    ITS.swapView("NEWITEMPANEL");
    

    …you are calling swapView on a new instance of InventoryTrackingSystem (which is not the one you created in main and for which the UI is showing on the screen).

    -edited-

    you need an instance of InventoryTrackingSystem in your action listener
    you can for example store it in a member variable of myItemCheckListener like this:

    class myItemCheckListener implements ActionListener{
    
        private InventoryTrackingSystem its;
    
        // constructor takes its instance as argument
        public myItemCheckListener(InventoryTrackingSystem its){
            // ...assigns it to the member variable
            this.its = its;
        }
    
        public void actionPerformed(ActionEvent ev){
            // call swapView on the correct instance of InventoryTrackingSystem
            its.swapView()
        }
    }
    

    Of course, since your action listener is created in guiBuilder.buildGui() / checkItemsGUI() you will need the ITS instance there, too.

    BTW:
    It is not really necessary to create new guiBuilder instances like this:

    JPanel main = new guiBuilder().buildGui("main");
    ...
    JPanel listAllItems = new guiBuilder().buildGui("listAllItems");
    

    instead you could:

    guiBuilder builder = new guiBuilder();
    builder.buildGui("main");
    builder.build("whatever");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.