My first applet shows nothing - java

I am trying to run this applet code below.But When I run it .I just get an empty Applet screen that says applet started.
public class Ass extends JApplet{
double sum;
public void init(){
double D1=Double.parseDouble(JOptionPane.showInputDialog("firstvAL"));
double D2=Double.parseDouble(JOptionPane.showInputDialog("secondvAL"));
sum=D1+D2;
}
#Override
public void print(Graphics g) {
// TODO Auto-generated method stub
super.print(g);
g.drawString("Sum is"+sum,44,44);
}
}
And I want to ask one more thing.I have removed eclips ide and downloaded Enterprice edition .And noticed these two section .Why is that? did I make a mistake while removing eclipse folder

I think you should be using paint ( ) instead of print ( ) .

Related

How to make automatic formatting of a java file seperate methods with 2 lines on VSC

I want to make VSC add 2 lines automatically between methods as part of the 'Format Document' keyboard shortcut (Shift + alt + f).
I shifted from Eclipse to VSC a couple of weeks ago and in Eclipse I had the option to freely customize pretty much any part of the document's auto-formatting. I've been searching for a way to do this on VSC but I couldn't find what I'm looking for. Not sure if it matters but I'm writing my code in java.
Basically what I want to happen is:
public class Testing {
public void func1() {
//Some code
}
public void func2(){
//Some code
}
public void func3(){
//Some code
}
Shift + alt + f ==>
public class Testing {
public void func1() {
//Some code
}
public void func2(){
//Some code
}
public void func3(){
//Some code
}
Open Command Palette and choose Java: Open Java Formatter Settings with Preview, then there'll be an notification popping up, click Generate a default profile, then java-formatter.xml will be displayed in the folder .vscode.
Open java-formatter.xml and set :
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_method" value="2"/>

JNI Unsatisfied Link Error when calling the methods of one class but not the other

I want to wrap a C++ library (PCL) in Java code using JNI, but I am having inconsistent results. I have first created a PointXYZ class for testing and it looks like this:
package pcl;
public class PointXYZ extends NativeObject {
PointXYZ() { }
#Override
public native void alloc(); // creates pointer + handle on the native side
#Override
public native void dispose(); // sets handle to 0 and deletes pointer
public native float getX();
// ...
}
I have generated the C header for this class using javah, compiled everything using CMake, tested it using its getters and setters and everything works perfectly.
static {
System.setProperty("java.library.path", System.getProperty("user.dir") + "/lib");
System.loadLibrary("pcl_java_common");
}
#Test
void attributeAccessTest() {
PointXYZ p = new PointXYZ();
p.alloc();
p.setX(3);
assertEquals(p.getX(), 3);
p.dispose();
// all is good
}
Now I have done the exact same steps for a PointXYZRGB class which inherits from PointXYZ and when I try to test that it throws java.lang.UnsatisfiedLinkError. Here is the class:
package pcl;
public class PointXYZRGB extends PointXYZ {
public PointXYZRGB() { }
#Override
public native void alloc();
#Override
public native void dispose();
public native short getR();
// ...
}
I have checked the generated .dll using Dependency Walker and the PointXYZRGB methods are all present. Anyone knows what the problem could be?
UPDATE: Here are the .dll functions as requested in the comment:
The problem was that System.setProperty("java.library.path", System.getProperty("user.dir") + "/lib"); does not actually make Java look for .dll files in the given path. It essentially does nothing. Then why do the tests work for PointXYZ? This is was my mistake of having put an older .dll into the project root folder, so it was essentially looking for methods in that.

Trying to get a list of Files in an internal Folder with libgdx

Im trying to make a level selection screen for a game
FileHandle dirHandle;
if(Gdx.app.getType() == ApplicationType.Android){
dirHandle = Gdx.files.internal("levels/");
} else{
dirHandle = Gdx.files.internal("./bin/levels");
}
for(FileHandle level : dirHandle.list()){
if(!level.name().equals("new_tileset.png") ){
Button mButton = new Button(buttonStyle);
mButton.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
// TODO Auto-generated method stub
game.setScreen(new GamePlay(game,level));
}
});
mTable.add(mButton);
}
}
this code seems to work on android and on desktop except when i export my project as an executable jar, there are no levels show, dirHandle.list() returns an empty array. Is there a work around for accessing files in my assets folder on desktop?
-Edit-
ok i tweaked my code a bit and it seems to work when i export it now. But it does not work when im running it from eclipse. Also when i upload the jar and download it and test it, the same thing happens, no levels show and dirHandle.list() is empty, i have no idea whats going on

Method called in constructor is not working after built in java netbeans

