JFrame equivalent for C# handle pointer - java

I am currently trying to port code from C# to java. In the C# code, the panel.handle part returns an intptr of the current panel. The intptr is then passed to a DLL method. The C# code is the following:
IntPtr Handle = Panel1.Handle;
The same DLL must be used. I have found something using Win32 and JNI but I have not understood how to use it. Does anyone know the java swing equivalent to this method?

Thanks to the link in the comment and some other research I have managed to find the equivalent to the Handle property in c#. Here is what I have done to anyone who is facing the same problem:
import javax.swing.*;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
public class main {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
final HWND hwnd = new HWND(Native.getComponentPointer(jFrame));
System.out.println(hwnd);
}
}
Please Note that this only works with windows machines since only windows have the concept of everything is a window and every window has a handle.

Related

Using VC++ dll using JNA

I am using a BTICARD.DLL, which is the dll of Arinc429 card. I need to write wrapper class in Java for the functions like BTICard_CardOpen for example.
I Had written an interface below BTICardAPI.java:
package NLIPjt;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.Native;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public interface BTICardAPI extends StdCallLibrary {
BTICardAPI INSTANCE = (BTICardAPI) Native.loadLibrary("BTICARD", BTICardAPI.class);
int BTICard_CardOpen(Pointer LPHCARD, int cardnum);
}
and my Java implementation prog
BTICardTest.java:
package NLIPjt;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public class BTICardTest {
public static void main(String args[]) {
BTICardAPI BTI1 = BTICardAPI.INSTANCE;
int iErr;
int CardNo = 0;
Pointer CardHandle = null;
iErr = BTI1.BTICard_CardOpen(CardHandle, CardNo);
System.out.println("Error Value: " + iErr);
}
}
i get the following error in netbeans IDE:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'BTICard_CardOpen': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:245)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:566)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:542)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:528)
at com.sun.jna.Library$Handler.invoke(Library.java:228)
at com.sun.proxy.$Proxy0.BTICard_CardOpen(Unknown Source)
at NLIPjt.BTICardTest.main(BTICardTest.java:14)
Looking for a solution!!
According to the documentation you need to make the library available. There are three ways to do this.
Make your target library available to your Java program. There are
several ways to do this:
The preferred method is to set the jna.library.path system property to
the path to your target library. This property is similar to
java.library.path, but only applies to libraries loaded by JNA.
Change the appropriate library access environment variable before
launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux,
and DYLD_LIBRARY_PATH on OSX.
Make your native library available on your classpath, under the path
{OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for
native libraries (e.g. win32-x86, linux-amd64, or darwin). If the
resource is within a jar file it will be automatically extracted when
loaded.

Using bluetooth in java

I'm attempting to write a Java application that will open up the device it is run on to receiving audio over Bluetooth and then playing that audio.
I looked up the JavaDoc for javax.bluetooth, frankly I'm a little confused by it, but while importing javax.bluetooth it failed to compile, So I'm not sure whats going on.
This is what I have so far if it helps:
import javax.swing.*;
import javax.obex.*;
import javax.bluetooth.*;
public class BluetoothConnection{
public static void main(String[] args) {
DiscoveryListener alpha;
startInquiry(777, alpha);
}
}

How to modify the "About" OS X dialog panel in a Java application?

What is the simplest way to customize this panel considered that the application is written in Java 7 and my favorite environment is Mac Os?
Thanks
You need to use the com.apple.eawt classes. For example, this scratch program shows a Java dialog instead of the Mac one.
import javax.swing.*;
import com.apple.eawt.*;
import com.apple.eawt.AppEvent.*;
public class Foo
extends JPanel
implements AboutHandler
{
public static void main(String[] args)
throws Exception
{
Foo r = new Foo();
}
public Foo() {
Application.getApplication().setAboutHandler(this);
}
public void handleAbout(final AboutEvent e) {
JOptionPane.showMessageDialog(null, "hello, world");
}
}
Unfortunately all of this stuff is deprecated. Apple used to develop and support all of this but doesn’t anymore, and there are a lot of dead links around the internet.
I figured out the API from reading the Javadoc comments in the source code for these classes.
And running mdfind -name apple | grep -i jar turned up /usr/share/java/Stubs/AppleJavaExtensions.jar on my machine, which allowed the above program to compile and run on my machine. But I have no idea where that file came from or if it’ll be in future versions of Mac OS.

Java Swing fullscreen with keyboard input on Mac OS X

I'm having some issues getting my Java JFrame to be fullscreen on all OS (Windows, Mac, Linux). It seems whatever solution I find it does run on one OS but not on others or has some other serious bugs. I wanted to use the setFullScreenWindow(window w) method to properly initiate a fullscreen because setExtendedState(...) won't work on Mac/Linux as the menubar and taskbar are still visible.
The setFullScreenWindow(...) method worked fine on all environments until Java 7 came along and now there seems to be an issue that as soon as you enter fullscreen mode the application no longer responds to key events on Mac OS X. The application works just fine on Windows.
Does anyone have a clue how I could possibly work around this issue?
Note:
The workaround described here (FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion) does not work for Windows because it will result in the JFrame flickering and not opening properly.
The fullscreen approach described here is the same used below which does not work because of problems with the key input: (How to make a JFrame really fullscreen?)
Example Code:
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class FullScreenKeyTest extends JFrame {
public void createFrame() {
initKey();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
this.setUndecorated(true);
this.setVisible(true);
gd.setFullScreenWindow(this);
}
private void initKey() {
Action keyAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("Escape key pressed");
setVisible(false);
System.exit(0);
}
};
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "keyPress");
this.getRootPane().getActionMap().put("keyPress", keyAction);
}
public static void main(String[] args) {
FullScreenKeyTest testFrame = new FullScreenKeyTest();
testFrame.createFrame();
}
}
This is a bit flaky as I can get it to work and break it at the same time.
With your example code, I added
getContentPane().setFocusable(true);
getContentPane().requestFocus();
To the end of the createFrame method, and instead of registering the action against the root pane, I registered against the content pane
I found that keyboard inputs work if you use the native OS X Lion fullscreen API. See solution to the question Fullscreen feature for Java Apps on OSX Lion.

