Menu Bar GUI Application - java

My menuBar isn't showing. Do I need the JPanel for it to show in my GUI?
private void buildCtrlPanel() {
ctrlPanel = new JPanel();
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
optionsMenu = new JMenu("Options");
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
ctrlPanel.setLayout(new FlowLayout());
ctrlPanel.add(menuBar);
ctrlPanel.add(frame);
menuBar.add(fileMenu);
menuBar.add(optionsMenu);
}

You can only add a component to one container. You've added the JMenuBar appropriately to the JFrame -- fine, but then you also add it incorrectly to a JPanel (why?) one that uses a FlowLayout, layouts that don't work well with JMenuBars (again why?). Solution: don't do that. Add it to the JFrame as you're already doing, and leave it be.
You also seem to be adding a JFrame to a JPanel -- something that you shouldn't be doing, and again which suggests that you will want to go through the Swing tutorials before proceding further.
You can find links to the Swing tutorials and to other Swing resources here: Swing Info
The Swing menu tutorial can be found here: How to use Menus

Related

Adding Webcam panel to existing jPanel

I'm using NetBeans and I made basic layout with jPanel in the middle of it. Now I would like to add Webcam panel in this jPanel.
Here's example how to get frames from webcam and display it in new window. It's working fine.
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setDisplayDebugInfo(true);
panel.setImageSizeDisplayed(true);
panel.setMirrored(true);
JFrame window = new JFrame("Test webcam panel");
window.add(panel);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
But when I tried to display it in my existing jPanel it's not working. Webcam screen is not visible. Here's my code:
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
WebcamPanel panel = new WebcamPanel(webcam);
panel.setFPSDisplayed(true);
panel.setDisplayDebugInfo(true);
panel.setImageSizeDisplayed(true);
panel.setMirrored(true);
jPanel5.add(panel);
jPanel5.setVisible(true);
I call all of this in my main class constructor. Just after other netbeans componenst are loaded. When I add example it's working good but then my main layout is loaded and webcam screen in another window. I would like to get it in the same window.
I have seen other topics about adding image to jPanel but it's not working with capturing movie from webcam.
Thanks for help.
But when I tried to display it in my existing jPanel it's not working.
When you add components to a visible GUI the basic code is:
panel.add(...);
panel.revalidate(); // invoke the layout manager
panel.repaint(); // paint components
All component have a default size of (0, 0) when created so there is nothing to paint. You need to invoke the layout manager so the component is given a size/location.
I changed a little my application and now in the middle of main windows is JTabbedPane and I found a solution to my problem. Instead of making Tabs using NetBeans window designer I made it with code.
I made empty JTabbedPane in Netbeans and then add this to code:
final JPanel jPanelCamera = new JPanel();
jTabbedPane1.addTab("Camera", jPanelCamera);
Webcam webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
WebcamPanel webcamPanel = new WebcamPanel(webcam);
webcamPanel.setFPSDisplayed(true);
webcamPanel.setDisplayDebugInfo(true);
webcamPanel.setImageSizeDisplayed(true);
webcamPanel.setMirrored(true);
jPanelCamera.add(webcamPanel);
jPanelCamera.getParent().revalidate();
System.out.println("Camera OK");
I have no idea why earlier when I made component using NetBeans designer it was't working but now it's working good. I think if someone would add this not to JTabbedPane, but to JPanel should also make this panel with code. Not with Netbeans designer and then it should work.

JMenuBar Cannot Find Symbol

I am just started learning Java and I've been reading through this documentation. I don't like to copy a bunch of code and paste it. So I have been trying to work my way through the documentation one thing at at time.
I already have a working JFrame and decided I would start by adding a menu.
HERE IS MY CODE:
package mainframe;
import javax.swing.*;
public class menuBar extends JMenuBar {
JMenuBar mainMenu = JMenuBar("Menu");
}
MY ERROR:
error: cannot find symbol
JMenuBar mainMenu = JMenuBar("Menu");
symbol: method JMenuBar(String)
location: class menuBar
1 error
So anyways. I am not really sure what the "cannot find symbol error" means. Maybe I am searching wrong. But every time I Google it it takes me to more complex questions with no clear answer. Any advice as to what I am doing wrong and or to what the cannot find symbol error means would be very much appreciated. Thanks in advance.
In response to your particular code here, I suggest that you do not extend the JMenuBar class. You may have seen it in many tutorials or examples where the JFrame class is extended, although that is considered bad practice. To add a JMenuBar to your window, I would suggest doing the following:
public class MyProgram {
JFrame frame;
public MyProgram() {
...
frame = new JFrame();
JMenuBar mainMenu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new JMenuItem("Open..."));
mainMenu.add(fileMenu); // adds a single JMenu to the menubar
frame.setJMenuBar(mainMenu); // adds the entire menubar to the window
...
frame.setVisible();
...
}
The only reason you would extend the JMenuBar class would be if you wanted to make a class that had additional functionality in terms of methods defined in your subclass, but that seems unlikely especially given the fact that you're just learning Swing.
The constructor for JMenuBar never takes any arguments. Also remember to use the new keyword when you instantiate (create an instance of) a new object. Consider using the following code:
JMenuBar mainMenu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
mainMenu.add(fileMenu);
JMenuBar mainMenu = JMenuBar("Menu");
should be
JMenuBar mainMenu = new JMenuBar("Menu");
You forgot the new keyword. You must always use new when creating a new object with a constructor. Otherwise, Java will think that it is a method, which it is not.
Furthermore, if you look at the documentation here. you will find that JMenuBar's constructor does not take any arguments. Therefore, don't pass anything:
JMenuBar mainMenu = new JMenuBar();

