javaw.exe incorrect redraw to gui - java

Hi there.
I have a problem to one my PC. Source code is correct. My problem is in graphic render. This bug only on one laptop.
Example screens in 1 PC:
(screen in http://)
JLabel - i.gyazo.com/deafe8b111007562e47d93ab4f9728a3.png
JMenu - i.gyazo.com/1d5c5babd243d958073d2635f64b0e8a.png
JMenuItem - http://i.gyazo.com/aca39fd386d7d186c1f22bb2d629148e.png
JMenuItem - i.gyazo.com/d78f33834913e4548edf93f8bd996002.png
On other PC's there is no problems.: http://i.gyazo.com/51e9c928627d7091bbb302b5eb78b6a2.png
I thought that problem was in java JRE (I use v1.7) and I reinstalled it. But the problem remains. Also, I switched off Desktop Composition - but there is no luck either. What should I do now? Maybe the problem is in video driver or hardware acceleration?
Tell me how to solve the problem?
[message edited]
Example "bug code":
public class Example extends JFrame {
private static final long serialVersionUID = 1L;
public Example(){
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("\u041C\u0435\u043D\u044E");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("\u0424\u0430\u0439\u043B");
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u0412\u044B\u0445\u043E\u0434");
mnNewMenu.add(mntmNewMenuItem_1);
}
public static void main(String[] args){
Example form = new Example();
form.setSize(640,480);
form.setLocationRelativeTo(null);
form.setVisible(true);
}
}
screen bug: i.gyazo.com/9fcf258e62c498f8468d47b7f73759e1.png
screen normal: i.gyazo.com/ec27e655a0209e4613c3a19863fa0e18.png
Ok, this problem solved!
From my PC, i added lines:
System.setProperty("sun.java2d.d3d", "false");
System.setProperty("sun.java2d.ddoffscreen","false");
System.setProperty("sun.java2d.noddraw", "true");
This video driver error.

Related

Displaying icon on menu bar in Java Swing

I have an application with a Swing GUI and I would like to add a search-field with a search-button (lupe icon) to the menu bar. However, the lupe icon won't display. Here is my code:
public class Ui_Frame {
public static void main(String[] args) {
SwingUtilities.invokeLater(Ui_Frame::createAndShowGUI);
}
private static void createAndShowGUI() {
f = new JFrame("Myframe");
...
JMenuBar menubar = new JMenuBar();
Icon lupeIcon = new ImageIcon("Resources/lupe_icon.png");
JButton j_searchButton = new JButton(lupeIcon);
menubar.add(j_searchButton);
...
f.setJMenuBar(menubar);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
My project structure is like
Project
Src
Ui_Frame.java
Resources
lupe_icon.png
The resulting button shows no icon at all :
I do not get any error message, it compiles without problems. Even when I try to catch any exception from the new ImageIcon(...) I'm not getting any hint at what the error is.
Any ideas as to what Im doing wrong here?
EDIT:
Here is a minimal example:
import javax.swing.*;
public class test {
private static JFrame f;
public static void main(String[] args) {
SwingUtilities.invokeLater(test::createAndShowGUI);
}
private static void createAndShowGUI() {
f = new JFrame("Test");
JMenuBar menubar = new JMenuBar();
Icon lupeIcon = new ImageIcon(test.class.getResource("/Resources/lupe_icon.png"));
JButton j_searchButton = new JButton(lupeIcon);
menubar.add(j_searchButton);
f.setJMenuBar(menubar);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(500,500);
f.setVisible(true);
}
}
As you can see I am now loading the image with class.getResource(...) as was suggested by #AndrewThompson and #SergiyMedvynskyy, but that does not solve the problem. Also I was told that my classes should not be static, but since my Main needs to be static for me to be able to run the program and I have been told that I should start the UI with SwingUtilities.invokeLater(test::createAndShowGUI);
which also forces createAndShowGUI() to be static, I do not know how to make it not static.
For me it's working without mention folder "Resources" in the path,
like that:
Icon lupeIcon = new ImageIcon(test.class.getResource("/lupe_icon.png"));
But, i'm not sure your project structure is correct.
If still doesn't work, try rename folder with small 'r', and maybe also change project structure, this is my structure:
\src\main\java... (all java files)
\src\main\resources (resources folder, it displayed for me like a package)
Note - i think best is to check why u can't get the image in debug
After switching to Icon lupeIcon = new ImageIcon(test.class.getResource("Resources/lupe_icon.png")); to load the icon instead of new ImageIcon("Resources/lupe_icon.png"); I noticed that I was getting nullpointer-exceptions, despite the image clearly being there. After some googling I found out that the resource folder apparently is not just any folder but has to be marked as the resource root folder, as suggested here.
After marking the folder everything works fine :)

How to fix the display of elements using Swing in Java

I'm working on an application using Swing in Java, however, I have a weird problem in the display, my code works perfectly fine, but the output is weird. When I run the program. The JFrame looks empty or something is missing (JMenuBar, JMenuItem, etc are invisible), then I maximize the screen, and all other stuff becomes visible, then I minimize the screen and it looks visible. I'm pretty sure that the code works fine, it's just a display problem. Can anyone help so that the first display looks fine?
Here is the code
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
Put the code below at the end. It'll work fine.
The method setVisible is an action, just like show() before JDK 1.5.
frame.setVisible(true);

MouseAdapter not working

I have a problem with java 7/8 under Ubuntu 15.04. I participate on a project, that's been up for 4 years now, so fixing things in there might be a bigger problem. Nevertheless: When I started using Ubuntu 15.04 the MouseListener stopped working. I minimized the problem to make clear, what I mean:
public class MenuGui {
public MenuGui() {
final JFrame frame = new JFrame("");
JMenuBar bar = new JMenuBar();
JMenu m1 = new JMenu("Start");
JMenuItem i11 = new JMenuItem("Action");
i11.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e) {
JOptionPane.showConfirmDialog(frame, "hi");
}
});
m1.add(i11);
bar.add(m1);
frame.setJMenuBar(bar);
frame.setVisible(true);
}
public static void main(String[] args){
new MenuGui();
}
}
When I run this example on my machine, the Menu is on the gnome menu, but clicking the Menu Entry does not do anything. Question now is: is there a way to easily place the menu back to the actual jframe or even better is there a fix for ubuntu 15.04?
Running it as root places the menu back to the jframe and it starts working just fine.
Without being an expert on JMenuItems, I think you need an ActionListener, NOT listen to mouse events.

