I need to create a JMenuBar that will be shown under of JTabbedPane named as shapes. When I changed tab, menu bar will not be shown in the other tab. You can see what I mean from images.
I created a menu bar and add it to JFrame but that is not I mean. If you did not understand a point let me explain.
public class Gui extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JMenuBar menuBar;
JMenu createShapes, display, help;
JMenuItem RandomShapes, Rectangle, Circle, Square;
public void createFrame() {
frame = new JFrame();
frame.setSize(1000, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setFocusable(false);
setVisible(true);
setSize(1000, 600);
}
public void createMenus() {
menuBar = new JMenuBar();
//frame.setJMenuBar(menuBar);
createShapes = new JMenu("CreateShapes");
menuBar.add(createShapes);
display = new JMenu("Display");
menuBar.add(display);
help = new JMenu("Help");
menuBar.add(help);
RandomShapes = new JMenuItem("Random Shapes");
createShapes.add(RandomShapes);
Rectangle = new JMenuItem("Rectangle");
createShapes.add(Rectangle);
Circle = new JMenuItem("Circle");
createShapes.add(Circle);
Square = new JMenuItem("Square");
createShapes.add(Square);
}
public Gui() {
createFrame();
createMenus();
frame.add(this);
//===frames part
tabbedPane = new JTabbedPane();
frame.getContentPane().add(tabbedPane);
JPanel gui2 = new JPanel();
tabbedPane.addTab("Shapes", this);
tabbedPane.addTab("Images", gui2);
//===endframespart
}
}
The other tab image
Shapes tab image
Actually, you cannot set menu bar to JTabbedPane.
You need to add JInternalFrame inside one of the tabs of JTabbedPane, then
you can call setJMenuBar of JInternalFrame.
Here is a simple example:
JInternalFrame jInternalFrame = new JInternalFrame();
jMenuBar = new javax.swing.JMenuBar();
jMenu1 = new JMenu("Save");
jMenu2 = new JMenu("Open");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jInternalFrame.setJMenuBar(jMenuBar);
tabbedPane.addTab("tab3", jInternalFrame);
Related
I'm working on a Java desktop Application that uses multiple Jpanels as cards in a CardLayout, the problem is that when I switch between the cards the JmenuBar disappears totally or just parts of it.I have a mainFramewhich holds all the Jpanels and the JmenuBar, the other Jpanels have some texts and JButtons and JLabels Here's how I'm implementing this :
This is the Frame that holds everything mainFrame.java
public class mainFrame extends JFrame {
JPanel cards ;
menubar menu= new menubar();
mainPanel card1 = new mainPanel();
Ajout card2= new Ajout();
//ViewAjoutEnf card3= new ViewAjoutEnf();
public mainFrame(){
setResizable(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
this.add(menu);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cards = new JPanel(new CardLayout());
cards.add(card1, "Card 1");
cards.add(card2, "Card 2");
// cards.add(card3, "Card 3");
getContentPane().add(cards);
}
This is the JmenuBar class menubar.Java:
public class menubar extends JMenuBar{
JMenu menuouvrir = new JMenu("ملف");
JMenuItem ajoutbase = new JMenuItem("فتح");
JMenuItem quiter = new JMenuItem("خروج ");
JMenuItem motpass = new JMenuItem("تبديل كلمة السر");
JMenu menuajout = new JMenu("ادخال ");
JMenuItem ajoutprodui = new JMenuItem("ادخال منتوج");
JMenuItem listproduit = new JMenuItem("لائحة المنتوجات");
JMenu menusortie = new JMenu("اخراج");
JMenuItem sortiproduit = new JMenuItem("اخراج منتوج");
JMenu retour = new JMenu("عودة ");
JMenu menuapropos = new JMenu("?");
public menubar (){
this.setBounds(0, 0, 1370, 30);
this.add(Box.createHorizontalGlue());
this.setFont(new Font("sans-serif", Font.PLAIN, 12));
menuouvrir.add(ajoutbase);
menuouvrir.add(motpass);
menuouvrir.addSeparator();
menuouvrir.add(quiter);
menuajout.add(ajoutprodui);
menuajout.add(listproduit);
menusortie.add(sortiproduit);
this.add(menuapropos);
this.add(retour);
this.add(menusortie);
this.add(menuajout);
this.add(menuouvrir);
this.setVisible(true);
}
and these are the Two Jpanels i have:
mainPanel.Java
public class mainPanel extends JPanel {
JButton but = new JButton("dsdffd");
public mainPanel(){
this.setLayout(null);
this.add(but);
but.setBounds(70,500,70,70);
this.setBackground(Color.white);
this.setVisible(true);
}
}
and Ajou.Java:
public class Ajout extends JPanel{
JButton but = new JButton("dsdffd");
public Ajout(){
this.setLayout(null);
this.add(but);
but.setBounds(70,500,70,70);
this.setBackground(Color.white);
this.setVisible(true);
}
}
Don't extend JMenuBar. There is no reason to do this. You create an instance of JMenuBar and and add JMenu instances to the menu bar.
Don't use a null layout. A JMenuBar has its own layout manager.
I don't see where you add the menu bar to the frame using the setJMenBar(...) method.
Read the section from the Swing tutorial on How to Use Menus for more information and working examples. The examples will also show you how to better structure your code.
Also, don't keep extending panels just to add a single component to the panel. You can add multiple components to any panel. And make sure your panels use a layout manager.
When I run the program, I see a blank window.
How am I able to solve this? Thanks you. What I did wrong?
Here is my code:
public class Environment{
private JFrame frame;
private JMenu jmenu;
private JMenuItem menuItem;
private JMenuBar menuBar;
Environment(){
frame = new JFrame("Notepad");
menuBar = new JMenuBar();
//menuBar.setVisible(true);
jmenu = new JMenu("Test");
menuItem = new JMenuItem("Open");
jmenu.add(menuItem);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(660, 350);
// Set a main menu
frame.setJMenuBar(menuBar);
menuBar.add(jmenu);
frame.setVisible(true);
}
}
Adding label1 and button1 - You can fix layout etc. Suggest you to use Jpanel as well.
public class Enviornment {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame;
JMenu jmenu;
JMenuItem menuItem;
JMenuBar menuBar;
frame = new JFrame("Notepad");
menuBar = new JMenuBar();
menuBar.setVisible(true);
jmenu = new JMenu("Test");
menuItem = new JMenuItem("Open");
jmenu.add(menuItem);
JLabel label1 = new JLabel("My Name");
JButton button1 = new JButton("Button");
frame.add(label1);
frame.add(button1);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(660, 350);
// Set a main menu
frame.setJMenuBar(menuBar);
menuBar.add(jmenu);
frame.setVisible(true);
}
}
Can someone please tell me how I can add my menubar to the borderlayout NORTH.
I have written it this way.
private void makeFrame()
{
frame = new JFrame("Game");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(menubar, BorderLayout.NORTH);
contentPane.add(new JButton("south"), BorderLayout.SOUTH);
contentPane.add(new JButton("center"), BorderLayout.CENTER);
contentPane.add(new JButton("east"), BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
makeMenuBar();
}
public void makeMenuBar(){
JMenuBar menubar = new JMenuBar();
JMenu menu;
JMenuItem item;
JMenu file = new JMenu("File");
menubar.add(file);
item = new JMenuItem("New Game...");
file.add(item);
item = new JMenuItem("Save As...");
file.add(item);
item = new JMenuItem("Quit");
file.add(item);
}
Can anyone tell me how to add this menubar to border north please.
Generally speaking, you shouldn't, instead, you should use JFrame#setJMenuBar instead
The problem you are having seems to be the fact that you are either adding the menu bar to the container BEFORE it's initialized or are creating a new instance of JMenuBar which is never added to the screen
private void makeFrame()
{
frame = new JFrame("Game");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
// ?? Don't know what this initialised to...
contentPane.add(menubar, BorderLayout.NORTH);
//...
makeMenuBar();
}
public void makeMenuBar(){
// A local variable with the same name...??
JMenuBar menubar = new JMenuBar();
Oh, and the instance you are creating in makeMenuBar is only local to the method...so it will never have an effect on the menubar variable you are using in makeFrame...
anyone can tell why the class OpenMenuListener doesnt send a feedback when i click the open button in my Gui ? The erase button works though. It sends me a feedback. I'm exausted.
import java.awt.*;
import javax.swing.*;
public class DrawingApplication extends JFrame {
JComponent drawingArea;
class EraseButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked erase");
}
}
class OpenMenuListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked open");
}
}
public DrawingApplication() {
JPanel frame = new JPanel();
add(frame);
// panel1.add( new JButton(Figuur),BorderLayout.CENTER);
drawingArea = new JLabel();
// label1.add(drawingArea);
frame.add(drawingArea);
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
// Add the menubar to the frame
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenu open = new JMenu("Open");
fileMenu.add(open);
fileMenu.addSeparator();
JMenu save = new JMenu("Save");
fileMenu.add(save);
fileMenu.addSeparator();
JMenu close =new JMenu("Close");
fileMenu.add(close);
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
helpMenu.add(new JMenu("Info"));
JPanel panel2 = new JPanel();
add(BorderLayout.SOUTH, frame);
frame.add(new JLabel("figuurkeuze"));
frame.add(panel2);
setVisible(true);
JRadioButton rectButton = new JRadioButton("Rectangle");
JRadioButton triangleButton = new JRadioButton("Triangle");
JRadioButton circleButton = new JRadioButton("Circle");
frame.add(rectButton);
frame.add(triangleButton);
frame.add(circleButton);
JButton erase = new JButton("Erase");
frame.add(erase);
EraseButtonListener eraselistener = new EraseButtonListener();
erase.addActionListener(eraselistener);
OpenMenuListener openMenuListener = new OpenMenuListener();
open.addActionListener(openMenuListener);
}
public static void main(String[] args) {
DrawingApplication frame = new DrawingApplication();
frame.setTitle("My prgram");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
It seems a problem of name aliasing, you add the listener to the open variable which is declared as a JMenuItem, so you are adding the ActionListener to the menu item instead that to a button (that you never declare since there is no JButton open = new JButton("open") anywhere).
I have a panel on my frame .and by clicking on a button I want to delete the old panel and make the other panel and add that panel to my frame.(also I use netbeans)
would you please help me that how can i do that?thanks
JFrame frame = new JFrame();
final JPanel origPanel = new JPanel();
frame.add(origPanel, BorderLayout.CENTER);
MouseListener ml = new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
// Mouse clicked on panel so remove existing panel and add a new one.
frame.remove(origPanel);
frame.add(createNewPanel(), BorderLayout.CENTER);
// Revalidate frame to cause it to layout the new panel correctly.
frame.revalidate();
// Stop listening to origPanel (prevent dangling reference).
origPanel.removeMouseListener(this);
}
}
origPanel.addMouseListener(ml);
This way:
final JFrame frame = new JFrame();
frame.setSize(200, 200);
final JPanel panelA = new JPanel();
final JPanel panelB = new JPanel();
JButton button = new JButton("Switch");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.remove(panelA);
frame.add(panelB);
frame.show();
}
});
JLabel label = new JLabel("This is panel B. Panel A is gone!");
panelB.add(label);
panelA.add(button);
frame.add(panelB);
frame.add(panelA);
frame.show();