I have a Class MainWindow which extends JFrame.
In MainWindow i have a JMenuBar.
I want to show the MenuBar in OSX on top (next to the Apple Symbol). This only works, when i dont set a Substance Skin. Is it possible to use a Substance Skin and use The MacOS MenuBar?
My Code:
//Set Menu for MacOS
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", name);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
SubstanceSkin skin = new GraphiteGlassSkin();
SubstanceLookAndFeel.setSkin(skin); //WORKS WHEN I COMMENT THIS (WITHOUT SUBSTANCE SKIN)
JFrame.setDefaultLookAndFeelDecorated(false);
MainWindow mainWindow = new MainWindow(name);
mainWindow.setVisible(true);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You can specify the UI for menu bar alone like this:
try {
UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
} catch (UnsupportedLookAndFeelException ex) {
// log...
}
JMenuBar menubar = frame.getJMenuBar(); // assuming you've set the menu bar already
String os = System.getProperty("os.name");
if (os.equals("Mac OS X")) {
try {
System.setProperty("apple.laf.useScreenMenuBar", "true");
menubar.setUI((MenuBarUI) Class.forName("com.apple.laf.AquaMenuBarUI").newInstance());
} catch (Exception ex) {
// log...
}
}
Yes, as shown below.
$ java -Xdock:name=MyApp -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel -jar MyApp.jar
Related
bg music plays on splash,menu and gamescene when i had put the below code, but i want to make bg music to play only on my gamescene, can you please help me. Thank you.
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
{
MusicFactory.setAssetBasePath("mfx/");
try {
music = MusicFactory.createMusicFromAsset(mEngine
.getMusicManager(), this, "abcd.wav");
music.setLooping(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
music.play();
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}
I usually use music.pause() and music.resume() (because after stop the play does not work any more, I do not know why). When you leave a scene, you can call music.pause(). When you enter a scene, you can call music.resume();
I'm using Swing in order to create a little java 2D game. I only try to display an image. Since i'm not on the EDT I'm using SwingUtilities.invokeLater() to do the stuff. When I use it, image is not displayed (in fact it's displayed during few miliseconds and disapear).
When I don't use SwingUtilities.invokeLater() the image is correctly displayed but I need to use invokeLater().
Here's my basic code :
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
#Override
public void run() {
final BreizhFrame frame = new BreizhFrame();
File imgFile = new File("src/test/lvl1-800x450.jpg");
System.out.println(String.valueOf(imgFile.exists()));
System.out.println(SwingUtilities.isEventDispatchThread());
Image i;
try {
i = ImageIO.read(imgFile);
frame.getGraphics().drawImage(i, 0, 0, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (InvocationTargetException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any idea ?
Thanks.
That's not how you do it in Java. Yes, you are drawing the image, but after a while the control gets invalidated and it's re-drawn without your image.
This tutorial should help you
This is my actionListener for a popup menu button i want to add this picture on the panel after every its clicked
mntmNewMenuItem.addActionListener(new ActionListener() {
//This method will be called whenever you click the button.
int i;
public void actionPerformed(ActionEvent e) {
try {
label.setIcon(new ImageIcon
(new URL("file:/C:/Users/Ashad/JunoWorkspace/FYP1/table.png")));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
panel.add(label);
//redraw panel after addition
panel.validate();
panel.repaint();
handleDrag(label);
}
});
you can only add a UI object once.
if you want just a single instance to be added, you need to remove that element 1st then add. (but it doesn't seem logical removing then adding).
instead you can create a Label() object inside actionperformed
public void actionPerformed(ActionEvent e) {
label = new Label();//Added
try {
label.setIcon(new ImageIcon
(new URL("file:/C:/Users/Ashad/JunoWorkspace/FYP1/table.png")));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
I want to add my application to the system tray when it's window is closed (similar to the Google Talk application). And then when I click the on icon in the system tray the application window becomes active again. How can I do this in Java?
final SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("images.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
this.addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == EXIT_ON_CLOSE) {
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
setVisible(true);
}
});
setVisible(false);
}
}
});
you have set DefaultCloseOperations correctly
myFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
this code line is same as myFrame.setVisible(false), then for restore of JFrame from JPopupMenu to call only myFrame.setVisible(true)
I got answer. Now when i close window its closing and when i click on System tray icon then it again open my window
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/ChatIcon1.jpeg");
final TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setToolTip("OfficeCommunicator");
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
trayIcon.displayMessage("hi", "You Opened Me Again", TrayIcon.MessageType.INFO);
setVisible(true);
}
});
}
btnnew.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
System.out.println("Hello");
packetListener.listener();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
I get a black screen when it runs. But when the packetListener.listener(); calls in the constructor it shows.
Can you please explain why this is happening?
Code that is executed from a listener executes on the EDT. I'm guessing that the packetListner.listener() method blocks in which case the GUI will freeze. You should not be blocking the EDT.
Read the section from the Swing tutorial on Concurrency for a full description of this problem and a solution.
I think the packetListener.listener(); method performs some complex operation which blocks your UI.
Better create a thread for listening the packet.
ie,use it like this
try {
System.out.println("Hello");
new Thread(new Runnable() {
public void run() {
packetListener.listener();
}
}).start();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Hope this helps you