I'm running a Swing application using JDK 1.7.0_45 on Windows 7. It uses a JFileChooser, which it creates with the no argument constructor, which makes the starting directory my Documents folder. I'd like to change this starting directory, without changing the source code, which I don't have, by setting the user.home system variable via -Duser.home=some-dir.
There is code in sun.awt.shell.ShellFolderManager that is supposed to make that happen, and on another forum, someone claimed it did work for him, also with Java 7 and Windows 7. When I step through his source example though, I'm getting a subclass of ShellFolderManager called sun.awt.shell.Win32ShellFolderManager2, which works differently and doesn't look at user.home. Why would that be different for him and me given the same source code, and Windows and Java versions?
Here's the source I'm working with:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.*;
public class TestFileChooser {
public static void main(final String[] args) throws IOException {
System.out.println(System.getProperty("user.home"));
final JFrame frame = new JFrame("Test");
frame.setSize(300, 200);
final JFileChooser fileChooser = new JFileChooser();
final JButton button = new JButton(new AbstractAction() {
#Override
public void actionPerformed(final ActionEvent e) {
fileChooser.showOpenDialog(frame);
}
});
button.setText("Open");
frame.add(button,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Related
Respected all,
I am making a Swing-application window in ECLIPSE. When I run the program as 'JAVA-Application' the program functions well. However when I try to run the program as "Java_applet" the components like 'command button', 'textbox' are invisible.
I am entirely new to Java. I had previously worked on C#. Kindly please help me.
import java.awt.EventQueue;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.BorderLayout;
public class sa extends Applet{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
sa window = new sa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public sa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
frame.getContentPane().add(rdbtnNewRadioButton, BorderLayout.CENTER);
}
}
You can't just have your class extend Applet and expect that that is all that is necessary for it to behave like a proper Applet. You need to give the class a proper init method and build the GUI from within this method. But most importantly you will need to read an Applet tutorial, any decent tutorial should do. Myself, I'd have my GUI extend JPanel, build the GUI in its constructor, and then I could use this JPanel in a JApplet or JFrame as needed.
As Andrew aptly notes in comment,
I think the OP should also dump the entire 'applet' part of it and develop the JFrame with an idea to launching it from a link using Java Web Start. At least then it would have a better chance for working for users of Chrome and FireFox (which are both planning to remove all support for applets).
I'm on Ubuntu 15.04 and I have written following program:
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TimeTable extends Frame {
private Frame frame;
public TimeTable(){
setupGUI();
}
private void setupGUI(){
frame = new Frame("TimeTable");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter(){
public void wndClose(WindowEvent wndEvent){
System.exit(0);
}
});
frame.setVisible(true);
}
public static void main(String[] args){
TimeTable timetable = new TimeTable();
}
}
It should be a little GUI (AWT) Test-Window.
I build it with:
>> javac TimeTable.java
And run it with:
>> java TimeTable
The ICON of the AWT APP is shown in my Launcher Sidebar, but the Window doesn't appears on my Desktop.
Why not?
You can install Java on Ubuntu without graphics libraries (headless?).
Install the standard Java including graphics libraries and it should work. Your code works fine on Windows inside IntelliJ.
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();
}
}
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.
Launch a file = launch program associated with given file and automatically open that file on start of the program.
Let's say I run IntelliJ IDEA, I run my code and main window (modal) of my program shows up. My program is in the foreground.
Then I launch a .pdf file (for now it means AcroReader will be executed) from my program. AA will show up in front of IntelliJ but behind my program.
Question
I would like AA (it is just an example here of course) to be shown in front of my program, not behind. How to do it?
Please note, this does not mean I would like to move my program to the background!
For launching files I use
java.awt.Desktop.getDesktop().open(new java.io.File(filepath));
My GUI is done in Swing.
Update 1
To rule out, any influence of custom widgets, events, and so on, I put simply JButton at the bottom of my window (JDialog) -- it is in Scala, but this piece is similar to Java syntax:
var dlg = new javax.swing.JDialog(null,"test",Dialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
var button = new JButton("Select Me");
var actionListener = new ActionListener() {
def actionPerformed( actionEvent : ActionEvent) = {
java.awt.Desktop.getDesktop().open(new java.io.File("test.pdf"))
}
};
button.addActionListener(actionListener);
dlg.add(button, BorderLayout.SOUTH);
dlg.setSize(300, 100);
dlg.setVisible(true);
Once clicked, AA is shown behind my app. Since it takes several seconds to run AA I also tried to click the button, and move the mouse away from my window. Exactly the same behaviour.
I also noted that AA is shown at the same relative position to my window, top left corner of AA is near bottom right corner of my app.
You may try something like this. On my machine (Ubuntu 10.4 LTS with Gnome2) it gives the evince (pdf-viewer) in the front, and if I close/hide evince - JDialog is placed back to the front.
On windows it may be very different, since actually without "dlg.toBack();" invocation behavior is the same.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class OpenFileTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final JDialog dlg = new javax.swing.JDialog(null, "test", JDialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton button = new JButton("Select Me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
java.awt.Desktop.getDesktop().open(
new java.io.File("/home/user/Downloads/jfreechart-1.0.13-US.pdf"));
dlg.toBack();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
});
dlg.add(button);
dlg.setVisible(true);
}
});
}
}