GUI project assistance - java

I am working on a project that I will be presenting. So far I have been doing well, but I have ran into a few problems that I can't figure out. I am building a basic GUI. There are 4 sections to choose from. The last menu option does not work. The drop down menu works, but I can't select an option. There is a small black arrow indicating a submenu?
I am having trouble finding what is the cause of that.
Also the GUI only saves one selection at a time. I need it to save all the choices and add everything at the end. What is the best way to accomplish that? If anyone has any input on how to improve my project all advice is welcome! Thank you all for your time and effort. It is much appreciated.
package guiproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Guiproject implements ActionListener{
JLabel jlabel;
Guiproject(){
JFrame frame = new JFrame("Vehicle Choices");//create new JFrame container
frame.setLayout(new FlowLayout()); //specify flowlayout for the layout manager
frame.setSize(570,400); // gives the frame its size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Terminates the program when user closes the application
jlabel = new JLabel();//creates a menu to display menu selections
JMenuBar menu = new JMenuBar();//creates menu bar
JMenu car = new JMenu("Choose Car");//creates field menu items.
JMenuItem sx240 = new JMenuItem("Nissan 240sx = $8,000$");//creates field menu items.
JMenuItem bmw = new JMenuItem(" BMW M5 = $15,000 ");//creates field menu items.
JMenuItem V8 = new JMenuItem(" Corvette = $22,000 ");//creates field menu items.
JMenuItem sky = new JMenuItem(" Skyline = $28,000 ");//creates field menu items.
JMenuItem ford = new JMenuItem(" Shelby Mustang = $25,500 ");//creates field menu items.
car.add(sx240); //add menu items to menu
car.add(bmw);//add menu items to menu
car.add(V8);//add menu items to menu
car.add(sky);//add menu items to menu
car.add(ford);//add menu items to menu
menu.add(car);
JMenu color = new JMenu("Choose Paint Color"); //creates the color selection option
JMenuItem red = new JMenuItem(" Cherry Red = $800.00 "); //craeted color choices
JMenuItem matte = new JMenuItem(" Matte Black = $1,700");//craeted color choices
JMenuItem pink = new JMenuItem(" Hot Pink = $975.00 ");//craeted color choices
JMenuItem purp = new JMenuItem (" Purple = $825.00 ");//craeted color choices
JMenuItem green = new JMenuItem(" Forest Green = $600.00");//craeted color choices
color.add(red); //adds choices to the menu
color.add(matte);//adds choices to the menu
color.add(pink);//adds choices to the menu
color.add(purp);//adds choices to the menu
color.add(green);//adds choices to the menu
menu.add(color);
JMenu drop = new JMenu("Choose Suspension type"); //creates option for suspension
JMenuItem stock = new JMenuItem(" Keep it Stock = $0.0 ");//creates menu choice
JMenuItem spring = new JMenuItem(" Basic Springs and Shocks = $ 150.00 ");//creates menu choice
JMenuItem coil = new JMenuItem(" Coilovers = $1,600 ");//creates menu choice
JMenuItem air = new JMenuItem(" Air Suspension = $ 3,000 ");//creates menu choice
JMenuItem low = new JMenuItem(" Lowering Springs = $575.00 ");//creates menu choice
drop.add(stock);//adds choice to the menu
drop.add(spring);//adds choice to the menu
drop.add(coil);//adds choice to the menu
drop.add(air);//adds choice to the menu
drop.add(low);//adds choice to the menu
menu.add(drop);//adds choice to the menu
JMenu engine = new JMenu("Choose performance parts"); //creates option for menu
JMenuItem stock1 = new JMenu(" Keep It Stock = $0.0 "); //creates menu choice
JMenuItem cam = new JMenu(" Upgrade Camshafts $475.00 ");//creates menu choice
JMenuItem turbo = new JMenu(" Turbo = $1,250.00 ");//creates menu choice
JMenuItem sup = new JMenu(" Supercharger = $2,800.00 ");//creates menu choice
JMenuItem twin = new JMenu(" Twin Turbo = $2,200.00 ");//creates menu choice
engine.add(stock1);//adds choice to the menu
engine.add(cam);//adds choice to the menu
engine.add(turbo);//adds choice to the menu
engine.add(sup);//adds choice to the menu
engine.add(twin);//adds choice to the menu
menu.add(engine);//adds choice to the menu
sx240.addActionListener(this); //adds action listener to menu items
bmw.addActionListener(this);//adds action listener to menu items
V8.addActionListener(this);//adds action listener to menu items
sky.addActionListener(this);//adds action listener to menu items
ford.addActionListener(this);//adds action listener to menu items
red.addActionListener(this);//adds action listener to menu items
matte.addActionListener(this);//adds action listener to menu items
pink.addActionListener(this);//adds action listener to menu items
purp.addActionListener(this);//adds action listener to menu items
green.addActionListener(this);//adds action listener to menu items
stock.addActionListener(this);//adds action listener to menu items
spring.addActionListener(this);//adds action listener to menu items
coil.addActionListener(this);//adds action listener to menu items
air.addActionListener(this);//adds action listener to menu items
low.addActionListener(this);//adds action listener to menu items
stock1.addActionListener(this);//adds action listener to menu items
cam.addActionListener(this);//adds action listener to menu items
turbo.addActionListener(this);//adds action listener to menu items
sup.addActionListener(this);//adds action listener to menu items
twin.addActionListener(this);//adds action listener to menu items
frame.add(jlabel); //adds label to content pane
frame.setJMenuBar(menu);//adds menu bar to frame
frame.setVisible(true);//displays frame
}
public void actionPerformed(ActionEvent ae){// handles menu item action events
String string = ae.getActionCommand();// gets action command to menu section
String string1 = ae.getActionCommand();
if(string.equals("Exit"))System.exit(0);//Exits the program when user chooses exit.
jlabel.setText(string+ " Selected ");//displays selected choice
jlabel.setText(string1+ " selected ");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Guiproject();
}
});
}
}