Apple's JAVA: JMenu's submenu is not being updated

I've run into some bugs in Apple's JVM's as i mentioned in my previous question.And i can live with first bug. But second is really annoying. If i create a JMenu with submenu in it and i have to modify submenu contents in runtime, i just can't do it. Debugging shows that items are added to Jmenu (submenu). But nothing's happening in screen menubar. This looks like a problem of synchronization of real JMenu object and it's representation in Mac OS X menubar.
Here's sample code:
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class TestMenu extends JFrame{
public TestMenu() {
System.setProperty("apple.laf.useScreenMenuBar", "true");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JMenuBar mb = new JMenuBar();
mb.setName("menubar");
JMenu menu = new JMenu("menu");
JMenu submenu = new JMenu("submenu");
JMenuItem item = new JMenuItem("test item");
JMenuItem item2 = new JMenuItem("test item2");
JMenuItem subitem1 = new JMenuItem("sub item1");
JMenuItem subitem2 = new JMenuItem("sub item2");
menu.add(item);
mb.add(menu);
menu.add(submenu);
setJMenuBar(mb);
menu.add(item2);
setBounds(100, 100, 100, 100);
setVisible(true);
submenu.add(subitem1);
submenu.add(subitem2);
}
public static void main(String[] args) {
new TestMenu();
}
}
Note: I'm speaking of version 1.6.0_15 of Apple's JVM. I have to keep obsolete versions in mind to make sure my software will not expose any data because of bugs in some JVM on user's computer which was not updated since he or she bought that MAC. Current version of Java for Windows and Mac OS X works fine.
The question itself: maybe someone knows a way to manually synchronize JMenu and it's representation? Or maybe you can propose another workaround?
I've found the solution, and it wasn't so hard...
.................................
menu.add(item2);
setBounds(100, 100, 100, 100);
setVisible(true);
submenu.add(subitem1);
submenu.add(subitem2);
SwingUtilities.updateComponentTreeUI(mb); //This line updates menu representation
}
................................
Maybe I understood you wrong, but it's only a problem to display the changes? If so, try repaint() after submenu.add(subitem1) and submenu.add(subitem2).

