How do I set an icon for my JFrame from the resources? - jframe

I am trying to set an icon image for my JFrame program. I can set it from an external location using this code:
JFrame jf = new JFrame("The Stick Hero");
try{
jf.setIconImage(ImageIO.read(new File("C:/sample/imagesample.png");
}catch (Exception e);
e.printStackTrace();
but if I try to do this from a resources file like this (The Images folder is in the resources src I made)
JFrame jf = new JFrame("The Stick Hero");
try{
jf.setIconImage(ImageIO.read(new File("/Images/Icon/png");
}catch (Exception e);
e.printStackTrace();
It doesn't work. It just appears as the java icon. Is there any way I can set it from the resources file?

It seems you need to add "/resources/" to your icon path:
JFrame jf = new JFrame("The Stick Hero");
try{
jf.setIconImage(new ImageIcon(Toolkit.getDefaultToolkit().getClass().getResource("/resources/Images/Icon/imagesample.png")).getImage());
}catch (Exception e);
e.printStackTrace();

Related

I need to print anchorPane in scenebuilder javafx

I'm creating customized TableViews and then i need to print them the way they look exactly, so i thought about taking like a screenshot for the AnchorPane itself then printing it, so is that possible anyways ? or what should i do exactly ?
You can place all the structures you need to print in a node for example anchorpane and then pass the anchorPane to this code,
FileChooser exportFileChooser = new FileChooser(); //The folder selector to save the pdf
exportFileChooser.setInitialDirectory(FileSystemView.getFileSystemView().getDefaultDirectory());
exportFileChooser.setInitialFileName(fileName);
File file = new File("temp/"+fileName+".png");
exportFileChooser.showSaveDialog(owner);
file.getParentFile().mkdirs();//crete temp directory if not available
final SnapshotParameters spa = new SnapshotParameters();
spa.setTransform(javafx.scene.transform.Transform.scale(0.71, 1.1));//Scaling only if required i.e., the screenshot is unable to fit in the page
WritableImage image = node.snapshot(spa, null);//This part takes the snapshot, here node is the anchorpane you sent
BufferedImage buffImage = SwingFXUtils.fromFXImage(image, null);
try {
ImageIO.write(buffImage, "png", file);//Writes the snapshot to file
} catch (Exception e) {
e.printStackTrace();
}

How to format JSP code in Eclipse with Java SWT?

I'm generating a plugin in Eclipse. My plugin generates source JSP code in an open Editor so I would like to format the whole code, just as if I pressed Ctrl+Shift+F.
I try to do that with Eclipse JDT:
Properties prefs = new Properties();
prefs.setProperty(JavaCore.COMPILER_SOURCE, CompilerOptions.VERSION_1_8);
prefs.setProperty(JavaCore.COMPILER_COMPLIANCE, CompilerOptions.VERSION_1_8);
prefs.setProperty(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, CompilerOptions.VERSION_1_8);
CodeFormatter formatter = ToolFactory.createCodeFormatter(prefs);
String texto2 = texto;
IDocument dc = new Document(texto2);
//ICompilationUnit cu = (ICompilationUnit)JavaCore.create(texto_editor);
TextEdit edit_formatear =
formatter.format(CodeFormatter.K_COMPILATION_UNIT,
texto2, 0, texto2.length(), 0, null);
if(edit_formatear!=null){
edit_formatear.apply(dc);
System.out.println("Edit: "+dc.get());
}
} catch (BadLocationException e) {
e.printStackTrace();
}
But I think this code only work with java code...
Any idea?

How to test scroll scenario using SELENIUM IDE

I'm totally a newbie in terms of using Selenium ide or any automation testing tool. Now i'm stuck with this scenario: after testing elements 1 & 2 on top of the page, i need to scroll down a bit to test the elements just below 1 & 2.
I've read something about using while and endWhile commands but you have to insert user-extension.js file first. [Link: http://www.software-testing-tutorials-automation.com/2013/09/how-to-generate-mouse-scrolling-event.html]. Problem is, it doesn't work. Any help guys? Thanks much!!
you can use javascript scroll down the page :
public void javaScriptToScrollDownPage(WebDriver driver, String coordinatevalue) throws KDTKeywordExecException {
try {
((JavascriptExecutor) driver).executeScript("scroll(0, " + coordinatevalue + ")");
} catch (Exception e) {
throw new KDTKeywordExecException("Not able to scroll down the page", e);
}
}
I have created as a function, can use this. You have to change the coordinate value ie how much you want to scroll down
Similarly for scrolling up:
public void javaScriptToScrollUpPage(WebDriver driver, String coordinatevalue) throws KDTKeywordExecException {
try {
((JavascriptExecutor) driver).executeScript("scroll(" + coordinatevalue + ",0)");
} catch (Exception e) {
throw new KDTKeywordExecException("Not able to scroll up the page", e);
}
}

Eclipse Plugin Development: How to open a TextEditor in java programmatically?

IPath path; //has some path
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
IWorkbenchPage page; //get current active page
try {
IJavaElement sourceJavaElement = JavaCore.create(file);
ITextEditor editor = (ITextEditor)JavaUI.openInEditor(sourceJavaElement); //illegal argument exception here
} catch (PartInitException | JavaModelException e1) {
e1.printStackTrace();
}
I am using this code code to open the editor but it gives an illegal argument exception when openInEditor is called on sourceJavaElement.

Issue while opening Marker in an editor programatically

I am trying to open a marker, while double-clicking on an entry from a TableViewer, inside an eclipse plug-in. I am able to get the associated resource from the marker, however nothing is happening while the openEditor method is executed.
The code is as below:
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
try {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
ReviewIssue reviewIssue = (ReviewIssue) sel.getFirstElement();
if(reviewIssue != null){
MessageDialog.openError(window.getShell(), "Insta Review", reviewIssue.getMarker().getResource());
try {
IDE.openEditor(window.getActivePage(), reviewIssue.getMarker(), true);
} catch (PartInitException e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
} catch (Exception e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
});
Please let me know, if am missing something here. Thanks in advance.
Also ignore the message dialogs, as I plan to implement the logging functionality later.
UPDATE:
Even though I created the marker on IFile, I was getting the same behaviour. I was finally able to open the editor by using the IFile, instead of the marker.
IFile iFile = markerProject.getFile(path);
//IMarker marker = iFile.createMarker("id.myMarker");
.....
IDE.openEditor(window.getActivePage(), reviewIssue.getiFile(), true);
//IDE.openEditor(window.getActivePage(), reviewIssue.getMarker()), true);
For this to work the IMarker.getResource() method must return an IFile. The code in IDE.openEditor is:
// get the marker resource file
if (!(marker.getResource() instanceof IFile)) {
IDEWorkbenchPlugin
.log("Open editor on marker failed; marker resource not an IFile"); //$NON-NLS-1$
return null;
}
so look in the .log file in the workspace .metadata directory to see if you are getting that log message.
Normally you would create a marker for a file using the IFile.createMarker method (createMarker is actually an IResource method).