JCheckBoxMenuItem cannot be added to Menu - java

The src code of JCheckBoxMenuItem has the following class head:
public class CheckboxMenuItem extends MenuItem implements ItemSelectable, Accessible
So when a class extends another one it means that it should also inherit its type. Or not?
My problem is that I can't add the JCheckBoxMenuItem to a JMenu (it needs a MenuItem to be added).
The following code does not work for me:
private void initMenu()
{
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnFile = new JMenu("File");
menuBar.add(mnFile);
mnAudio = new JMenu("Audio");
menuBar.add(mnAudio);
mnitmQuit = new JMenuItem("Quit");
mnFile.add(mnitmQuit);
rmnitmNoice = new CheckboxMenuItem("Noice");
// Eclipse gives error Message below *
mnAudio.add(rmnitmNoice);
rmnitmNuke = new JRadioButtonMenuItem("Nuke");
// Same here
mnAudio.add(rmnitmNuke);
}
The method add(JMenuItem) in the type JMenu is not applicable for the
arguments (CheckboxMenuItem)
I m quite sure that I used this one before and had no problems with it. But since I started to use Marven I get strange behaviour sometimes (other example: #Override does not work for Methods, that implement interfaces anymore)
Anyone with the same issue or the solution?

As VGR explained in his comment: The mistake was trying to add a CheckboxMenuItem instead of a JCheckBoxMenuItem.

Related

Why is the program throwing an error "cannot find symbol" at setVisible in my showPanel method?

Why is the setVisible method throwing an error saying symbol not found in my showPanel method?
It does not make sense as I am referencing a JPanel stored in an ArrayList so it should be able to use setVisible.
public class mainFrame extends javax.swing.JFrame {
/**
* Creates new form mainFrame
*/
private ArrayList list;
public mainFrame() {
initComponents();
this.setSize(500,500);
int h=this.getHeight();
int w=this.getWidth();
homePanel homePnl = new homePanel();
this.add(homePnl);
homePnl.setLocation(0,0);
homePnl.setSize(w,h);
homePnl.setVisible(true);
DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
this.add(infoPanel);
infoPanel.setLocation(0,0);
infoPanel.setSize(w,h);
atomServerPanel atomPnl = new atomServerPanel();
this.add(atomPnl);
atomPnl.setLocation(0,0);
atomPnl.setSize(w,h);
autoDeploymentPanel autoPnl = new autoDeploymentPanel();
this.add(autoPnl);
autoPnl.setLocation(0,0);
autoPnl.setSize(w,h);
list = new ArrayList<>();
list.add(homePnl);
list.add(infoPanel);
list.add(atomPnl);
list.add(autoPnl);
this.pack();
}
public void showPanel(int panelNum){
list.get(1).setVisible(true);
}
private ArrayList list;
You didn't specify the type of Object that will be added to the ArrayList. So by default get() method will return an instance of Object. There is no setVisible(…) method for an Object
When you define the ArrayList you should be using:
private ArrayList<Component> list;
Now the compiler knows you are adding Component instances to the ArrayList.
In fact, the compiler will check to make sure you only add Component.
It will also get rid of the warning messages when you compile.
Also class names should start with an upper case character. Sometimes you do and sometimes you don't:
DeploymentInfoPanel infoPanel = new DeploymentInfoPanel();
...
atomServerPanel atomPnl = new atomServerPanel();
...
autoDeploymentPanel autoPnl = new autoDeploymentPanel();
Notice how the forum highlights properly named classes making the code easier to read?
Follow Java conventions and be consistent.
Finally, to display multiple panels in the same area of the frame you should be using a Card Layout.

jMenuItem doesn't appear

I've just starting using Java Swing and I have a issue.
I tried to do a simple menuBar and a menuItem 'Exit', but before linking the button to the action the menuItem appeared, now that I've linked the button to a System.exit(0) action it disappeared. Help?
The code is the following:
in MainPanel (the autogenerated code from swing is excluded):
public void init() {
initComponents();
initActions();
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private void initActions() {
this.menuItemExit.setAction(Application.getInstance().getPanelControl().getActionExit());
}
In PanelControl:
public class PanelControl {
private Action actionExit;
public Action getActionExit() {
return actionExit;
}
public class ActionExit extends AbstractAction{
public ActionExit(){
putValue(Action.NAME, "Exit");
putValue(Action.SHORT_DESCRIPTION, "Exit from the application");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl e"));
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
In Application:
private void init() {
viewMainPanel = new MainPanel();
controlPanel = new ControlPanel();
viewMainPanel.init();
}
i think the problem is somewhere in here but i can't figure out where. any help?
(there's other code but i just put the more relevant part, also i translated the code from italian so i'm sorry if there are any problems or a few names dont match up)
private Action actionExit;
public Action getActionExit() {
return actionExit;
}
Your actionExit variable is null.
Nowhere in your code do you create an instance of your ActionExit class.
Somewhere you need:
actionExit = new ActionExit();
Your design seems a bit complicated, I have no idea why you have a panel just to create an instance of the ActionExit class.
I would suggest you just create the ActionExit instance in your main class and get rid of the PanelControl class.
Instead of using an IDE to generate confusing code you should consider learning how to write the code yourself so you can better structure your classes. Read the section from the Swing tutorial on How to Use Menus for a working example to get you started.
A menu item has to be added to a Native Java Swing component. You have to add it to a JFrame. You can't add a MenuItem to a Panel. The Parent 'root' container in any Java Swing application is 'native' and a JFrame. Everything else in that container is 'drawn' into the rectangle using the look and feel of your choosing.
Then you CREATE a MenuItem using your TAbstractAction item. That object CAN be used to create a JButton, JMenuItem or ToolBar button. Keeping a reference to your TAbstractAction in your code, you can enable/disable the object and it implements an 'observable' pattern where it will enable/disable ALL UI controls you used to build with it. I actually wrote a Java Swing framework for doing Java Applications. It used to be on the Sun Open Source web site. If you wish I can put it up on GitLab for you to play with. Java Swing is nice but JavaFX should be the long term goal for UI on a JVM.
In your JFrame object you need to do this:
_menuBar = new JMenuBar();
// add controls to the frame
setJMenuBar(_menuBar);
Then you need to add your 'exitMenuItem' to your _MenuBar control.
Cheers

JTextArea shows up but for further action on it I receive empty

I have a class "MainFrame1" that extends a JFrame and also another class that is a file chooser. Whenever I press one of the JMenuItems in MainFrame1 class, I want the file chooser to open up and load up the text of the chosen file on a JTextArea that was created in MainFrame1 class. This works perfectly fine as I created a separate class implementing an ActionListener. Now my problem is that when I press another JMenuItem I want to do something else to the text in the JTextArea. I have implemented another ActionListener for that in a different class but the problem is that the JTextArea seems to be empty when I do that although I can see the text in there. Thanks in advance.
This is how I have created the JTextArea in the MainFrame1:
showAction = new JTextArea(10,10);
showAction.setEditable(false);
showAction.setFont(new Font("Arial", Font.BOLD, 12));
add(showAction, BorderLayout.NORTH);
And this is my second ActionListener class (also, whenever the text of a file is printed in the JTextArea, the text "loaded up." will also be printed) and I always get the else branch.
public class TransformController implements ActionListener{
MainFrame1 mf;
public TransformController(MainFrame1 mf) {
this.mf = mf;
}
#Override
public void actionPerformed(ActionEvent e) {
String text = mf.showAction.getDocument().toString();
if(text.contains("loaded up.")) {
char[] charText = text.toCharArray();
Parser parser1 = new Parser(charText);
parser1.packageVisitor();
}
else {
System.out.println("Load up a Java file first!");
}
}
}
This seems to be mostly a debugging question: First, find out what's in showAction.getDocument() to see if your menu item just isn't loading it right. Then check (with an IDE or via toString()) that mf.showAction really is the same object in the two cases.
Structurally, there's nothing in Java that prevents you from having a reference to the same JTextArea in two parts of the code, and reading the text out of it for different purposes.

java null pointer exception JMenuBar [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
Im stuck today with a java error on an eclipse 3.8 platform.
I try to generate a simple MenuBar on a JFrame. The JMenuBar contains the JMenu("Help") and the JMenu contains a JMenuItem("Quit"). The class extends a JFrame. With the function (this.)setJMenuBar(MenuBar); I try to set my MenuBar to my JFrame, which should work fine as long I just have one MenuBar.
GUI-class code:
import javax.swing.*;
import java.awt.*;
public class Taschenrechner extends JFrame{
//Instanzvariablen im Zusamenhang mit dem Menu
private JMenuBar MenuBar;
private JMenu MenuHelp;
private JMenuItem MenuHelpQuit;
public Taschenrechner() {
super();
//Flaeche festlegen
this.setSize(300,300);
//Menukomponenten definieren
MenuHelp= new JMenu("Help");
MenuHelpQuit=new JMenuItem("Quit");
//Menukomponenten zusammensetzen
MenuHelp.add(MenuHelpQuit);
MenuBar.add(MenuHelp);
this.setJMenuBar(MenuBar);
}
}
my main-class code (declare objekt and setVisible):
public class Taschenrechnerstart {
public static void main(String[] args) {
Taschenrechner taschenrechner1 = new Taschenrechner();
taschenrechner1.setVisible(true);
}
}
Now on starting the code I get a NullPointerException-error in the GUI-class on line:
MenuBar.add(MenuHelp);
and a NullPointerException-error in the main-class on the line:
Taschenrechner taschenrechner1 = new Taschenrechner();
Does someone have an idea why my code isn't working?
MenuBar.add(MenuHelp);
You are accessing the MenuBar variable without initializing it.
You are missing a
MenuBar = new JMenuBar (..);
BTW, your code would be more readable if you use Java naming conventions (variables should start with a lower case letter).

How to make WindowBuilder create fields that are not nested in Eclipse, Java

I am using WindowBuilder for Eclipse Java.
When I make a Button or a Menu (or anything), it creates the fields nested inside of the constructor. Is there any way to make them fields and then initialized in the constructor so I can use them outside of the constructor? Thank you.
ie: FROM
public GUIFrame() {
JMenuBar menuBar = new JMenuBar();
}
TO
public JMenuBar menubar;
public GUIFrame() {
menuBar = new JMenuBar();
}
There's a button to convert an element from "local to field".
If you want all components to be fields by default: Windows -> Preferences -> WindowBuilder -> Swing -> Code Generation -> Find "Variable generation" and select the "Field" tab.

Categories