JMenuItem stock1 = new JMenu(" Keep It Stock = $0.0 "); //creates menu choice
JMenuItem cam = new JMenu(" Upgrade Camshafts $475.00 ");//creates menu choice
JMenuItem turbo = new JMenu(" Turbo = $1,250.00 ");//creates menu choice
JMenuItem sup = new JMenu(" Supercharger = $2,800.00 ");//creates menu choice
JMenuItem twin = new JMenu(" Twin Turbo = $2,200.00 ");//creates menu choice
You define your objects a JMenuItem, but you are creating a JMenu. Since JMenu extends JMenuItem, this is legal, but not what you want.
You want:
JMenuItem stock1 = new JMenuItem(" Keep It Stock = $0.0 "); //creates menu choice
JMenuItem cam = new JMenuItem(" Upgrade Camshafts $475.00 ");//creates menu choice
JMenuItem turbo = new JMenuItem(" Turbo = $1,250.00 ");//creates menu choice
JMenuItem sup = new JMenuItem(" Supercharger = $2,800.00 ");//creates menu choice
JMenuItem twin = new JMenuItem(" Twin Turbo = $2,200.00 ");//creates menu choice

Related

Menu bar not showing in JFrame

I've added a a custom JPane and a custom JMenuBar to my JFrame. The JPane shows up just fine, but not the JMenuBar. I've defined the frame and the menu bar in different classes
Here's the frame class:
public class pixelFrame { //This frame will hold the a pixelPane for art creation. This will also hold a menu bar, defined in another file.
pixelPane editingArea;
pixelMenuBar menuBar;
public pixelFrame()
{
EventQueue.invokeLater(new Runnable() { //create a new thread at runtime
#Override
public void run() { //when the program runs, do the following
JFrame frame = new JFrame("Pixel Art Creator"); //create a new JFrame (similar to a regular window), and give it the following title
editingArea = new pixelPane();
menuBar = new pixelMenuBar();
frame.add(editingArea); //add a pixelPane named editingArea to the JFrame
frame.setJMenuBar(menuBar); //adds a menu to the JFrame
frame.pack(); //make the window big enough to fit all components (in this case, editingArea)
frame.setLocationRelativeTo(null); //set window to the center of the screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Stop the thread and terminate the program.
frame.validate();
frame.setVisible(true); //You can see the window.
menuBar.setVisible(true);
}
});
}
public static void main(String[] args)
{
pixelPane.gridEnabled = true; //changing another variable from pixelPane just for testing.
new pixelFrame(); //create an instance of pixelFrame.
}
}
And here's the menu bar:
public class pixelMenuBar extends JMenuBar {
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar;
JMenu file, tool;
JMenuItem save, load, changeColor;
JCheckBoxMenuItem gLines;
public pixelMenuBar() {
menuBar = new JMenuBar();
//creates a "File" menu in the menu bar
file= new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
menuBar.add(file); //add the menu to the menu bar
//creates a "Tools" menu in the menu bar
tool = new JMenu("Tools");
tool.setMnemonic(KeyEvent.VK_T);
menuBar.add(tool);
//Menu items that go under the File menu
save = new JMenuItem("Save File"); //save and export the image as an .svg file
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_S)); //a Ctrl+S hotkey. Handy dandy!
file.add(save);//adds the Save button to the File menu
load = new JMenuItem("Load File"); //load the file into BufferedImage, make it into a JLabel and add it to the panel.
load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_L));//a Ctrl+L hotkey. Also handy dandy!
file.add(load);//adds the Load button to the File menu
//Menu items that will go under the Tools menu
gLines = new JCheckBoxMenuItem("Gridlines"); //creates a new checkbox for enabling grid lines. will toggle pixelPane.gridEnabled
gLines.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_G));
tool.add(gLines);//adds the gridlines tool to the Tools menu
tool.addSeparator(); //adds a separating line in the Tools menu. Organization.
changeColor = new JMenuItem("Change Color");//creates a new menu item that will let the user the color of the pixel. Uses a color picker
tool.add(changeColor);//adds Change Color to the Tools menu
}
}
Am I not adding something to the UI correctly? I double checked that I'm using addJMenuBar() and not addMenuBar().
First of all class names SHOULD start with an upper case character. Have you ever seen a class in the Java API that doesn't? Follow Java conventions. Learn by example.
public class pixelMenuBar extends JMenuBar
{
...
public pixelMenuBar()
{
menuBar = new JMenuBar();
Your class "is a " JMenuBar, but the first thing you do in the class is create a JMenuBar.
Don't create the JMenuBar!
Just add the menu items to the JMenuBar class itself:
//menuBar.add(file); //add the menu to the menu bar
add(file); //add the menu to the menu bar
In reality there is no need to even have a PixelMenuBar class, since you are not adding any new functionality to the JMenuBar. Just add a method to your main class like createMenuBar(...) that creates the JMenuBar and adds the JMenu/JMenuItem objects.

java multi level JPopupMenu

I've created a JPopupMenu with two items (add,remove). I want "addItem" to have a sub popup menu. The hierarchy is like this:
add
pizza
cake
...
remove
my code:
JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("remove"));
JMenuItem addItem = new JMenuItem("add");
menu.add(addItem);
addItem.add(new JPopupMenu()); // it is not working for me
Once I move the mouse close to "add item", the menu disappears.
please help me to build this popup menu.
Use a JMenu (sub class of JMenuItem).
JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("remove"));
JMenuItem addItem = new JMenu("add");
menu.add(addItem);
addItem.add(new JMenuItem("pizza"));
addItem.add(new JMenuItem("cake"));