Can "Windows Error Reporting" be used for non-fatal Java problems?

I was wondering if there was a way to use Windows Error Reporting from "inside" a Java program?
In other words, use the mechanism to report exceptions back to a central location without having the actual JVM crash (which to my understanding is what triggers this in the first place).
The idea here is to make it easier to collect bug reports from Windows users.
I'd like to hear too if it can be part of a controlled shutdown. I.e. not a JVM crash but a normal, controlled exit from a Java program.
After thinking it over, I think that it would be sufficient for our purposes to create a set of text files (or perhaps just pipe in a single text stream) to a tiny Windows application located inside our part of the file system. Said Windows application then crashes prominently and cause a report to be sent including the text provided by us. Would that work?
You can definitely use the Windows Error Reporting API that ships in wer.dll as part of the Win32 API.
The best way to call DLL-based functions from Java is using the actively developed Java Native Access project.
In order to make the required Win32 API calls, we'll need to teach JNA about at least these functions:
HRESULT WINAPI WerReportCreate(
__in PCWSTR pwzEventType,
__in WER_REPORT_TYPE repType,
__in_opt PWER_REPORT_INFORMATION pReportInformation,
__out HREPORT *phReportHandle
);
HRESULT WINAPI WerReportSubmit(
__in HREPORT hReportHandle,
__in WER_CONSENT consent,
__in DWORD dwFlags,
__out_opt PWER_SUBMIT_RESULT pSubmitResult
);
and also this struct:
typedef struct _WER_REPORT_INFORMATION {
DWORD dwSize;
HANDLE hProcess;
WCHAR wzConsentKey[64];
WCHAR wzFriendlyEventName[128];
WCHAR wzApplicationName[128];
WCHAR wzApplicationPath[MAX_PATH];
WCHAR wzDescription[512];
HWND hwndParent;
} WER_REPORT_INFORMATION, *PWER_REPORT_INFORMATION;
To do this, we'll create WER.java:
package com.sun.jna.platform.win32;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Wer extends StdCallLibrary {
Wer INSTANCE = (Wer) Native.loadLibrary("wer", Wer.class,
W32APIOptions.DEFAULT_OPTIONS);
public static class HREPORT extends HANDLE {
public HREPORT() { }
public HREPORT(Pointer p) { super(p); }
public HREPORT(int value) { super(new Pointer(value)); }
}
public static class HREPORTByReference extends ByReference {
public HREPORTByReference() {
this(null);
}
public HREPORTByReference(HREPORT h) {
super(Pointer.SIZE);
setValue(h);
}
public void setValue(HREPORT h) {
getPointer().setPointer(0, h != null ? h.getPointer() : null);
}
public HREPORT getValue() {
Pointer p = getPointer().getPointer(0);
if (p == null)
return null;
if (WinBase.INVALID_HANDLE_VALUE.getPointer().equals(p))
return (HKEY) WinBase.INVALID_HANDLE_VALUE;
HREPORT h = new HREPORT();
h.setPointer(p);
return h;
}
}
public class WER_REPORT_INFORMATION extends Structure {
public DWORD dwSize;
public HANDLE hProcess;
public char[] wzConsentKey = new char[64];
public char[] wzFriendlyEventName = new char[128];
public char[] wzApplicationName = new char[MAX_PATH];
public char[] wzDescription = new char[512];
public HWND hwndParent;
dwSize = new DWORD(size());
}
public abstract class WER_REPORT_TYPE {
public static final int WerReportNonCritical = 0;
public static final int WerReportCritical = 1;
public static final int WerReportApplicationCrash = 2;
public static final int WerReportApplicationHang = 3;
public static final int WerReportKernel = 4;
public static final int WerReportInvalid = 5;
}
HRESULT WerReportCreate(String pwzEventType, int repType, WER_REPORT_INFORMATION pReportInformation, HREPORTByReference phReportHandle);
HRESULT WerReportSubmit(HREPORT hReportHandle, int consent, DWORD dwFlags, WER_SUBMIT_RESULT.ByReference pSubmitResult);
}
I just knocked that together from the MSDN dcoumentation in a few minutes--in case it's incomplete, or incorrect, there are tons of examples and pretty good documentation on the JNA web site.
In order to run JNA, you'll need jna.jar and platform.jar, which you can also grab from the JNA web site.
I've had to program inter-operability between Java and .NET in the past, so I started to do some research on using .NET to interact with WER with the intention to see if it's possible to interact with WER in a .NET app that you could then interface with Java. Interestingly, I came across this post on SOF - Is there a .Net API for Windows Error Reporting
That post has some good information related to interacting with WER.
I know that the post revolves around using .NET against WER, but as you're trying to interact with native Windows functionality, I recommend using .NET to interact with it as it's SO much easier to interact with native Windows resources using .NET than it is with Java (and it usually takes half the code it would in Java). You could then interface to the .NET app (might be best set up as a Windows service) with Java (for instance, you could use temporary "trigger" files in the .NET app to indicate when the .NET app is done with it's processing; the Java app could then probe to see when that "trigger" file has been created and continue from there...).
As the accepted answer in that post recommends, though, it might be best to use Windows Quality Online Services instead of programming something to interact with WER as it appears that WER is not meant to be used by other applications.
You could interop with the native WER library functions.
Here is the relevant MSDN documentation:
Creating a report
Submitting a report
Perhaps someone with more Java-interop experience can provide you with code examples, I'm more of a .NET guy unfortunately.
EDIT:
I did some more research, and I think you could try using GlueGen or SWIG to generate the Java bindings. You will have to download the Windows SDK to get the werapi header file if you want to generate the bindings.
Do you mean to say that instead of hs_err_pid*.log files being created WER should log the logs with it ??Or you intent to log the detailed exception message of any exception which can be handled in Java programs in WER ?
If the case is 1 then obviously you can't do it neatly. You may have a separate daemon running all the time to look for hs_err_pid*.log created->interpret it using external libraries->use WER APIs suggested above to write it in WER recorords.
If the case is 2nd one then you may like to make a JNI call and have the calls made to WER APIs to write stuff in WER.

Categories

Resources