JMenu-JMenuItem doesn't appear - java

My problem with this code is that when I run it the JMenuItem doesn't appear. I'm so confused! Anyone can help?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewClass extends JFrame implements ActionListener{
JFrame window = new JFrame("Ηλεκτρονικό Έντυπο Υπερωριών");
JMenu menu = new JMenu("Example");
JMenuItem menuItem = new JMenuItem("hi");
public NewClass(){
window.setSize(200,200);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu.add(menuItem);
menuItem.addActionListener(this);
window.add(menu);
window.pack();
window.setVisible(true);
}
public static void main(String []args){
NewClass example = new NewClass();
}
#Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}

You need to add a menubar to the window. To this menubar you add your menu(s). Take a look at oracle's tutorial on How to Use Menus.

Related

How to implement the ActionListener to get the Exit Button to work

Here is my code, but the error shows that the implemented ActionListener is not correct. I also declared the buttons so how do I make the system exit? What did I do wrong? Thanks in advance
import javax.swing.*;
import java.awt.*;
import java.awt.FlowLayout;
public class MyFrame extends JFrame implements ActionListener {
public MyFrame() {
// set flow layout for the frame
this.getContentPane().setLayout(new FlowLayout());
JButton ExitBtn = new JButton();
ExitBtn.setText("Exit");
JButton Find = new JButton("Find");
JButton Clear = new JButton("Clear");
// add buttons to frame
add(ExitBtn);
add(Find);
add(Clear);
}
public void actionPerformed(ActionEvent e){
System.exit(0);
ExitBtn.addActionListener(this);
}
public static void main(String[] args) {
MyFrame mf = new MyFrame();
mf.pack();
mf.setVisible(true);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
onClick:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
I think you should move the ExitBtn.addActionListener(this) call to the constructor of MyFrame class so that it looks like this:
JButton ExitBtn = new JButton();
ExitBtn.setText("Exit");
ExitBtn.addActionListener(this)
and actionPermormed method looks like this:
#Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}

Nothing appears when I try to create a JFrame. (JButtons, JMenuBar etc.)

I'm working on a pathfinder program which uses GUI. I tried to run the program before I added some stuff and it showed everything fine. But after working on it some more it stopped showing the buttons and the menu bar.
Here is the code. Some variable names are in Swedish as well, but I hope that it won't be an issue. (Please take in mind that the program is far from finished.)
Thank you in advance!
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Pathfinder extends JFrame {
JButton hittaVäg, visaFörbindelse, nyPlats, nyFörbindelse, ändraFörbindelse;
JMenuBar menyBar;
JMenuItem ny, avsluta, hittaVägMeny, visaFörbindelseMeny, nyPlatsMeny, nyFörbindelseMeny, ändraFörbindelseMeny;
String str = System.getProperty("user.dir");
JFileChooser jfc;
BildPanel Bild = null;
Pathfinder(){
super("PathFinder");
setLayout(new BorderLayout());
setSize(590, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
jfc = new JFileChooser(".");
JPanel norra = new JPanel();
add(norra, "norra");
JButton hittaVäg = new JButton("Hitta väg");
JButton visaFörbindelse = new JButton("Visa förbindelse");
JButton nyPlats = new JButton("Ny plats");
JButton nyFörbindelse = new JButton("Ny förbindelse");
JButton ändraFörbindelse = new JButton("Ändra förbindelse");
norra.add(hittaVäg);
norra.add(visaFörbindelse);
norra.add(nyPlats);
norra.add(nyFörbindelse);
norra.add(ändraFörbindelse);
hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new NyFörbindelseLyss());
JMenuBar menyBar = new JMenuBar();
setJMenuBar(menyBar);
JMenu arkivMeny = new JMenu("Arkiv");
JMenu operationerMeny = new JMenu("Operationer");
menyBar.add(arkivMeny);
menyBar.add(operationerMeny);
JMenuItem ny = new JMenuItem("Ny");
JMenuItem avsluta = new JMenuItem("Avsluta");
arkivMeny.add(ny);
arkivMeny.add(avsluta);
ny.addActionListener(new NyLyss());
avsluta.addActionListener(new AvslutaLyss());
JMenuItem hittaVägMeny = new JMenuItem("Hitta väg");
JMenuItem visaFörbindelseMeny = new JMenuItem("Visa förbindelse");
JMenuItem nyPlatsMeny = new JMenuItem("Ny plats");
JMenuItem nyFörbindelseMeny = new JMenuItem("Ny förbindelse");
JMenuItem ändraFörbindelseMeny = new JMenuItem("Ändra förbindelse");
operationerMeny.add(hittaVägMeny);
operationerMeny.add(visaFörbindelseMeny);
operationerMeny.add(nyPlatsMeny);
operationerMeny.add(nyFörbindelseMeny);
operationerMeny.add(ändraFörbindelseMeny);
hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new ÄndraFörbindelseLyss());
}
class HittaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class VisaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyPlatsLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class ÄndraFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
int svar = jfc.showOpenDialog(Pathfinder.this);
if (svar == JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String filnamn = f.getAbsolutePath();
if (Bild != null)
remove(Bild);
Bild = new BildPanel(filnamn);
add(Bild, BorderLayout.CENTER);
validate();
repaint();
pack();
}
}
}
class AvslutaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
public static void main (String[] args){
new Pathfinder();
}
}
One of the issues is IllegalArgumentException at line
add(norra, "norra");
The layout of the frame's content pane is set to BorderLayout, but this layout does not understand "norra" constraint. See How to Use BorderLayout for more details and examples.
Also, you should call setVisible once all the components are added and initialized.
Call setVisible(true) on the JFrame after adding all components, not before. The order should be:
Add components
Call pack() on the JFrame
Then call setVisible(true) on the JFrame.
You need to first add the components and then call setVisible(true).
So the order matters here. Try it, see if it helps.