How to remove arrow on JMenuItem?

I need help with JMenuItem. I didn't write any line about arrow on JMenuItem. So how to disable that? Here's the image:
Let me try to reproduce the issue.
Add JMenuItem in JMenu and then in JMenuBar.
sample code: (No issues)
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A Menu");
JMenu submenu = new JMenu("A submenu");
JMenuItem menuItem = new JMenuItem("Another item");
submenu.add(menuItem); //comment this line and look the output
menu.add(submenu);
menuBar.add(menu);
Add JMenu directly in JMenuBar without any JMenuItem.
sample code: (This creates problem)
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A Menu");
JMenu submenu = new JMenu("A submenu");
menu.add(submenu);
menuBar.add(menu);
Add JMenuItem directly in JMenuBar.
sample code: (No issues)
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A Menu");
JMenuItem menuItem = new JMenuItem("Another item");
menu.add(menuItem);
menuBar.add(menu);
Now the problem is crystal clear, if it's leaf node then add JMenuItem instead of JMenu in JMenuBar.
Read more...
I had the same issue with JavaFX with my MenuItems having arrows all over the place.
Thanks to #Braj response I realized that I was creating my items this way:
MenuItem saveMenu = new Menu();
instead of
MenuItem saveMenu = new MenuItem();
This, of course, works in Java thanks to polymorphism but created this side effect in my app. That being said, this can help you better organize your menus using a tree structure like Save > Save to File / Save to DB / Save As...