java clickable-through non transparent Component in a transparent clickable-through jframe

OS = Ubuntu; JDK = 1.7.0_10
General:
What I want is something commonly available as desktop widget (e.g. annotate for compiz or gromit), is there a way to do it in Java?
I want to create one jframe or window to be full-screen, transparent, clickable-though and always on top.
Inside the jframe I want to add/remove components (text, images, rectangles..) to be positioned on a specific point of the screen. Those component should be non-trasparent and always on top. If possible they should allow to click through.
Example:
I have several app opened on my desktop.
I want to automatically draw a box around window which is on focus drawing a rectangle around the window, the area of the rectangle should be transparent and clickable-thsough in order to allow me to use my application.
Problems:
I can create a transparent frame which stays on top but is not clickable-through, but The component what I will ad to the frame will be in any case transparent.
JFrame frame = new JFrame("MyFrame");
frame.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setLocation(0,0);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(true);
//frame.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false); //apple
frame.getContentPane().setLayout(new BorderLayout());
System.setProperty("sun.java2d.noddraw", "true");
AWTUtilities.setWindowOpaque(frame, false);
AWTUtilities.setWindowOpacity(frame, 0.0f);
JLabel label = new JLabel("Hello NOT transparent label");
label.setOpaque(true);
label.setBackground(new Color(255, 0, 0));
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
I can make the frame transparent and clickable-through by setting the IconImage with a transparent image loaded from URL.
This works if the image is loaded from an URL but not if I load the image from a local file (why?!?).
Also in this case, when I add a component to the frame is not displayed.
try {
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(new URL("http://i.imgur.com/xtZK0.png")));
} catch (MalformedURLException e) {e.printStackTrace();}
I tried to add a JFXPanel but no success, I tried adding the component to the GlassPane but no success..
After one week of intensive tests and search I hope someone can give me a solution or the right hint.
Thanks.

How to merge JOptionPane and Frame into one

Currently I have a very basic file viewer working as follows :
- in JOptionPane I browse for files, and set some variables to display (colors, line connecting etc)
- previous windows loads a frame with drawn points
alt text http://img190.imageshack.us/img190/4443/104bu.jpg
Code :
http://paste.pocoo.org/show/220066/
Now I'd like to throw it into one window, with JMenu for selecting files and changing display parameters. How to get started ? Should I rewrite everything to JDialog ?
alt text http://img684.imageshack.us/img684/5264/lab10db.jpg
If you want the JOPtionPane as a child of the main JFrame, then add it as a child. Of course it will then cover your dots. Hence you will have to not draw your dots directly in the content pane of the main JFrame, but rather in a new JPanel that you have also added to the JFRame's content pane. Let me know if I've understood the question whatsoever.
Here's some code for how I see the setup (I'm leaving the layout problem out of this, partly because it depends on what you want to see):
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(400,400));
frame.getContentPane().add(new JOptionPane());
JPanel canvasForDots = new JPanel();
frame.getContentPane().add(canvasForDots);
You might also like to look at How to Use Tool Bars and How to Use Menus. ImageApp is a typical implementation that associates menu items with the corresponding Action instances.
private class ClearAction extends AbstractAction {…}
private class ImageOpenAction extends AbstractAction {}
private Action openAction = new ImageOpenAction("Open");
private Action clearAction = new ClearAction("Clear");
…
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(openAction));
menu.add(new JMenuItem(clearAction));
This related example adds the file chooser directly to the main frame. Here's a more elaborate example of connecting lines and shapes using the same principles.

setting Swing JInternalFrame lnf manually

UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(new Color(207,255,247)));
UIManager.put("InternalFrame.inactiveTitleBackground", new ColorUIResource(new Color(207,255,247)));
JDesktopPane baTabbedPane = new JDesktopPane();
JInternalFrame iframe = new JInternalFrame("Cheapest To Deliver",true,true,true,true);
iframe.setSize(400,150);
baTabbedPane.add(iframe);
why is my Internal Frame's title background not set on startup?
I've tried setting it on the overall JFrame init but made no difference (By contrast I could change other JFrame ui component look n feel such as MenuItem.background in this location so I thought it might have been because the JInternalFrame was not a top-level component i.e. under a tabbed pane, that maybe it needed changing at some other point, but where?)
Any tips on the correct place to call UIManager.put() for JInternalFrame?
got it eventually - the call to put() works fine after JInternalFrame creation but I did make it before I added the component to a container. I then still had to set it's UI:
JInternalFrame iframe = new JInternalFrame("blah",true,true,true,true);
UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(new Color(248,250,175)));
UIManager.put("InternalFrame.inactiveTitleBackground", new ColorUIResource(new Color(248,250,175)));
javax.swing.plaf.basic.BasicInternalFrameUI ui =
new javax.swing.plaf.basic.BasicInternalFrameUI(iframe);
iframe.setUI(ui);
I think you need to make all calls to UIManager.put before you create any Swing components.

Categories

Resources