Problem with multiple JFrames
807607Sep 30 2006 — edited Oct 2 2006Hello everyone. I'm coding a program as my Senior Project, and I've stubled across a problem. The objective of my program is an inventroy program that has 3 JFrames - The main JFrame where the program begins, a second JFrame for if the user clicks the "View Inventory" JButton, and the third JFrame if the user clicks the "Add/Edit Inventory" JButton. Now, my problems are this: 1) Whenever you click either of the two JButtons, it opens the JFrame, but nothing appears on it. 2) After you click one of the two buttons, the JMenuBar in the original JFrame experiences a problem, and seems to be "overwritten" but the text of the "View Inventory" JButton. I've tried the window.pack() method, and it seems to have had no effect. Note that the code is incomplete, seeing as I can't progress any further until I can see what's on the JFrames. The code(all 300+ lines(including spaces)) will follow below. Any and all help would be appreciated.
Thanks in advance.
(Code begins)
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.util.*;
public class SeniorProject extends JFrame implements ActionListener
{
DataOutputStream output;
DataInputStream input;
String name[];
String type[];
String number[];
JFrame viewFrame = new JFrame();
JFrame inputFrame = new JFrame();
JScrollPane viewPane = new JScrollPane();
JTextPane textPane = new JTextPane();
JPanel scrollPanel = new JPanel();
JPanel updatePanel = new JPanel();
JPanel buttonPanel = new JPanel();
JLabel msgLabel = new JLabel(" Welcome to the Riverside Tech Inventory Program!");
JLabel msg2Label = new JLabel(" Please click one of the following buttons to continue.");
JButton viewButton = new JButton("View Inventory");
JButton inputButton = new JButton("Input Item(s)");
public JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menuFile = new JMenu("File", true);
menuBar.add(menuFile);
JMenuItem menuFileExit = new JMenuItem("Exit");
menuFile.add(menuFileExit);
menuFileExit.setActionCommand("Exit");
menuFileExit.addActionListener(this);
JMenu menuAbout = new JMenu("About", true);
menuBar.add(menuAbout);
JMenuItem menuAboutProg = new JMenuItem("About this program...");
menuAbout.add(menuAboutProg);
menuAboutProg.setActionCommand("About");
menuAboutProg.addActionListener(this);
return menuBar;
}
public JMenuBar createMnuBar()
{
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
JMenu mnuFile = new JMenu("File", true);
mnuBar.add(mnuFile);
JMenuItem mnuFileReturn = new JMenuItem("Return to main window...");
mnuFile.add(mnuFileReturn);
mnuFileReturn.setActionCommand("Return");
mnuFileReturn.addActionListener(this);
JMenu mnuOptions = new JMenu("Options", true);
mnuBar.add(mnuOptions);
JMenu mnuOptionsSort = new JMenu("Sort");
mnuOptions.add(mnuOptionsSort);
JMenuItem mnuOptSortName = new JMenuItem("by Name...");
mnuOptionsSort.add(mnuOptSortName);
mnuOptSortName.setActionCommand("name");
mnuOptSortName.addActionListener(this);
JMenuItem mnuOptSortType = new JMenuItem("by Type...");
mnuOptionsSort.add(mnuOptSortType);
mnuOptSortType.setActionCommand("type");
mnuOptSortType.addActionListener(this);
JMenuItem mnuOptSortNum = new JMenuItem("by Number...");
mnuOptionsSort.add(mnuOptSortNum);
mnuOptSortNum.setActionCommand("number");
mnuOptSortNum.addActionListener(this);
JMenu mnuOptionsSearch = new JMenu("Search...");
mnuOptions.add(mnuOptionsSearch);
JMenuItem mnuOptSrchName = new JMenuItem("by Name...");
mnuOptionsSearch.add(mnuOptSrchName);
mnuOptSrchName.setActionCommand("NAME");
mnuOptSrchName.addActionListener(this);
JMenuItem mnuOptSrchType = new JMenuItem("by Type...");
mnuOptionsSearch.add(mnuOptSrchType);
mnuOptSrchType.setActionCommand("TYPE");
mnuOptSrchType.addActionListener(this);
JMenuItem mnuOptSrchNum = new JMenuItem("by Number...");
mnuOptionsSearch.add(mnuOptSrchNum);
mnuOptSrchNum.setActionCommand("NUMBER");
mnuOptSrchNum.addActionListener(this);
JMenu mnuAbout = new JMenu("About");
mnuBar.add(mnuAbout);
JMenuItem mnuAboutData = new JMenuItem("About the database...");
mnuAbout.add(mnuAboutData);
mnuAboutData.setActionCommand("Data");
mnuAboutData.addActionListener(this);
return mnuBar;
}
public JMenuBar createMBar()
{
JMenuBar mBar = new JMenuBar();
setJMenuBar(mBar);
JMenu mFile = new JMenu("File", true);
mBar.add(mFile);
JMenuItem mFileSav = new JMenuItem("Save and return...");
mFile.add(mFileSav);
mFileSav.setActionCommand("Save");
mFileSav.addActionListener(this);
JMenuItem mFileRet = new JMenuItem("Return without saving...");
mFile.add(mFileRet);
mFileRet.setActionCommand("Without");
mFileRet.addActionListener(this);
JMenu mOptions = new JMenu("Options", true);
mBar.add(mOptions);
JMenuItem mOptEdit = new JMenuItem("Edit...");
mOptions.add(mOptEdit);
mOptEdit.setActionCommand("Edit");
mOptEdit.addActionListener(this);
JMenuItem mOptAdd = new JMenuItem("Add...");
mOptions.add(mOptAdd);
mOptAdd.setActionCommand("Add");
mOptAdd.addActionListener(this);
JMenu mMisc = new JMenu("Misc.", true);
mBar.add(mMisc);
JMenuItem mMiscInfo = new JMenuItem("Generic information...");
mMisc.add(mMiscInfo);
mMiscInfo.setActionCommand("Info");
mMiscInfo.addActionListener(this);
return mBar;
}
public SeniorProject()
{
Container c = getContentPane();
c.setLayout((new GridLayout(2,1)));
buttonPanel.setLayout(new FlowLayout());
updatePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
viewFrame.setVisible(false);
inputFrame.setVisible(false);
updatePanel.add(msgLabel);
updatePanel.add(msg2Label);
buttonPanel.add(viewButton);
buttonPanel.add(inputButton);
c.add(updatePanel);
c.add(buttonPanel);
viewButton.addActionListener(this);
inputButton.addActionListener(this);
}
public JFrame viewFrame()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"The Look and Feel could not be set.","Error Message",JOptionPane.ERROR_MESSAGE);
}
viewFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
viewFrame.setSize(400,300);
viewFrame.setTitle("Riverside Tech Inventory - Viewing");
viewFrame.setJMenuBar(createMnuBar());
viewFrame.setResizable(true);
viewFrame.setLocation(280,280);
Container co = getContentPane();
co.setLayout(new FlowLayout());
scrollPanel.setLayout(new FlowLayout());
co.add(scrollPanel);
return viewFrame;
}
public JFrame inputFrame()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"The Look and Feel could not be set.","Error Message",JOptionPane.ERROR_MESSAGE);
}
inputFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
inputFrame.setSize(400,300);
inputFrame.setTitle("Riverside Tech Inventory - Editing");
inputFrame.setJMenuBar(createMBar());
inputFrame.setResizable(true);
inputFrame.setLocation(280,280);
return inputFrame;
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Exit")
{
System.exit(0);
}
if (arg == "About")
{
JOptionPane.showMessageDialog(null,"Tech Inventory Program\nVersion 1.0\nBy: Josh Thomas");
}
if (arg == "Return")
{
viewFrame.setVisible(false);
}
if (arg == "name" || arg == "NAME")
{
sort(name);
}
if (arg == "type" || arg == "TYPE")
{
sort(type);
}
if (arg == "number" || arg == "NUMBER")
{
sort(number);
}
if (arg == "Data")
{
JOptionPane.showMessageDialog(null,"Number of items in the database: " + tally(name) + ".");
}
if (arg == "Save")
{
inputFrame.setVisible(false);
}
if (arg == "Without")
{
inputFrame.setVisible(false);
}
if (arg == "Edit")
{
}
if (arg == "Add")
{
}
if (arg == "Info")
{
}
if (e.getSource() == viewButton)
{
viewFrame.pack();
viewFrame.setVisible(true);
viewFrame();
}
if (e.getSource() == inputButton)
{
inputFrame.setVisible(true);
inputFrame();
}
}
public int tally(String temp[])
{
int total = 0;
return total;
}
public void sort(String tempArray[])
{
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "The Look and Feel could not be set.","Error Message",JOptionPane.ERROR_MESSAGE);
}
SeniorProject f = new SeniorProject();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(350,200);
f.setTitle("Riverside Tech Inventory");
f.setJMenuBar(f.createMenuBar());
f.setResizable(false);
f.setLocation(250,200);
f.setVisible(true);
}
}
(I apologize ahead of time if this is the wrong place for this. I just guessed where to put it, since I couldn't really narrow down where it should specifically go.)