My JMenu Bar is not showing up - java

My JMenuBar is not showing up when i run my App.
How can I fix this??
So when I run my JFrame I need to see my JMenuBar on top.
my Layout is Null
Code:
package view;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.WindowConstants;
public class Home extends JFrame {
private Container window = getContentPane();
public Home(){
initGUI();
}
public void initGUI(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
setLayout(null);
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
menu.add(file);
window.add(menu);
pack();
}
}

you not added JMenuBar to the JFrame
use setJMenuBar(menu);

Instead of add(menu) call
setJMenuBar(menu);
Also you better use SwingUtilities.invokeLater()

Related

Is there any way to make a translucent JButton? [duplicate]

I have simple GUI code as follows, in which I want to make the JButton one translucent, so that the image behind the JButton is visible!
package dealORnodeal;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2");
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{
super("Deal Or No Deal");
setSize(800,850);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
contentPane.add(bg);
JMenuItem item1;
item1 = new JMenuItem("Start Game");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Rules");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Credits");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Menu");
item1.addActionListener(this);
menu1.add(item1);
bar1.add(menu1);
setJMenuBar(bar1);
//GAME CODE
one.setBounds(25,151,190,49);
one.addActionListener(this);
add(one);
//GAME CODE END
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{}
}
Now how would the code be if I wanted to set the button to be translucent so that the background image would be visible through the button. BTW please don't confuse Translucent with transparent!
I can't comment to your question, so I'll answer you here..
if you use this code:
myButton.setOpaque(false);
It would not paint the button - because now it's a trasnparent.
to create the button translucent I think you should override the button paint method..
take a look at this thread
setOpaque doesn't work for JButtons, the right property is:
button.setContentAreaFilled(false);

Adding ScrollPane to BoxLayout

IN MY PROGRAM,
I have a button when pressed, adds a new JPanel to a JPanel with BOXLAYOUT. Since when I add the JPanel it adds it to the bottom of the previous one. BUT THERE IS NO SCROLL BAR.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class tes{
public static void main(String[] args) {
JFrame newL = new JFrame();
newL.setTitle("New Level Files");
//newL.setLayout(new BoxLayout());
//t.setSize(500,600);
//newL.pack();
newL.setVisible(true);
newL.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(listPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(600, 100));
newL.add(scrollPane, BorderLayout.CENTER);
JPanel levelP = new JPanel();
levelP.setBorder(BorderFactory.createLineBorder(Color.black));
levelP.setLayout(new GridBagLayout());
GridBagConstraints l = new GridBagConstraints();
l.insets = new Insets(10,10,10,5);
JButton okForFileEdit = new JButton("Edit this File");
l.gridx = 1;
l.gridy = 6;
levelP.add(okForFileEdit, l);
okForFileEdit.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent n){
JPanel createInPanel = new JPanel();
createInPanel.setSize(200,200);
createInPanel.setBorder(BorderFactory.createLineBorder(Color.black));
createInPanel.setLayout(new GridBagLayout());
listPane.add(createInPanel);
JLabel yout = new JLabel("YEah this is a long sentence to see the placement");
createInPanel.add(yout);
listPane.revalidate();
listPane.repaint();
newL.revalidate();
}});
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
newL.pack();
}
** I added a SIMPLIFIED VERSION OF THE PROGRAM THAT DOESN'T MAKE THE SCROLLBAR
Hope this makes more sense. Thanks for the help in advance :);
I have a button when pressed, adds a new JPanel to a JPanel with BOXLAYOUT.
When you dynamically add components to a panel the basic logic need in your ActionListener is:
panel.add(...);
panel.revalidate();
panel.repaint();
You need to invoke the revalidate method to invoke the layout manager of the panel so the preferred size can be recalculated.
Edit:
newL.add(listPane);
You don't need the above statement. A Swing component can only have a single parent. That statement removed the panel from the scroll pane so you don't see the scroll bars.
newL.revalidate();
You don't need that statement. As I said in my answer you only need to revalidate() the panel that you changed.
Also you don't need all the statements below:
listPane.add(levelP);
listPane.revalidate();
listPane.repaint();
newL.add(listPane);
That is the layout manager is invoked when you pack the frame of make the frame visible. Your code should be:
newl.pack();
newl.setVisible(true);
That is you should only make the frame visible AFTER adding all the components to the frame.

Opacity/Translucency of a JButton?