Here while I run java project in netbeans all things are working okay. But after they are built there is not any item added in combobox as it works during netbeans run. The sample code is given below.
First Login JFrame
public class Login_Frame extends javax.swing.JFrame {
welcome w = new welcome();
public Login_Frame() {
initComponents();
}
//button action perform event for dispose this window and open new welcome window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
.
.
w.setVisible(true);
this.dispose();
.
.
}
}
Second JFrame
public final class welcome extends javax.swing.JFrame {
// comboitem is class in which method for adding item in combobox from sqlite
db is declared
comboitem c = new comboitem();
// textclass is class in which method for changing lowercase text entered in
text to uppercase is declared
textclass tc = new textclass();
public welcome() {
// while I try to run the project using netbeans run project option
// logincall() method initialized and work fine.
Problem
After project built when I try to run the jar file from the cmd. it runs without any
error but logincall() method doesn't work or may be not initialized.
initComponents();
logincall();
.
.
}
public void logincall(){
//Remarks
//tc.uppercase() method is working fine after built. But other c.but_stn() like
// doen't.while during running project through netbeans all thing working fine.
c.bus_stn();
c.bus_trl();
c.inq_stn();
c.editframe();
c.userlist();
c.editTrainStation();
c.editFlightStation();
c.flightFlight();
c.pickupstand();
tc.uppercase();
}
I didn't know what is wrong with it. I searched on google but didn't find any proper answer.
There also any error showing up in netbeans. Please fill free to ask any questions if more information is needed. I appreciate all your replies.
This is my Welcome Main class's main method.
public static void main(String args[]) {
...
look and feel auto generated code
....
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new welcome().setVisible(true);
}
});
}
A couple of things that might be an issue here:
1) Are you running your GUI on the EventDispatchThread? this is mandatory for java swing GUI to be able to work properly. The main reason for this is concurrency. Details here.
2) Are you re-rendering your combobox? it is important that you do this because changes to GUI elements may not be immediately shown.
3) What Database are you using? in order to determine if the fault lies in the code or the DB you could write a test using static data, if it loads in the IDE and outside of it chances are that your code is correct but the DB isn't

Java Swing - How to double click a project file on Mac to open my application and load the file?

I have created a Mac Java Swing application, and i have set a file extension(*.pkkt) for it in the "Info.plist" file, so when double clicking that file it opens my application.
When i do that the program runs fine. Now i need to load the (*.pkkt) project in the program, but the file path is not passed as an argument to the main(...) method in Mac as happens in Windows Operating System.
After some search i found an Apple handling jar "MRJToolkitStubs" that has the MRJOpenDocumentHandler interface to handle such clicked files. I have tried using it to load that file by implementing that Interface in the main program class, but it is not working. The implemented method is never called at the program start-up.
How does this Interface run ?
------------------------------------------------- Edit: Add a Code Sample
Here is the code i am using :
public static void main( final String[] args ) {
.
.
.
MacOpenHandler macOpenHandler = new MacOpenHandler();
String projectFilePath = macOpenHandler.getProjectFilePath(); // Always Empty !!
}
class MacOpenHandler implements MRJOpenDocumentHandler {
private String projectFilePath = "";
public MacOpenHandler () {
com.apple.mrj.MRJApplicationUtils.registerOpenDocumentHandler(this) ;
}
#Override
public void handleOpenFile( File projectFile ) {
try {
if( projectFile != null ) {
projectFilePath = projectFile.getCanonicalPath();
System.out.println( projectFilePath ); // Prints the path fine.
}
} catch (IOException e) {}
}
public String getProjectFilePath() {
return projectFilePath;
}
}
As mentioned in the comment above "getProjectFilePath()" is always Empty !
On Java 9, use Desktop.setOpenFileHandler()
The proprietary com.apple.eawt packages have been removed from recent versions of Java and has been incorporated into various methods in the Desktop class. For your specific example:
import java.awt.desktop.OpenFilesHandler;
import java.awt.desktop.OpenFilesEvent;
import java.io.File;
import java.util.List;
public class MyOpenFileHandler implements OpenFilesHandler {
#Override
public void openFiles​(OpenFilesEvent e) {
for (File file: e.getFiles​()) {
// Do whatever
}
}
}
Then elsewhere, add this:
Desktop.getDesktop().setOpenFileHandler(new MyOpenFileHandler());
The OpenFilesEvent class also has a getSearchTerm() method. Say that a person used Spotlight on macOS to search for the word "StackOverflow", then decided to open up a document. With this method, can you determine that "StackOverflow" was the word they searched for, and choose to do something with that (perhaps highlight the first occurrence of the word).
You're going to want to use the Apple Java Extensions.
They should be included in any JDK that runs on Mac OS X, but the documentation is kind of hard to get. See this answer for more details.
Specifically, you'll want to make an OpenFilesHandeler.
This code snippet should work:
import com.apple.eawt.event.OpenFilesHandeler;
import com.apple.eawt.event.AppEvent;
import java.io.File;
import java.util.List;
class MacOpenHandler implements OpenFilesHandeler {
#Override
public void openFiles(AppEvent.OpenFilesEvent e) {
List<File> files = e.getFiles();
// do something
}
}
And somewhere:
import com.apple.eawt.Application;
...
MacOpenHandeler myOpenHandeler = new MacOpenHandeler();
Application.getApplication().setOpenFileHandler(myOpenHandeler);

Categories