After done a LFS(linuxfromscratch) system, I has 2 problems with input, some applications like google docs (presentations edit in browsers) and some java apps apparently do not recognize the keyboard input, the first one was resolved adding UTF-8 locale, but the java no way.
So I do some research and limited it in a awt scope. It means all apps writed in java awt do not recognize the keyboard inputs.
I tried http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/awt.html , but no success to solve the issue.
I too recompiled the libXt after the new locale.
I don't have Qt, awt is qt dependent?
Using eclipse(which is java app and do not have the issue), I created a little app using awt to reproduce the problem. The problem is here but it's don't give a stack trace nor a message warning.
From this moment I haven't idea how to solve or track this issue.
Some help/tip?
Here a simple program to reproduce the problem (jdk1.7.0_21)
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
public class testmain extends java.applet.Applet{
public void init()
{
Panel p;
setLayout(new BorderLayout());
p = new Panel();
TextArea x = new TextArea();
x.setFocusTraversalKeysEnabled(true);
x.setText("asdf");
x.setEditable(true);
p.add(x);
add("Center", p);
p = new Panel();
p.add(new Button("One"));
p.add(new Button("Two"));
Choice c = new Choice();
c.addItem("one");
c.addItem("two");
c.addItem("three");
p.add(c);
add("South", p);
}
public static void main(String [] args)
{
Frame f = new Frame("Example 4");
testmain ex = new testmain();
ex.init();
f.add("Center", ex);
f.pack();
f.show();
}
}
Related
I can't seem to find a solution online for why I'm getting this error on attempted run
I'm working on making a simple test system for a different program when are button press will yield value in a text box. I would like them to be on different lines to make it cleaner, so I looked into layouts. I decided a Box Layout would fit me best. I looked at different examples before attempting this and my code ended up looking like this, (apologies for the messy code)
Update
Got the box layout error to disappear but the code will not center them on the panel/frame. The label and button align left while the textfield becomes very large. I don't need it todo that
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import static javax.swing.BoxLayout.Y_AXIS;
import static javax.swing.SwingConstants.CENTER;
public class button extends JFrame {
static JFrame f;
static JButton b;
static JLabel l;
// main class
public static void main(String[] args)
{
// create a new frame to stor text field and button
f = new JFrame("panel");
BoxLayout layout = new BoxLayout(f, BoxLayout.Y_AXIS);
f.setLayout(layout);
// create a label to display text
l = new JLabel("panel label");
b = new JButton("button1");
JTextField textArea = new JTextField(5);
textArea.setEditable(false);
//textArea.append("Hello World");
// create a panel to add buttons
JPanel p = new JPanel();
// add buttons and textfield to panel
f.add(p);
f.setSize(300, 300);
p.add(l);
p.add(b);
p.setBackground(Color.white);
p.add(textArea);
f.show();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Random r = new Random();
textArea.setText(String.valueOf(r));
}
});
}
}
Error
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at java.desktop/javax.swing.BoxLayout.checkContainer(BoxLayout.java:461)
at java.desktop/javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:245)
at java.desktop/javax.swing.BoxLayout.addLayoutComponent(BoxLayout.java:278)
at java.desktop/java.awt.Container.addImpl(Container.java:1152)
at java.desktop/java.awt.Container.add(Container.java:1029)
at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553)
at java.desktop/java.awt.Container.add(Container.java:436)
at button.main(button.java:36)
I would like the three items to all to be stacked one on top of another with a space between them. The order doesn't matter right now.
Swing was first added to the JDK in 1998 and has undergone a lot of changes since. Unfortunately, when you read Web pages about Swing, it is not obvious when that page was last updated. Consequently you may be learning outdated techniques for writing Swing code.
First of all, according to the code you posted, class button does not need to extend class JFrame since you use a static variable as your application's JFrame. Also, JFrame is a top-level container which makes it a special kind of container and not the same kind of continer as a JPanel. You need to set the layout manager for your JPanel and then add the JLabel, JTextField and JButton to that JPanel. And then add the JPanel to the JFrame.
Calling method pack() of class JFrame will automatically set the preferred sizes for the components inside the JFrame. It appears in the code below.
Please also look at Java coding conventions which allows others to more easily read and understand your code. And note that, according to these conventions, I renamed your class from button to Buttons and also because there are already several class in the JDK named Button.
Here is my rewrite of your code...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Buttons implements Runnable {
public void run() {
createAndShowGui();
}
private void createAndShowGui() {
JFrame f = new JFrame("Box");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel p = new JPanel();
BoxLayout layout = new BoxLayout(p, BoxLayout.Y_AXIS);
p.setLayout(layout);
JLabel l = new JLabel("panel label");
JTextField textField = new JTextField(5);
JButton b = new JButton("button1");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Random r = new Random();
textField.setText(String.valueOf(r.nextBoolean()));
}
});
p.add(l);
p.add(textField);
p.add(b);
f.add(p);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
Buttons instance = new Buttons();
EventQueue.invokeLater(instance);
}
}
I've recently begun learning Java. I've taken the usual approach that I take, to dissect other code and learn how it works. My first goal is to simply animate a GIF, but I can't make heads or tails of this problem.
I followed the answer in this question. But when I compile and run the program, I get a blank window. This window does have the title assigned to it, but with no GIF inside. Here is my current code:
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;
class giftest {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://i.imgur.com%2FTdRNfPP.jpg");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("j0in Dedsec!");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
I get no errors when compiling or running this code, and here is a screenshot of the window that shows:
Any help whatsoever is highly appreciated.
-Defalt
I am working on a Java Swing GUI and am having a minor issue with tool tip text on popup menu items.
Basically when you hover over a JMenuItem it is supposed to leave that JMenuItem selected and display the desired tool tip text.
What actually happens is when the tool tip text is made visible a StateChange event seems to cause the relevant JMenuItem to lose selection status and so the tool tip text very quickly disappears. Note that this only happens the first time, if you move your mouse around you can get the JMenuItem to select again and it will also display the tool tip text properly. I could leave this but I was hoping to set a delay through the ToolTipManager's sharedInstance() which at this point would hurt the user-friendly side of things since the user would have to wait twice as long after realizing the issue themselves.
I built a very simple demo that reflects the problem I am seeing, am I missing something or is this just a Java 8 with Mac issue?
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestFrame {
static JFrame jf = new JFrame();
public static void main(String[] args){
jf = new JFrame();
JPanel jp = new JPanel();
jp.setBackground(Color.white);
jp.setForeground(Color.black);
JPopupMenu p = new JPopupMenu();
JMenuItem jmi = new JMenuItem("An option");
jmi.setToolTipText("mouse over text");
jmi.addChangeListener(new ChangeListener(){
#Override
public void stateChanged(ChangeEvent e) {
System.out.println("CHANGED by: "+e.getSource().toString());
}});
p.add(jmi);
jp.setComponentPopupMenu(p);
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setSize(1000, 500);
jf.setPreferredSize(jf.getSize());
jf.setVisible(true);
}
}
For reference, I tried this modified version that runs on the event dispatch thread. It's seems improved, but it still fails intermittently. It looks like a regression.
Console:
$ javac TestFrame.java ; java TestFrame
1.8.0_31
10.9.5
…
Code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.event.ChangeEvent;
/** #see http://stackoverflow.com/a/28160300/230513 */
public class TestFrame {
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.version"));
EventQueue.invokeLater(() -> {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JPopupMenu p = new JPopupMenu();
JMenuItem jmi = new JMenuItem("An option");
jmi.setToolTipText("Mouse over text");
jmi.addChangeListener((ChangeEvent e) -> {
System.out.println("Changed by: " + e.getSource().toString());
});
p.add(jmi);
jp.setComponentPopupMenu(p);
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.pack();
jf.setSize(320, 240);
jf.setVisible(true);
});
}
}
I used the following code:
JLabel jLabel = new JLabel(new ImageIcon(someImage));
I don't get it.. sometimes the image appears when I run the code and sometimes not.. I'm not always getting the same output. Anyone can explain why this could happen?!
Without more code for context it's hard to know for sure, but whenever I hear about a Swing problem that sometimes works, I tend to suspect threading problems; if your GUI is, say, a dialog that is not being built on the Event Dispatch Thread then this sort of randomness is common. If you are not sure about your threading, put this at the top of your method where this code is being executed:
System.out.println(String.format("This code %s running on the Event Dispatch Thread.", (javax.swing.SwingUtilities.isEventDispatchThread() ? "IS" : "IS NOT"));
and see what you get.
Here is simple example for you with JLabel and Icon, examine that :
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Example extends JFrame {
public Example() {
URL resource = getClass().getResource("image.png");
ImageIcon icon = new ImageIcon(resource);
JLabel l = new JLabel(icon);
add(l);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String... s){
new Example();
}
}
image.png is my image, that is placed in the same folder as the class.
Right, so Ive got an interesting problem here concerning SWT and swing integration on mac running java 1.7. Im trying to embed an SWT Browser widget into my swing project as a panel which is pretty simple to do on java version 1.6. There has been a number of posts which explain how to do this with SWT_AWT bridge classes along with the following example:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MySWTBrowserTest implements ActionListener {
public JButton addCodeButton;
public JButton launchBrowserButton;
public JTextField inputCode;
public JFrame frame;
static Display display;
static boolean exit;
public MySWTBrowserTest() {
frame = new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
inputCode = new JTextField(15);
inputCode.setText("999");
addCodeButton = new JButton("Add Code");
addCodeButton.addActionListener(this);
addCodeButton.setActionCommand("addcode");
launchBrowserButton = new JButton("Launch Browser");
launchBrowserButton.addActionListener(this);
launchBrowserButton.setActionCommand("launchbrowser");
mainPanel.add(inputCode);
mainPanel.add(addCodeButton);
mainPanel.add(launchBrowserButton);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("addcode")) {
} else if (e.getActionCommand().equals("launchbrowser")) {
createAndShowBrowser();
}
}
public void createAndShowBrowser() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Canvas canvas = new Canvas();
f.setSize(850, 650);
f.getContentPane().add(canvas);
f.setVisible(true);
display.asyncExec(new Runnable() {
#Override
public void run() {
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(800, 600);
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
browser.setSize(800, 600);
browser.setUrl("http://www.google.com");
shell.open();
}
});
}
public static void main(String args[]) {
//SWT_AWT.embeddedFrameClass = "sun.lwawt.macosx.CEmbeddedFrame";
display = new Display();
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MySWTBrowserTest mySWTBrowserTest = new MySWTBrowserTest();
}
});
while (!exit) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Im using the swt-3.8M5-cocoa-macosx-x86_64 JAR files which obviously need to be included to run the above example. When using both the 32 bit and 64 bit versions of the 1.6 JDK, this runs perfectly fine, but when switching to the JDK 1.7 or 1.8 VM the reproducible error is thrown:
2012-05-14 15:11:30.534 java[1514:707] Cocoa AWT: Apple AWT Java VM was loaded on first thread -- can't start AWT. (
0 liblwawt.dylib 0x00000008db728ad0 JNI_OnLoad + 468
1 libjava.dylib 0x00000001015526f1 Java_java_lang_ClassLoader_00024NativeLibrary_load + 207
2 ??? 0x00000001015a4f90 0x0 + 4317663120
)
_NSJVMLoadLibrary: NSAddLibrary failed for /libjawt.dylib
JavaVM FATAL: lookup of function JAWT_GetAWT failed. Exit
Java Result: 255
Ive inspected the java 1.7 vm and did find the libraries there, so Im struggling to see what could cause it to not load that library. Of course I make sure to use: -XstartOnFirstThread as one of the VM parameters, as is required for the SWING/AWT integration.
On a further note, I have tried the DJ Native Widgets framework, and it throws the exact same error as it also uses the underlying SWT framework.
To reproduce the effects i suggest installing JDK 1.7 (release not the developer preview) on mac, downloading the: http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops4/S-4.2M7-201205031800/swt-S-4.2M7-201205031800-cocoa-macosx-x86_64.zip to get the library and then running it with the -XstartOnFirstThread -d64 java 1.7 vm.
really hoping someone has been able to sort this out as Im sure Im not the only one trying to integrate SWT into swing on the 1.7 vm
I also spent 8 hours on google to see if this error has been reproduced anywhere else, and it has come up on a few Matlab mailing lists, but other than that I haven't been able to find something even close to a solution.
Thanks in advance.
>> UPDATE 1
Looks like we may have a winner: https://bugs.eclipse.org/bugs/show_bug.cgi?id=374199
Going to monitor this and see where it goes.
>> UPDATE 2
Here is a working example: https://stackoverflow.com/a/27754819/363573
Unfortunately there's no good answer to this. In Java 7 the AWT has been completely rewritten to use CoreAnimation layers. The SWT assumes that an AWT Canvas will be backed by an NSView but that's no longer the case. Your only choice right now is to stick with Java 6.
The AWT team is aware of the problem but you may want to file another bug on bugs.sun.com.