Change the title text of JMenuItem upon click?

I am adding a JMenuItem (Show History) that will toggle the appearance of a JPanel upon click. But after doing so, I want to change the title of that menu item to state the opposite action (Hide History). Is there a way to change just the text for that menu item, or must I remove the old JMenuItem and add a new one?
JMenuItem history = new JMenuItem("Show History");
history.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//code here to show the history
//history.changeText("Hide History") OR viewMenu.remove(history) and create/add new one
}
});
viewMenu.add(history);
So this is what you do:
history.setText("Hide History");
And make history final.

a GUI java program needing an action event button

I am making a simple java program that represents a Microsoft Word menu bar, and in the file menu I add an exit button... it does nothing.
I need your help to tell me what to do to make the exit button actually exit and close the program.
Here is my code:
public class MicrosoftWordMenu {
public static void main(String [] args)
{
JPanel panel = new JPanel();
JFrame frame = new JFrame("Microsoft Word");
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu1 = new JMenu("Edit");
JMenu menu2 = new JMenu("View");
JMenu menu3 = new JMenu("Insert");
JMenu menu4 = new JMenu("Format");
JMenu menu5 = new JMenu("Tools");
JMenu menu6 = new JMenu("Table");
JMenu menu7 = new JMenu("Window");
JMenu menu8 = new JMenu("Help");
frame.add(bar, BorderLayout.NORTH);
frame.add(panel);
bar.add(menu);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
bar.add(menu4);
bar.add(menu5);
bar.add(menu6);
bar.add(menu7);
bar.add(menu8);
JMenuItem menuitem = new JMenuItem("New...");
JMenuItem menuitem1 = new JMenuItem("Open...");
JMenuItem menuitem2 = new JMenuItem("Close");
JMenuItem menuitem3 = new JMenuItem("Save");
JMenuItem menuitem4 = new JMenuItem("Save as...");
JMenuItem menuitem5 = new JMenuItem("Save as web page...");
JMenuItem menuitem6 = new JMenuItem("Web page preview ");
JMenuItem menuitem7 = new JMenuItem("Print ");
JMenuItem menuitem8 = new JMenuItem("Exit");
menu.add(menuitem);
menu.add(menuitem1);
menu.add(menuitem2);
menu.add(menuitem3);
menu.add(menuitem4);
menu.add(menuitem5);
menu.add(menuitem6);
menu.add(menuitem7);
menu.add(menuitem8);
frame.setSize(600,100);
frame.setVisible(true);
}
}
Also consider using Action to let components share functionality, as shown here.
Calling System.exit(0) on menu selection would do just fine.
At the moment you're just creating the GUI element, but they're basically empty shells. In particular, you've created items that appear on the menu, but you haven't added any behaviour to those items. (Since you haven't told your program anywhere what to do when an item is clicked, what did you think would happen)?
In order to provide this behaviour, you'll need to register an ActionListener, which will be called when something happens on an element, and will let you take an appropriate action at that point (such as calling System.exit(0). See the tutorial on Writing [Swing] Event listeners for details.
I always extend the AbstractAction class (which implements the ActionListener interface) which allows you to reuse your actions. By extending the AbstractAction, the actionPerformed(ActionEvent e) method will be invoked very time your button is clicked.
public class CloseAction extends AbstractAction {
public CloseAction() {
super("Close");
}
public actionPerformed(ActionEvent e) {
System.exit(1);
}
}
Now, to apply the above action to one of your buttons, you simply need to follow the below piece of code.
CloseButton.setAction(new CloseAction());
The close button will now shut down your application each time it gets pressed.
Use the ExitAction defined in Closing an Application. Then clicking on the Exit menu item will be just like clicking on the Close icon at the top right of the window. This allows for consistency when closing an application in case you ever need to do close processing.
Edit:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
// JMenuItem menuitem8 = new JMenuItem("Exit");
JMenuItem menuitem8 = new JMenuItem( new ExitAction() );

Categories