I need some help with gui in java. I am an absolute beginner with java and programming. I copy pasted this code in BlueJ
import javax.swing.*;
class gui{
public static void main(){
JFrame frame = new JFrame("My First GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button = new JButton("Press");
frame.getContentPane().add(button); // Adds Button to content pane of frame
frame.setVisible(true);
}
}
and I didn't get the desired results and this is what showed up in the terminal window:
Device "Mobile Intel(R) 45 Express Chipset Family (Microsoft Corporation - WDDM 1.1)" (\.\DISPLAY1) initialization failed :
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302
Can anyone tell me how to fix this error?
Just like it says, download the latest graphics driver on the official Intel website and install,then try it again.
Related
The following simple code:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ButtonTextMain {
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final JTextField field = new JTextField(20);
final JButton button = new JButton("Click to change text");
button.addActionListener(e -> button.setText(field.getText()));
final JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
can always reproduce the same bug (at least for my setups, which are given below).
The program is supposed to change the button text, according to the text-field's text, when the button is clicked.
The problem is that the text of the button reverts/changes back to its previous value on its own unexpectedly.
The problem arises when I use the Tab to alternate/navigate between the button and the text-field. Here are the specific steps which always reproduce the same bug on my setups:
Run the program.
Resize the frame to be a bit bigger than it was.
Type some short text in the text-field.
Press Tab to navigate the focus to the button.
Press Spacebar to invoke the button.
Press Tab to navigate the focus back to the text-field.
Type any letter you like into the text-field.
Notes:
I know that step 2 is relevant, because if I ommit it then the bug does not reproduce.
After step 2 (and before 3), the mouse should not be needed any more. Leave it. The focus of the program should be in the text-field as it was when the program launched.
In step 3 I usually type something like abc but the error repoduces for any other input I tried.
In step 6 you can also use Shift+Tab to navigate back to the text-field.
On step 7, after typing the first letter, you will see that the button's text changes back to its initial/previous value.
My first tested setup:
Apache NetBeans IDE 8.2, which is a bit outdated.
java -version yields:
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)
javac -version yields javac 1.8.0_161. There is a missmatch here with the runtime environment.
My second tested setup:
Apache NetBeans IDE 11.0.
java -version yields:
java version "12.0.2" 2019-07-16
Java(TM) SE Runtime Environment (build 12.0.2+10)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)
javac -version yields javac 12.0.2.
The operating system is Windows 10 on both setups.
So the question is: did I do something wrong, and if so, what is it please? First of, can anybody else reproduce the bug I am getting on their setup?
Update:
I can also reproduce this behaviour on the system L&F too, by using:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
inside just before the JTextField creation (and catching exceptions, etc).
Based on the above comments I retested and now I am able to reproduce the problem. The issue occurs when you increase the vertical height of the text field (by some minimal amount).
I guess I tested before by only increasing the horizontal size of the text field.
I found a simple work around:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ButtonTextMain {
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final JTextField field = new JTextField(20);
final JButton button = new JButton("Click to change text");
button.addActionListener(e ->
{
button.setText(field.getText());
//button.getParent().revalidate();
button.getParent().repaint();
});
final JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
This definitely seems like a bug to me.
Having said that, rarely would you want the vertical height of the text field to increase in size. So maybe you can find a layout manager that only affects the horizontal size and not the vertical height?
I don't have this problem.
I use VScode, java version 1.8.0.25.
I think problem is your compiler.
Exactly the same code running under Java 9u4 on the left and 8u144 on the right on Windows 7.
Java 9 seems to making the window larger. What is causing this - JEP 263? How can I disable it?
public class SimpleFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel("Horse"));
frame.setSize(new Dimension(200, 100));
frame.setVisible(true);
}
}
I found this obscure option in a substance bug report. This fixes the issue for Swing applications.
-Dsun.java2d.uiScale=1.0
If you're using JavaFX you'll need
-Dprism.allowhidpi=false
Unfortunately I cannot find official documentation for either of these options
I'm trying to make a java Swing program default to anti-aliasing when run on Linux. It works when I pass the settings as command line options or using _JAVA_OPTIONS, but not using System.setProperty().
I'm using Java SE 6 on Centos 5.5 via VNC with gnome metacity WM.
public class Test {
public static void main(String args[]) {
System.setProperty("useSystemAAFontSettings", "lcd");
System.setProperty("swing.aatext", "true");
JFrame frame = new JFrame("Test");
JButton button = new JButton("Button");
frame.getContentPane().add(button);
frame.setSize(100,100);
frame.setVisible(true);
}
}
Any clues what I'm doing wrong? Thanks.
PS: The swing.aatext setting seems to have no effect in any case, but I included it as I saw it as part of the solution elsewhere.
I'm just now getting into GUI's in Java and when experimenting with JFrame I get the following error:
java[3126:71534] Can't open input server /Library/InputManagers/Inquisitor
Despite the error the program runs fine, but I'd like to know what this is about as I couldn't find much about Inquisitor anywhere.
Running Netbeans 8.0.2 and Java 8 Update 40 on OS X Yosemite (10.10.2). The java code being run is:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ikkuna extends JFrame implements ActionListener{
JTextField syöte;
JLabel vastaus;
JButton painike;
public void setTitle(String string){
super.setTitle(string);
}
public Ikkuna(){
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("Celsius / Fahrenheit -muunnos");
this.setSize(400, 200);
this.setResizable(false);
JPanel paneeli = (JPanel) getContentPane();
syöte = new JTextField(10);
vastaus = new JLabel("tuntematon");
painike = new JButton("Laske");
painike.addActionListener(this);
syöte.addActionListener(this);
paneeli.setLayout(new FlowLayout(FlowLayout.LEFT, 10,10));
paneeli.add(syöte);
paneeli.add(vastaus);
paneeli.add(painike);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
vastaus.setText("" + ((Integer.parseInt(syöte.getText())*1.8+32)));
}
public static void main(String[] args){
Ikkuna i = new Ikkuna();
}
}
TLDR: Don't worry about it, it has nothing to do with your code, although you're probably better of removing Inquisitor extension as there's a good chance that it's not working properly.
InputManagers are intended to be Keyboard/Mouse extensions for applications. The idea is that the code is loaded into applications and can change their behavior in relation to Mouse/Keyboard input, but are generally used as a mechanism for extending applications. They became more restricted in 10.5, where they had to be in the system supported location only (/Library/InputManagers), and stopped being loaded entirely on 64bit mode.
Java 8 is a 64bit only application, so if you're seeing this error, then it's probably because the extension is not being loaded because it's a 64bit application and the system doesn't load extensions in this case.
The extension Inquisitor doesn't look to have been updated in a long time, and was used to extend the functionality of Safari, adding auto-complete to the search bar. The default search functionality of Safari now includes auto-complete, so I would consider it obsoleted.
If you want extended functionality for Safari, there are other plugins which extend the behaviour, such as Glims (I used to use it, but stopped because I felt it destabilized safari more than usual).
Opening the /Library/InputManagers folder (open a Finder window, hit Command-G to get a location field and type in the directory, then hit enter) will allow you to see what input managers are present on the system.
I'm trying to code my first JFrame for a simple app.
The problem is that as soon as I uncomment the
setVisible(true);
I obtain the following error message:
Cocoa AWT: Running on AppKit thread 0 when not expected.
Config:
Running eclipse on a mac OS 10.10 (Yosemite) and Java is up to date.
Here is the code:
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainFrame extends JFrame {
private JLabel appTitle;
public MainFrame(){
super("Tabum by Team Alpha");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,500);
setLayout(new BorderLayout());
setVisible(true);
}
}
I've never used your tools, but I guessing that the problem is that all GUI code should execute on the Event Dispatch Thread (EDT) and your code is not doing this.
You do this by wrapping your code in a SwingUtilities.invokeLater(...):
EventQueue.invokeLater(new Runnable()
{
public void run()
{
// add your code here
}
});
Read the section from the Swing tutorial on Concurrency for more information.
I couldn't get the solution working with Eclipse on my Mac.
However, when I loaded the exact same solution in IntelliJ IDEA, it worked!
I would classify this as a Eclipse bug.
For reference:
I'm using the current JDK 1.8.0_31
I was using Eclipse Kepler Service Release 2 (20140224-0627)
I'm on a Mac OS X Yosemite
Please let me know if it helps you or if you have a better solution.