Reputation: 131
First, I would like to say I have searched these forums, as well as others for an answer to my question. I am very new to Java, and programing in general. After working on this for several hours, I would like to ask for some help.
I am trying to open up a window (that will have some graphics later), that has a menu bar. The options are 3 drop down menus that perform a task when selected. The file menu lets me save, load, and exit the program. For now the rest just print to the screen to let me know that much is working. Right now, I want to set it up so when I click the "Create Seller" option, it will open up a new dialog box with 3 fields. I would like to enter a string in one, and two doubles in the other two. I will then store those as variables and send them to another function. I have been stuck on this part for way too long. Keep in mind that, while this has probably been answered before, I think I am just not competent enough with Java yet to understand the answer. Try to dumb it down for me if that is not too much to ask. THANKS!
here is the class I am working on. Feel free to ask for less or more if you need.
package seller;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import datastore.Meteorite;
import datastore.continents;
import datastore.customers;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class BaseFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JFileChooser myChooser = new JFileChooser(".");
JMenuItem menuItemSave;
JMenuItem menuItemLoad;
JMenuItem menuItemExit;
JMenuItem menuItemCreateSeller;
JMenuItem menuItemUpdateSeller;
JMenuItem menuItemRemoveSeller;
JMenuItem menuItemCreateMeteorite;
JMenuItem menuItemUpdateMeteorite;
JMenuItem menuItemRemoveMeteorite;
JMenuItem menuItemCreateOneLine;
continents worldMap = new continents();
boolean[][] mapArray = null;
int selection = 1;
String shopName = null;
Map myMap= new Map();
Scanner myScanner = new Scanner(System.in);
Menu myMenu = new Menu();
seller seller= new seller();
Meteorites myMeteorite = new Meteorites();
customers sellers= new customers(0,0,"test");
customers buyers= new customers(0, 0, "test");
ArrayList <Meteorite> myMeteorites = new ArrayList<Meteorite>();
ArrayList <customers> mySellers= new ArrayList<customers>();
ArrayList <customers> myBuyers= new ArrayList<customers>();
customers people= new customers(0,0,"test");
public void makeGui()
{
this.setTitle("Phase 3");
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
//Load and Save used from class example
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
menuItemSave = new JMenuItem("Save");
menuItemSave.addActionListener(this);
menuItemLoad = new JMenuItem("Load");
menuItemLoad.addActionListener(this);
menuItemExit = new JMenuItem("Exit");
menuItemExit.addActionListener(this);
fileMenu.add(menuItemSave);
fileMenu.add(menuItemLoad);
fileMenu.add(menuItemExit);
//seller menu
JMenu sellerMenu = new JMenu("Seller");
mb.add(sellerMenu);
menuItemCreateSeller = new JMenuItem("Create Seller");
menuItemCreateSeller.addActionListener(this);
menuItemUpdateSeller = new JMenuItem("Update Seller");
menuItemUpdateSeller.addActionListener(this);
menuItemRemoveSeller = new JMenuItem("Remove Seller");
menuItemRemoveSeller.addActionListener(this);
sellerMenu.add(menuItemCreateSeller);
sellerMenu.add(menuItemUpdateSeller);
sellerMenu.add(menuItemRemoveSeller);
//handle meteorite menu
JMenu handleMeteoriteMenu = new JMenu("Handle Meteorite");
mb.add(handleMeteoriteMenu);
menuItemCreateMeteorite = new JMenuItem("Create Meteorite");
menuItemCreateMeteorite.addActionListener(this);
menuItemUpdateMeteorite = new JMenuItem("Update Meteorite");
menuItemUpdateMeteorite.addActionListener(this);
menuItemRemoveMeteorite = new JMenuItem("Remove Meteorite");
menuItemRemoveMeteorite.addActionListener(this);
menuItemCreateOneLine = new JMenuItem("Create Meteorite(1 line)");
menuItemCreateOneLine.addActionListener(this);
handleMeteoriteMenu.add(menuItemCreateMeteorite);
handleMeteoriteMenu.add(menuItemUpdateMeteorite);
handleMeteoriteMenu.add(menuItemRemoveMeteorite);
handleMeteoriteMenu.add(menuItemCreateOneLine);
JPanel tPanel = new JPanel();
this.add(tPanel);
this.pack();
this.setSize(new Dimension(640,480));
this.setVisible(true);
}
String myName;
double myLong;
double myLat;
@Override
public void actionPerformed(ActionEvent e)
{
Object source=null;
source=e.getSource();
if (source == menuItemLoad)
{
mapArray = worldMap.createMap();
myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = myChooser.showOpenDialog(this);
//if user clicked Cancel button, return
if (result == JFileChooser.CANCEL_OPTION)
System.exit(1);
File myFile = myChooser.getSelectedFile(); //get file
//display error if invalid
if ( ( myFile == null) || (myFile.getName().equals("")))
{
JOptionPane.showMessageDialog(this, "Invalid Name", "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
myMeteorites= myMeteorite.loadMeteorite(myMeteorites, myFile);
sellers.loadSeller(mySellers);
buyers.loadBuyer(myBuyers);
}
if (source == menuItemSave)
{
mapArray = worldMap.createMap();
myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = myChooser.showOpenDialog(this);
//if user clicked Cancel button, return
if (result == JFileChooser.CANCEL_OPTION)
System.exit(1);
File myFile = myChooser.getSelectedFile(); //get file
//display error if invalid
if ( ( myFile == null) || (myFile.getName().equals("")))
{
JOptionPane.showMessageDialog(this, "Invalid Name", "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
myMeteorites= myMeteorite.saveMeteorite(myMeteorites, myFile);
sellers.saveSeller(mySellers);
buyers.saveBuyer(myBuyers);
}
if (source == menuItemExit)
{
System.exit(1); //exit system
}
if (source == menuItemCreateSeller)
{
JFrame myFrame = new JFrame("Create a Seller");
JPanel myPanel = new JPanel();
JLabel myLabel1 = new JLabel();
JLabel myLabel2 = new JLabel();
JLabel myLabel3 = new JLabel();
final JTextField myText1 = new JTextField(30);
final JTextField myText2 = new JTextField(30);
final JTextField myText3 = new JTextField(30);
setDefaultCloseOperation(EXIT_ON_CLOSE);
myLabel1.setText("Enter Name");
myLabel2.setText("Enter Longitude");
myLabel3.setText("Enter Latitude");
myPanel.add(myLabel1);
myPanel.add(myText1);
myPanel.add(myLabel2);
myPanel.add(myText2);
myPanel.add(myLabel3);
myPanel.add(myText3);
myFrame.add(myPanel);
myFrame.setSize(350,200);
myFrame.setVisible(true);
myText1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
myName = myText1.getText();
}
});
myName = myText1.getText();
//myLong = Double.parseDouble(myText2.getText());
//myLat = Double.parseDouble(myText3.getText());
System.out.println(myName);
//mySellers=people.createCust(mySellers, myLong, myLat, myName);
//mySellers=people.saveSeller(mySellers);
}
if (source == menuItemRemoveSeller)
{
System.out.println("remove seller");
}
if (source == menuItemUpdateSeller)
{
System.out.println("update seller");
}
if (source == menuItemCreateMeteorite)
{
System.out.println("create meteorite");
}
if (source == menuItemUpdateMeteorite)
{
System.out.println("updateMeteorite");
}
if (source == menuItemRemoveMeteorite)
{
System.out.println("remove meteorite");
}
if (source == menuItemCreateOneLine)
{
System.out.println("create one line");
}
}
}
Upvotes: 0
Views: 87
Reputation: 13123
You've gotten good advice in comments, and the other answer that has been posted looks reasonable at a glance. I'm going to throw in an explanation that you may or may not need; if you do need it, it would be the sort of thing it would be hard to explain that you need, because it is a concept that you may not have assimilated yet.
UI programming is "event-driven"; that means that, instead of having control in your code as you do for a non-interactive program, the control is passed to another system or part of the system, and when certain events happen, that other part calls your code. The 'other part' in this case is the Swing runtime. So your code is all about setting up the frames, panels, layouts, text fields, drop-downs, etc., AND setting listeners to those things that might cause events, and waiting for events to happen.
If your confusion is rooted in a lack of understanding of this, then the answer to your question might be that the listener for the button on the dialog is going to read the values from the drop-downs and/or text fields -- presumably the user has chosen and/or entered values as he/she was supposed to and then clicked the button. So the action routine that fires when the button (or the enter key) is clicked (pressed) goes and gets those values as they are at that time and puts them into variables (or writes them to a database or instantiates another object with them or whatever it is supposed to do.
This routine also might do validation -- if, for instance, one of the text fields holds a value that is required and the user didn't fill it out, then this listener routine could detect that and, instead of allowing the program to continue, could popup a message dialog for the user telling him/her what is wrong.
Again, I don't know that this is any part of your problem, but I thought it might be. It is a concept that inexperienced UI programmers sometimes have trouble with, and it is so basic to UI programming that a lot of the tutorials just assume you already know it.
Upvotes: 1
Reputation: 209004
See this example for what I am talking about. Though I'd advise against using multiple frames, this will work for what yo are trying to do. As @MadProgrammer suggested, look into creating JDialog
s
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BaseFrame extends JFrame {
private String name;
private double value1;
private double value2;
private final JMenuBar jMenuBar = new JMenuBar();
private final JMenu jmOpen = new JMenu("Open");
JMenuItem menuItemCreateSeller = new JMenuItem("Create Seller");
InnerFrame innerFrame = new InnerFrame();
public BaseFrame() {
innerFrame.setResizable(false);
jmOpen.add(menuItemCreateSeller);
jMenuBar.add(jmOpen);
setJMenuBar(jMenuBar);
menuItemCreateSeller.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
innerFrame.setVisible(true);
}
});
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
Frame frame = new BaseFrame();
}
});
}
private class InnerFrame extends JFrame {
JPanel myPanel = new JPanel();
JLabel myLabel1 = new JLabel("Enter Name");
JLabel myLabel2 = new JLabel("Enter Longitude");
JLabel myLabel3 = new JLabel("Enter Latitude");
JLabel jlblStatus = new JLabel(" ");
JTextField myText1 = new JTextField(10);
JTextField myText2 = new JTextField(10);
JTextField myText3 = new JTextField(10);
JButton jbtPrint = new JButton("Print");
public InnerFrame() {
myPanel.add(myLabel1);
myPanel.add(myText1);
myPanel.add(myLabel2);
myPanel.add(myText2);
myPanel.add(myLabel3);
myPanel.add(myText3);
JPanel p1 = new JPanel(new GridLayout(1,2));
p1.add(jlblStatus);
p1.add(jbtPrint);
jlblStatus.setHorizontalAlignment(SwingConstants.LEFT);
jlblStatus.setForeground(Color.RED);
jbtPrint.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
name = myText1.getText();
value1 = Double.parseDouble(myText2.getText());
value2 = Double.parseDouble(myText3.getText());
System.out.println(name);
double total = value1 + value2;
System.out.println(total);
setVisible(false);
} catch (NumberFormatException ex) {
jlblStatus.setText("Invalid Input");
}
}
});
add(myPanel, BorderLayout.CENTER);
add(p1, BorderLayout.SOUTH);
setSize(180, 220);
setLocationRelativeTo(null);
setVisible(false);
}
}
}
Upvotes: 1