I have simple GUI code as follows, in which I want to make the JButton one translucent, so that the image behind the JButton is visible!
package dealORnodeal;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2");
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{
super("Deal Or No Deal");
setSize(800,850);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
contentPane.add(bg);
JMenuItem item1;
item1 = new JMenuItem("Start Game");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Rules");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Credits");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Menu");
item1.addActionListener(this);
menu1.add(item1);
bar1.add(menu1);
setJMenuBar(bar1);
//GAME CODE
one.setBounds(25,151,190,49);
one.addActionListener(this);
add(one);
//GAME CODE END
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{}
}
Now how would the code be if I wanted to set the button to be translucent so that the background image would be visible through the button. BTW please don't confuse Translucent with transparent!
I can't comment to your question, so I'll answer you here..
if you use this code:
myButton.setOpaque(false);
It would not paint the button - because now it's a trasnparent.
to create the button translucent I think you should override the button paint method..
take a look at this thread
setOpaque doesn't work for JButtons, the right property is:
button.setContentAreaFilled(false);

Java JDialog messes up JMenuBar on mac

I am having some problems regarding to the JMenuBar and I cant seem to figure it out.
I will start with an abriviation of the problem: The program consists of a JFrame, a JDialog and a JMenuBar. Initially, you will get to see a JFrame with the JMenuBar in the top. But at some point, the JDialog will pop up where a user can fill in some text fields. The problem that I'm having is that as soon as the focus goes to the JDialog, the JMenuBar disappears. What I want is that the JMenuBar stays in the top of the screen all the time, except if the whole program is NOT in focus. Here are 2 screenshots, in the first screen shot, the JFrame is selected and in the other one the JDialog is selected.
So what i actually want is instead of only seeing the JMenuBar when the focus is on the JFrame, i want to see the JMenuBar all the time. Since a JDialogs can not have the JMenuBar in the top, like a JFrame has, i decided not to have multiple JMenuBars, but just the one that should be visible all the time.
At last i will give a part of the code that is as small as possible (and still working) and also contains the problem:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
/**
* #author Guus Leijsten
* #created Oct 27, 2012
*/
public class MenuBarProblem extends JFrame {
public MenuBarProblem() {
super("Frame");
this.setMinimumSize(new Dimension(270, 200));
this.setPreferredSize(new Dimension(800, 530));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JRootPane root = this.getRootPane();
//Menu
JMenu fileMenu = new JMenu("File");
JMenuItem file_exit = new JMenuItem("Exit");
file_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
file_exit.setToolTipText("Exit application");
file_exit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(file_exit);
JMenuBar menu = new JMenuBar();
menu.add(fileMenu);
root.setJMenuBar(menu);
this.setVisible(true);
JDialog d = new JDialog(this, "Dialog");
d.setSize(200, 100);
d.setLocation(0, (int)root.getContentPane().getLocationOnScreen().getY());
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.setVisible(true);
}
public static void main(String[] args) {
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("mac") >= 0) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
new MenuBarProblem();
}
}
If I can be honoust, i think that the problem lies in the part of JRootPane. But we'll see ;)
Did anyone else encountered this problem and managed to solve it alrady, or is there anybody that wants to give it a shot?
Thanks in advance!
Added content:
In the following example I will show a version that gives some functionality to the play.
This is the program i'm making:
The second image shows the state in which the right menu is undocked.
Obviously the JMenuBar should still be visible and operational because without it, a lot of functionalities of the program will be disabled.
At this point i'm starting to think that it is impossible for the JMenuBar to stay visible when the dialog (undocked menu) is undocked, and focussed on.
I know that the JMenuBar on a JDialog can not be in the mac osx style (top of screen), so are there any other techniques i can use for undocking, which does give me a mac osx style JMenuBar?
One key to solving this problem, pun intended, is to let a key binding share a common menu action, as shown below. Note how a menu item, your dialog's content and an (otherwise superfluous) button can all use the same Action instance. A few additional notes:
Kudos for using getMenuShortcutKeyMask().
Swing GUI objects should be constructed and manipulated only on the event dispatch thread (EDT).
System properties should be set before starting the EDT.
Make the dialog's setLocation() relative to the frame after its geometry is known.
A common Mac idiom uses the following predicate:
if (System.getProperty("os.name").startsWith("Mac OS X") {…}
See also this example.
For local use in the dialog itself, also consider JToolBar.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/**
* #see https://stackoverflow.com/a/13100894/230513
*/
public class MenuBarProblem extends JFrame {
private static final int MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
private static final String exitName = "Exit";
private static final KeyStroke exitKey =
KeyStroke.getKeyStroke(KeyEvent.VK_W, MASK);
private final ExitAction exitAction = new ExitAction(exitName);
public MenuBarProblem() {
super("Frame");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenu fileMenu = new JMenu("File");
JMenuItem fileExit = new JMenuItem(exitAction);
fileMenu.add(fileExit);
JMenuBar menu = new JMenuBar();
menu.add(fileMenu);
JDialog d = new JDialog(this, "Dialog");
JPanel p = new JPanel();
p.getInputMap().put(exitKey, exitName);
p.getActionMap().put(exitName, exitAction);
p.add(new JButton(exitAction));
d.add(p);
d.pack();
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setJMenuBar(menu);
this.pack();
this.setSize(new Dimension(320, 240));
this.setLocationByPlatform(true);
this.setVisible(true);
d.setLocation(this.getRootPane().getContentPane().getLocationOnScreen());
d.setVisible(true);
}
private static class ExitAction extends AbstractAction {
public ExitAction(String name) {
super(name);
this.putValue(Action.MNEMONIC_KEY, exitKey.getKeyCode());
this.putValue(Action.ACCELERATOR_KEY, exitKey);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MenuBarProblem();
}
});
}
}
Solved!
Using a JFrame with the use of setAlwaysOnTop(true) gives me the desired effect of having a JMenuBar when the focus changes.

