Can someone convert this into Clojure, I don't know to do the line setMainWindow(argument) like things....
import com.vaadin.Application;
class something {
public void init() {
Window main = new Window("The Main Window");
setMainWindow(main);
addComponent(new WindowOpener("Window Opener", main));
}
}
Update:
package app;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Window;
/**
* The Application's "main" class
*/
#SuppressWarnings("serial")
public class MyVaadinApplication extends Application{
private Window window;
#Override
public void init(){
window = new Window("My Vaadin Application");
setMainWindow(window);
window.addComponent(new Button("Click Me"));
}
}
There is a "/lib/vaadin.jar" which contains all "com.vaadin.*" things.
I think setMainWindow(window); is from the extended class. I am not going to write that method.
Literal translation:
(defn init []
(let [main (Window. "The Main Window")]
(setMainWindow main)
(addComponent (WindowOpener. "Window Opener" main))))
Though it doesn't make much sense without the context.
See http://dev.vaadin.com/wiki/Articles/ClojureScripting. Also I would suggest http://www.odesk.com.
Related
Everything works in the program when the libraries with import javax.swing.* and import java.awt.*,but it doesn't work when something from these libraries is typed that doesn't offer awt and swing options in drop-down menu.
Example:
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame{
public static MainFrame instance = null;
private MainFrame() {
inicijalizacija();
}
private void inicijalizacija() {
Toolkit kt=Toolkit.getDefaultToolkit();
Dimension velicina=kt.getScreenSize();
int visina=velicina.height;
int sirina=velicina.width;
setSize(visina/2,sirina/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("GeRuDok");
}
public static MainFrame getInstance() {
if(instance==null) {
instance=new MainFrame();
}
return instance;
}
}
e.g Toolkit cannot be released here as awt, but offers it as com.sun.javafx.tk and like others(Dimension,getScreenSize...)
I thought it had something to do with the content assistant, but it doesn't work either.
It may have something to do with the Java version 1.8 that the project is in, but I really have no idea how to fix it.
I'd appreciate it.
I'm just starting out in java and trying to use some example code I found online to get started, but for some reason, I am unable to compile this code. I on Ubuntu 16.04 and I have the "default-jdk" installed.
Here's the code:
import java.awt.*;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame{
public static void main (String argv [])
{
new Test("Window Application");
}
public Test(String title)
{
super(title);
setSize(200, 100);
addWindowListener((WindowListener) new WindowDestroyer());
setVisible(true);
}
private class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
When I try doing javac Test.java I get 2 cannot find symbol errors.
private class WindowDestroyer extends WindowAdapter
public void windowClosing(WindowEvent e)
From the Java 8 docs for WindowAdapter, it is defined as java.awt.event.WindowAdapter.
You need to import the class first:
import java.awt.event.WindowAdapter;
in addition to your other imports.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
As a side note, you might be tempted to just do
import java.awt.event.*;
to avoid import errors like that in the future.
I suggest reading the discussions on Why is using a wild card with a Java import statement bad? to have an idea on the pro's and con's of doing so.
I can see, that you create simple Swing application window and close it close window. You do it in incorrect way. It is much better to use setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) (only if you not plan to do smth. special before). And use SwingUtilities.invokeLater() to execute asynchronously on the AWT event dispatching thread:
public class Test extends JFrame {
public static void main(String... ars) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
public Test() {
super("Window Application");
setSize(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
I have a Java Application, I am wanting to format a button as either Active or Inactive (Also possibly a hover method).
The code as I would like to implement it:
//Home Tab - Active by default
home = new TabButton();
home.setSize(new Dimension(tabWidth, tabHeight));
home.setFont(getLauncherFont(34));
home.setForeground(Color.white);
home.setText("HOME");
home.setBounds(160, 0, tabWidth, tabHeight);
home.setActive(); --> This Method is what I would like to create
I already have a class to create a JButton for the tab:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
}
I have researched abstract classes. But my TabButton class already extends JButton.
I would like a method like this:
public void setActive(){
this.setBackground(Color.red);
//Any other changes a want to make regularly
}
That can simply be implemented like this home.setActive();
My Question I suppose is: Is it easy enough to implement what I am looking for, or will I have to got the long way and set all attributes manually every time?
What you've described in the post can be done like this:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {// initialize
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
// add your own methods or override JButton methods
public void setActive(){
//Add code
//example: setEnabled(true);
}
}
I am java newbie.
I was reading a tutorial book, and tried almost all code given as examples, and they all worked perfectly. But, when I tried this audio playing tutorial, even though I understood most of it, I still can't make it play. It gives me error, saying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at MouseClicker.main(MouseClicker.java:9)
Here is the code.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.net.URL;
public class MouseClicker extends Jframe{
AudioClip click;
public static void main(String[] args){
new MouseClicker();
}
public MouseClicker(){
this.setSize(400,400);
this.setTitle("Mouse Clicker");
this.addMouseListener(new Clicker());
URL urlClick = MouseClicker.class.getResource("hello.wav");
click = Applet.newAudioClip(urlClick);
this.setVisible(true);
}
private class Clicker extends MouseAdapter
public void mouseClicked(MouseEvent e){
click.play();
}
}
public class MouseClicker extends Jframe{
It's a JFrame, not a Jframe. (capital F)
Remember, Java is case sensitive!
You're missing an opening brace in the definition of the Clicker class
private class Clicker extends MouseAdapter {
^
A Java IDE can highlight these syntax errors.
Also ensure that the audio file hello.wav is located in the same location as MouseClicker.class (the bin folder in this case) so that it can be read as a resource.
I have picked a basic example of printing "Hello World" on screen when the mouse is clicked The code goes like this.
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author gauravp
*/
public class Sample extends Application {
/**
* #param args the command line arguments
*/
Button btn = new Button("ok");
//Label l = new Label("Done");
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("First Stage");
//Created anonymous inner class EventHandler<ActionEvent>
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.print("Hello World !!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
In documentation it is mentioned that EventHandler is an interface , but how come the interface be instantiated...
"new EventHandler<ActionEvent>()"
In a lot of confusion....please reply if you have any idea.. Here is the link
for the EventHandler interface :
http://docs.oracle.com/javafx/2.0/api/javafx/event/EventHandler.html
The syntax
new EventHandler<ActionEvent>() {
#Override // <- notice the annotation, it overrides from the interface.
public void handle(ActionEvent event) {
System.out.print("Hello World !!");
}
}
creates an "anonymous inner class" that implements EventHandler, and defines the handle method. If you inspect the classes generated when you compile your project, you will probably find a class file named Sample$1 (or similar) which is the class generated for this code.
You can read up on inner (anonymous) classes here: http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
To answer your question: EventHandler is an interface, and this code doesn't actually create an instance of it, but an instance of the newly declared anonymous class.
What you're seeing there is an anonymous inner class.
It's an implementation of the interface "on the spot", without creating a separate class with a name that implements the interface.
Anonymous inner classes are often used for event handlers of GUI components, as your example code shows.