Why won't my JMenuBar show up?

I'm trying to make a GUI in java, but JMenuBar has been giving me a hard time for two days. Can someone please tell me why it isn't showing up?
import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.Arrays;
import javax.imageio.ImageIO;
public class selectionFrame extends JFrame
{
Font name;
Font title;
public void setup() //can't use constructer because this isn't given a size until after it is constructed.
{
//getContentPane().add(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
//getContentPane().add(j);
setJMenuBar(createMenuBar());
//getContentPane().add(createMenuBar());
}
public JMenuBar createMenuBar()
{
JMenuBar menuBar;
JMenu m_file;
JMenuItem mi_save;
JMenuItem mi_load;
JMenu m_edit;
JMenuItem mi_tileHeight;
JMenuItem mi_tileWidth;
menuBar = new JMenuBar();
m_file = new JMenu("File");
m_edit = new JMenu("Edit");
mi_save = new JMenuItem("Save file", KeyEvent.VK_S);
mi_load = new JMenuItem("Load file", KeyEvent.VK_L);
mi_tileHeight = new JMenuItem("Set tile height", KeyEvent.VK_H);
mi_tileWidth = new JMenuItem("Set tile width", KeyEvent.VK_W);
menuBar.add(m_file);
m_file.add(mi_save);
m_file.add(mi_load);
menuBar.add(m_edit);
m_edit.add(mi_tileHeight);
m_edit.add(mi_tileWidth);
return menuBar;
}
public static void main(String[] args) //run
{
selectionFrame sel = new selectionFrame();
sel.setLocationRelativeTo(null);
sel.setSize((int) 400 + (sel.getInsets().left + sel.getInsets().right),(int) 400 + (sel.getInsets().top + sel.getInsets().bottom));
sel.setVisible(true);
sel.setTitle("Tiles/Meta");
sel.setResizable(false);
sel.setFocusable(true);
sel.getContentPane().setSize(sel.getSize());
sel.setLocation((int) sel.getX() - (sel.getWidth()/2),(int) sel.getY() - (sel.getHeight()/2));
sel.setup();
sel.repaint();
}
}
You have an awful lot of extra code there.
public class SelectionFrame extends JFrame
{
Font name;
Font title;
public SelectionFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMenuBar());
}
public JMenuBar createMenuBar()
{
JMenuBar menuBar;
JMenu m_file;
JMenuItem mi_save;
JMenuItem mi_load;
JMenu m_edit;
JMenuItem mi_tileHeight;
JMenuItem mi_tileWidth;
menuBar = new JMenuBar();
m_file = new JMenu("File");
m_edit = new JMenu("Edit");
mi_save = new JMenuItem("Save file", KeyEvent.VK_S);
mi_load = new JMenuItem("Load file", KeyEvent.VK_L);
mi_tileHeight = new JMenuItem("Set tile height",
KeyEvent.VK_H);
mi_tileWidth = new JMenuItem("Set tile width",
KeyEvent.VK_W);
menuBar.add(m_file);
m_file.add(mi_save);
m_file.add(mi_load);
menuBar.add(m_edit);
m_edit.add(mi_tileHeight);
m_edit.add(mi_tileWidth);
return menuBar;
}
public void main( String args[] )
{
SelectionFrame sel = new SelectionFrame();
sel.setLocationRelativeTo(null);
sel.setSize(400 + (sel.getInsets().left + > sel.getInsets().right), 400
+ (sel.getInsets().top + sel.getInsets().bottom));
sel.setTitle("Tiles/Meta");
sel.setResizable(false);
sel.setFocusable(true);
sel.getContentPane().add( new JLabel( "Content", SwingConstants.CENTER),
BorderLayout.CENTER );
sel.setLocation(sel.getX() - (sel.getWidth() / 2), sel.getY() - > (sel.getHeight() / 2));
sel.setVisible(true);
}
}
That shows up with a menu bar and everything. if you add your content to the CENTER of the content pane (by default a border layout), the center automatically fills the whole content area, you don't have to resize anything.
This shows up as a window with a menu bar and everything works fine.
What platform are you doing this on? I'm on Vista, i get what i expect to see.
What Java version are you using? Your menu bar shows up fine in 1.6.0_10 on my system. Try wrapping the body of your main method in an invokeLater() call so that it runs on the correct thread, like so:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
selectionFrame sel = new selectionFrame();
sel.setLocationRelativeTo(null);
sel.setSize((int) 400 + (sel.getInsets().left + sel.getInsets().right),
(int) 400 + (sel.getInsets().top + sel.getInsets().bottom));
sel.setTitle("Tiles/Meta");
sel.setResizable(false);
sel.setFocusable(true);
sel.getContentPane().setSize(sel.getSize());
sel.setLocation((int) sel.getX() - (sel.getWidth() / 2),
(int) sel.getY() - (sel.getHeight() / 2));
sel.setup();
sel.setVisible(true); // Follow Kendrick's advice too.
}
});
}
Turns out you HAVE to set the JMenuBar inside the JFrame's constructor. I figured this out while looking at the differences between my code, and the marked answers code.
Thank you for your wonderful answer, John Gardner. Without you I would have most likely been stuck for months.
In my case I have tracked down a missing menu bar to a bug where I set the RootPane layout, tsk tsk. The RootPane (see eg http://download.java.net/jdk7/archive/b123/docs/api/javax/swing/JRootPane.html ) controls the layout of the menu bar, so when I changed its layout manager it lost the bar.
Instead, one should use the ContentPane to layout and add components to, eg:
frame.getContentPane().setLayout(...);
frame.getContentPane().add(...);
For future reference... this has nothing to do with the component being visible (as the OP said, the frame is visible but the menu bar is not), I have working code that sets the JMenuBar outside the constructor, and while being swing-thread-safe is Good Practice, it is not the cause of the problem.
sel.setVisible(true);
Should be the last thing you call......
Also, just before the call to sel.setVisible(true); pls invoke sel.pack();
Pls note that instead of setSize it is better to use setPreferredSize, which is leveraged during frame packing.
Not directly relevant to your question, but still -- the use of a good layout manager is a huge time and frustration saviour when using Swing. MigLayout is simply an excellent one-stop layout manager.
When I compile and run it, it shows up with a menu bar with file and edit menu items. Were you expecting more?
Also, capitalize your class- SelectionFrame
EDIT:
One thing I forgot to look at, your code and every answer here is technically wrong. Often, it happens to work, but you are not allowed to do anything with Swing components unless you are in the AWT worker thread.
Normally you don't have to think about the worker thread much because every event that comes from your window will be on the worker thread anyway, but there is a tendency to forget about it when you construct your initial frame--and more often than not it just works anyway.
Sun used to recommend that you can work with components outside the AWT thread until the window has been realized (with either pack() or setVisible(true)) but this is no longer considered safe.
The easiest way to fix this might be for your main to create a swing worker thread before newing your SelectionFrame.
There is only a 50-50 chance this will fix your problem, but you should still take it into consideration whenever working on a GUI.

Categories

Resources