JFrame not showing up

I have one class called TabBuilder which I use to create the interface of my application, but I'm going through a weird problem. If I try to run the code as it's below it will draw the mainScreen but if I request the SearchScreen from the BarMenu it doesn't show up. If I try to execute only SearchScreen by itself (calling its builder) within the public static void main (String[] args) MainString it wont show up too. but If go to the request event and tip TabBuilder tb = new TabBuilder(); tb.requestTab();the screen will show up as it should be. So, what might be wrong? Thanks in advance
MainScreen:
public class MainScreen{
public MainScreen()
{
TabBuilder tb = new TabBuilder();
tb.mainTab();
}
}
SearchScreen:
public class SearchScreen{
public void SearchScreen(){
TabBuilder tb = new TabBuilder();
tb.requestTab();
}
}
TabBuilder:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TabBuilder implements ActionListener {
JTabbedPane tabbedPane = new JTabbedPane();
JMenuItem close, request;
protected JTextField txtrequest = new JTextField();
JButton btrequest = new JButton();
protected JFrame requestFrame = new JFrame();
public void TabBuilder(){
}
public void mainTab(){
JMenuBar bar;
JMenu file, register;
JFrame mainFrame = new JFrame();
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Request");
close= new JMenuItem("Close");
close.addActionListener(this);
request= new JMenuItem("Request Query");
request.addActionListener(this);
bar.add(file);
bar.add(register);
file.add(close);
register.add(request);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void requestTab(){
JLabel lbrequest;
JPanel requestPane;
btrequest= new JButton("request");
lbrequest= new JLabel("Type Keywords in english to be requested below:");
txtrequest= new JTextField();
requestPane=new JPanel();
requestPane.setBackground(Color.gray);
requestPane.add(lbrequest);
requestPane.add(txtrequest);
requestPane.add(btrequest);
requestPane.setLayout(new GridLayout(3,3));
btrequest.setEnabled(true);
requestFrame.add(requestPane);
requestFrame.setTitle("SHST");
requestFrame.setSize(400, 400);
requestFrame.setVisible(true);
requestFrame.setDefaultCloseOperation(1);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==request){
TabBuilder tb = new TabBuilder();
tb.requestTab();
}
}
public static void main (String[] args){
MainScreen m = new MainScreen();
}
}
The constructor of SearchScreen was settled as void. That caused nothing to return as object when calling the constructor. Newbie failure but simple solution.

Example Program JMenubar on JInternalFrame when i Maximize the JInternalFrame

Hi I need an Example program in which
When i maximize the JInternalFrame the JMenuBar of JFrame should set on JInternalFrame
and When i minimize the JInternalFrame again the JMenuBar should leave JinternalFrame and set
to JFrame as shown below
Please provide me an Example Program in Java Swing
Seems to work fine, please post SSCCE to show specific problem:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
JDesktopPane jdpDesktop;
static int openFrameCount = 0;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
I also have similar problem. It is a bug in JInternalFrame LookAndFell. JInternalFrames using Windows L&F, should be
more Windows like. If I maximize a JInternalWindow,
that window is not working as in Windows.
Its top bar should be one with the MDI.
Meus and Toolbars should be inside.
Maximized Internal frames should not have their
own border.
Visit
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4102061
for datails.

Java programming style

its a maybe a dumb question but i am curious to understand this thing....
The below code works but the one below that doesn't work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Menu extends JFrame
{
public Menu()
{
JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon("exit.png");
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem fileClose = new JMenuItem("Close",icon);
fileClose.setMnemonic(KeyEvent.VK_C);
fileClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{System.exit(0);}
});
file.add(fileClose);
menubar.add(file);
setJMenuBar(menubar);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
}
public static void main (String args[])
{
new Menu();
}
}
The below one doesn't work
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Menu extends JFrame
{
public Menu()
{
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon("exit.png");
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem fileClose = new JMenuItem("Close",icon);
fileClose.setMnemonic(KeyEvent.VK_C);
fileClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{System.exit(0);}
});
file.add(fileClose);
menubar.add(file);
setJMenuBar(menubar);
}
public static void main (String args[])
{
new Menu();
}
}
I thought Java supports free style coding !!! (that's what it says in my Book)
PS: Please Someone Edit the Tile to suit the question correctly, I am not sure what to put in Title.
The issue in your 2nd code sample is that you are calling setVisible before you actually add stuff to the GUI. Your saying "Here's some stuff, now show" while in the second one your saying "Show, now here's some stuff"
Fix: Move setVisible to the end of the constructor

Categories

Resources