How to refresh a panel?

I have a JMenu and when a person clicks on a JMenuItem I want the middle panel to refresh and display some new stuff. So I tried the .removeAll which works fine but when I try to add something it wont show.
Note: I'm using the WindowBuilder PRO, so I still trying to get use to it and whatnot
Here is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import java.awt.List;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JSeparator;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Panel;
import java.awt.GridBagConstraints;
public class HomeScreen {
JFrame frame;
private final Panel panel = new Panel();
public HomeScreen(String name) {
initialize(name);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(String name) {
frame = new JFrame("Timzys CMS / Monitor / Account( " + name + " )");
frame.setResizable(false);
frame.setSize(694,525);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnMonitor = new JMenu("Monitor");
menuBar.add(mnMonitor);
JMenuItem mntmUsers = new JMenuItem("Users");
mnMonitor.add(mntmUsers);
JMenuItem mntmContentPosts = new JMenuItem("Content Posts");
mnMonitor.add(mntmContentPosts);
JMenuItem mntmLogs = new JMenuItem("Logs");
mnMonitor.add(mntmLogs);
JMenu mnExtras = new JMenu("Extras");
menuBar.add(mnExtras);
mntmUsers.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.getContentPane().removeAll();
frame.getContentPane().add(new JLabel("Test"));
frame.getContentPane().revalidate();
}
});
JMenuItem mntmFeedbacksuggestions = new JMenuItem("Feedback/Suggestions");
mnExtras.add(mntmFeedbacksuggestions);
frame.getContentPane().setLayout(null);
panel.setBounds(0, 0, 688, 476);
frame.getContentPane().add(panel);
panel.setLayout(null);
JTextPane txtpnWelcomeTimzysCms = new JTextPane();
txtpnWelcomeTimzysCms.setFont(new Font("Arial", Font.PLAIN, 18));
txtpnWelcomeTimzysCms.setEditable(false);
txtpnWelcomeTimzysCms.setText("Welcome Timzys CMS Monitor. If you are here then you are a admin! So please do not tamper with any important things. If you have questions or suggestions goto the extras tab and submit a feedback idea. Enjoy!");
txtpnWelcomeTimzysCms.setBounds(10, 11, 668, 72);
panel.add(txtpnWelcomeTimzysCms);
}
}
Probably you're facing similar problem as someone else in the question:
Java Swing revalidate() vs repaint()
As suggested: try to call repaint instead of revalidate (the one you're calling right now in your implementation of method actionPerformed)
The problem is that because you have set the frame's layout to null, the new JLabel that you add will need to have its size set. e.g.:
JLabel label = new JLabel("Test");
label.setSize(100, 100);
frame.getContentPane().add(label);
As an aside:
Actually I wonder why you are mixing AWT & Swing components here. You have a heavyweight AWT panel added to the frame which blocks out the menus. Switching to JPanel would fix this.

Categories

Resources