I've been running into a problem lately where I try to se a JMenuItems icon which when I define and point to the icon the application it self don't start/show.
I started looking for errors, but there where none; started looking for write occurencies, which typically pretty much does appear when I add the icon and after that as pointed above the application doesn't start/show.
When the icon is set but commented:
Code
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
// mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
Screen Shot Of Visible Application
After the icon is set and trying to execute application:
Code:
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
The window not created and application terminated
Note that when the window is not created in this picture the application is therefor terminated.
Please try to answer nice, and if you need the whole source file it is possible.
Edit:
Also if needed i can make a video where i show when i start the application when the icon is set but not commented.
getResource uses the relative path with respect to the package (folder), like in
Vision.class.getResource("fullscreen16x.png")
or absolute like in:
Vision.class.getResource("/xray/fullscreen16x.png")
Related
So, I am creating a GUI interface for the project via Netbeans (using it is a requirement). Everything is fine until I add an Icon for a label. The generated code is below:
jLabel25 = new javax.swing.JLabel();
jLabel25.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/mycompany/sostarnyba/images/map.png")));
jLabel25.setText("");
I add the icon exactly as the person in this question shows it: Netbeans ImageIcon not displaying
Except my app does not run at all.
When the icon is removed, everything runs, but when it is added it does not. The error is: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null.
What am I doing wrong?
The problem was solved by putting not a full path within the project but the absolute path in your computer starting from /Users
So I've been playing around with JButtons and I've been trying to add an ImageIcon to a JButton. I have the following code:
window = new JFrame("Test");
window.setSize(1000, 600);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
Icon icon = new ImageIcon("/Apple.jpg");
JButton apple = new JButton(icon);
apple.setBounds(50, 50, 200, 200);
window.add(apple);
I was wondering where would the Apple.jpg file have to be located for the code to work? Currently, Apple.jpg is located in the same package as this class.
Looking at the source code for constructor ImageIcon(String), leads me to the conclusion that the string you pass is treated as is.
Hence, according to the code you posted in your question, i.e.
Icon icon = new ImageIcon("/Apple.jpg");
Java will search for file Apple.jpg in the root directory.
If you are running on a Windows machine, like I am, Java considers my root directory to be C:\ (on my machine) so it will search for this file: C:\Apple.jpg
The answer linked to in this comment to your question (from Gilbert le Blanc) details all the different ways to load an image. I just tried to answer your question. So using the code in your question, the answer to it is that you would have to place your file (Apple.jpg) in the root directory. I'm assuming that you know where that is on your computer. In any case, I couldn't find enough information in your question to help you with that. for example, I couldn't tell what platform you are on.
I'm trying to add proper support for OS X in my Java 8 Swing application. To do this I'm using the com.apple:AppleJavaExtensions:1.4 package from maven.
I initialise the menu bar using the following code:
Application application = Application.getApplication();
JMenu fileMenu = new JMenu("File");
JMenuItem openMenuItem = new JMenuItem("Open...");
openMenuItem.addActionListener(action -> openFileDialog());
openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.META_MASK));
fileMenu.add(openMenuItem);
JMenuBar mainMenuBar = new JMenuBar();
mainMenuBar.add(fileMenu);
application.setDefaultMenuBar(mainMenuBar);
This works just fine the first time, without any Swing windows opened. However, when one file is opened (and a Swing window is created), the shortcut stops working. Pressing CMD+O still highlights the menu item, but the action isn't executed. Using the mouse to select the 'Open...' menu item does work. I'm guessing the Swing window is somehow stealing the event (but the menu is still highlighted when the key combo is pressed.. weird).
When the file open dialog (that openFileDialog() shows) is cancelled, the menu accelerator will keep working. So it is definitely related to the Swing window being opened when a file is opened.
Another problem with opening files with OS X is that the following code never shows 'foo':
application.setOpenFileHandler(e -> System.out.println("foo"));
This code is executed in main() (which returns almost instantly, after creating the menu bar I mentioned earlier). The file handler isn't ever called; not when dropping a file onto the dock icon, nor when double clicking a file and opening it with my application.
What am I doing wrong?
If your app doesn't have many Frames then this could be a workaround.
setJMenuBar(menuBar);
final Dimension invisible = new Dimension(0, 0);
menuBar.setSize(invisible);
menuBar.setMaximumSize(invisible);
menuBar.setPreferredSize(invisible);
menuBar.setMinimumSize(invisible);
Apparently the OpenFileHandler does nothing unless your Java application is running from an application bundle with an Info.plist that specifies appropriate CFBundleDocumentTypes. Here's an example of what it should look like:
http://www.qtcentre.org/wiki/index.php?title=Opening_documents_in_the_Mac_OS_X_Finder
I worked around the menu accelerator not working by simply registering a KeyEventDispatcher to the KeyboardFocusManager on each of the Swing windows, and manually invoking the appropriate ActionListeners when the correct KeyEvent is caught. Very very ugly, but it works. I'm still wondering why this is necessary.
I'm working in eclipse and I have made an application without name and icon. When i start the application it's a really creepy name displaying in the upper left corner (Mac). It's some thing like. I wonder how I can change this to my own name. Second question is how i can change the icon. Can I do that in eclipse?
If you're using JFrames, you can try setting the icon image as follows.
frame.setIconImage(img);
Also, by name it sounds a bit like you mean the frame's title. When you create the a frame, you can set the title as follows:
Frame frame = new JFrame("Title goes here");
To change name: How to change an Android app's name?
To change icon: How to change the icon of an Android app in Eclipse?
It's already here at stackoverflow, please check if your answers are here before posting.
The prefered method would be to create a Mac OS application bundle (and the bundler), but if this seems like to much work, you can supply custom properties to, for example, you could supply the Xdock:name property when running the application, for example...
-DXdock:name="Application Name"
If you can't do that, you can set it when your application runs, for example...
public static void main(String[] args) {
try {
// Sets the application name on the menu bar
System.setProperty("Xdock:name", "Application Name");
// Set the applications dock icon...
Application application = Application.getApplication();
application.setDockIconImage(ImageIO.read(TestDockIcon.class.getResource("/Icon.png")));
// Start the application...
new TestDockIcon();
} catch (IOException exp) {
exp.printStackTrace();
}
}
I have a Liferay project with Vaadin portlet.
I want to add icon to my buttons.
Button search = new Button("Search");
search.setIcon(new ThemeResource("img/silk/add.png"));
But dont know where i gonna put it? Now i put it in docroot directory.
UPDATE
No i try this.
Button search = new Button("Search");
search.setIcon(new ThemeResource("LifeRayVaadin-portlet/icons/add.png"));
But when i redeploy project in console get
09:34:05,773 WARN [404_jsp:109] /html/VAADIN/themes/liferay/LifeRayVaadin-portlet/icons/add.png
So your portlet is looking for the icons in /html/VAADIN/themes/liferay/LifeRayVaadin-portlet/icons/add.png.
You could create a directory icons under VAADIN and call:
search.setIcon(new ThemeResource("../../icons/add.png"));
ThemeResource without any path will look for the file in VAADIN/themes/yourtheme/ path and thus ../../ will get you (in this case) to /VAADIN/. I personally would never hardcode the name of a theme or a portlet in a project, because when it changes you have to go through every reference and change them.
You can put your images in $PORTLET-NAME/docroot/icons directory and call them using the Path
/$PORTLET-NAME$/icons/add.png
in your case it will be
Button search = new Button("Search");
search.setIcon(new ThemeResource("/$PORLTET-NAME$/icons/add.png"));