I have a question.
My question
How do I make the rectangle hide under the button when it slides. I do not want it to come out of the other side of the button.
package javafxapplication24;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* #author phili_000
*/
public class JavaFXApplication24 extends Application {
public Button by;
#Override
public void start(Stage stage) throws Exception {
FlowPane root = new FlowPane( );
root.setAlignment(Pos.CENTER);
Button butt = new Button("Play");
Rectangle rect = new Rectangle(200,40);
rect.setFill(Color.VIOLET);
TranslateTransition transition = new TranslateTransition(Duration.millis(6000),rect);
Scene scene = new Scene(root,700,700);
transition.setToX(-300);
root.getChildren().addAll(butt,rect);
butt.setOnAction((event) -> {
transition.play();
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I tried my best explaining, but if you have any questions. Or comment on how I can improve my questions. Please, don't hesitate to click that 'add comment' button and give me your ideas.
Add the rectangle first, then the button:
root.getChildren().addAll(text, butt);
Related
I've been working a lot in JavaFX lately, by writing code only -- no CSS no FXML and I want to keep it that way. I've managed to do all of the stuff I wanted that way except that now I can't set the text color of TextField without using CSS, so I'm asking for advice here. Is there any way to do it, no matter how hacky it is?
Note that I'm still using Java 8 version.
Considering your points : "No CSS" and "how hacky it can be.." below is one way to achieve the text coloring.
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TextField_TextColor_Demo extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
TextField textField = new TextField();
textField.setSkin(new TextFieldSkin(textField){
#Override
protected void layoutChildren(double x, double y, double w, double h) {
super.layoutChildren(x, y, w, h);
if(textField.getProperties().get("colorChanged")==null) {
textFill.setValue(Color.RED);
textField.getProperties().put("colorChanged",true);
}
}
});
StackPane root = new StackPane(textField);
Scene scene = new Scene(root, 300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String... args){
Application.launch(args);
}
}
I could not make it work. But made another solution. Although the caret color also changes.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
public class TextField_TextColor_Demo extends Application {
#Override
public void start(Stage stage) throws Exception {
TextField txt = new TextField();
CustomTextFieldSkin skin = new CustomTextFieldSkin(txt);
txt.setSkin(skin);
skin.setHighlight(Color.RED);
HBox box = new HBox(txt);;
Scene scene = new Scene(box, 300, 100);
stage.setScene(scene);
stage.show();
}
public class CustomTextFieldSkin extends TextFieldSkin{
public CustomTextFieldSkin(TextField textField) {
super(textField);
}
public void setHighlight(Paint value) {
setTextFill(value);
}
}
public static void main(String... args){
Application.launch(args);
}
}
Seems it is possible to color the caret separately, according these two answers:
https://stackoverflow.com/a/47876289/7989121
https://stackoverflow.com/a/27323035/7989121
Both use caretPath though, which I could not use due it not being visible according to my IDE
So I've linked my android app to back4app parse server. According to their documentation when I run the following code a class is supposed to be created in the database. However Android studio shows the following errors:
cannot resolve symbol SaveCallback
Method does not override method from its superclass
package com.example.android.beastt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.parse.ParseObject;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParseObject soccerPlayers = new ParseObject("SoccerPlayers");
// Store an object
soccerPlayers.put("playerName", "A. Wed");
soccerPlayers.put("yearOfBirth", 1997);
soccerPlayers.put("emailContact", "a.wed#email.io");
soccerPlayers.addAllUnique("attributes", Arrays.asList("fast", "good conditioning"));
// Saving object
soccerPlayers.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// Success
} else {
// Error
}
}
});
}
}
I run into a problem, that I really don't know how to probably create a functional buttons in Java Swing GUI ( I guess this is how I should call it). I create a print statement to check whether if my buttons work or not,and it doesn't work.Here is my code.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
/**
* Create a JFrame to hold our beautiful drawings.
*/
public class Jan1UI implements ActionListener
{
/**
* Creates a JFrame and adds our drawings
*
* #param args not used
*/
static JFrame frame = new JFrame();
static JButton nextBut = new JButton("NEXT");
static NextDayComponents nextDaycomponent = new NextDayComponents();
public static void main(String[] args)
{
//Set up the JFrame
nextBut.setBounds(860, 540, 100, 40);
/*nextBut.setOpaque(false);
nextBut.setContentAreaFilled(false);
nextBut.setBorderPainted(false);
*/
frame.setSize(1920, 1080);
frame.setTitle("Jan1UI demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
frame.add(nextBut);
frame.add(nextDaycomponent);
}
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
if (b == nextBut)
{
System.out.println("ok");
}
}
}
/*static class Butt implements ActionListener
{
}*/
You need to add an action listener to the button, but cant do that in main as it is a static method. Instead, create a constructor to do thework similar to this:
public class Jan1UI implements ActionListener
{
public static void main(String[] args)
{
Jan1UI ui = new Jan1UI();
}
public Jan1UI ()
{
JFrame frame = new JFrame();
JButton nextBut = new JButton("NEXT");
nextBut.setBounds(860, 540, 100, 40);
nextBut.addActionListener(this);
frame.setSize(1920, 1080);
frame.setTitle("Jan1UI demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
frame.add(nextBut);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("ok");
}
}
You must bound an ActionListener to the button:
nextBut.setBounds(860, 540, 100, 40);
nextBut.addActionListener(new Jan1UI());
I'm using Spring boot annotation style, and I don't understand after several searches, why the annotated field returns null.
Please below my java code:
package app.ui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
#Service
public class MyFrame extends JFrame {
private JMenu fileMenu;
private JMenuBar menuBar;
private JMenuItem openMenuItem;
#Autowired
MyDialog myDialog;
#Autowired
JdbcTemplate jdbcTemplate;
public MyFrame() {
initComponents();
setVisible(true);
}
private void initComponents() {
jdbcTemplate.toString();
this.setTitle("Vue de ma fenĂȘtre");
this.setSize(new Dimension(300, 150));
this.setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());
menuBar = new JMenuBar();
fileMenu = new JMenu();
openMenuItem = new JMenuItem();
fileMenu.setText("File");
openMenuItem.setText("Inscription");
openMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
myDialog.setVisible(true);
}
});
fileMenu.add(openMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
//
final JButton cmd1 = new JButton("Créer Table");
getContentPane().add(cmd1);
cmd1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
jdbcTemplate.execute(
"CREATE TABLE customer(id bigint(11), nom VARCHAR(55), prenom VARCHAR(55), dnaiss date)");
cmd1.setEnabled(false);
}
});
//
in this sample, jdbcTemplate.toString() returns null, and I can't understand why, because it works below in the action Performed method
Thank you for Help.
this is the way Java initializes class , first constructor got executed later class variables got initialized . untill and unless variables are part of static block .same logic is followed by spring as well .
in your example jdbcTemplate is a class variable, jdbcTemplate.toString(); is called via default constructor , while actionperformed is called on instance of MyFrame .
to know more about class initialization please check below link
http://www.javaworld.com/article/2075796/java-platform/java-101--class-and-object-initialization.html?nsdr=true
I am facing certain error while setting my job for a custom InputFormat
Below Is my Code
package com.nline_delimiter;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class NL_driver {
public static void main(String [] args) throws IOException, InterruptedException, ClassNotFoundException
{
Configuration conf=new Configuration(true);
Job job_run =new Job(conf);
job_run.setJobName("nline input format each line seperate wth delimiter");
job_run.setJarByClass(NL_driver.class);
job_run.setMapperClass(NL_mapper.class);
job_run.setReducerClass(NL_reducer.class);
job_run.setInputFormatClass(NL_inputformatter.class);;
job_run.setMapOutputKeyClass(Text.class);
job_run.setMapOutputValueClass(IntWritable.class);
job_run.setOutputKeyClass(Text.class);
job_run.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job_run,new Path("/home/hduser/input_formatter_usage.txt"));
FileOutputFormat.setOutputPath(job_run, new Path("/home/hduser/input_formatter_usage"));
job_run.waitForCompletion(true);
}
}
The Line
job_run.setInputFormatClass(NL_inputformatter.class)
shows error
The NL_inputformatter is a custom Inputformatter class that extends the FileInputFormat
Is there something I need to import for the setInputFormatClass, because the default error check in Eclipse asks me to change the setInputFormatClass to setOutFormatClass but doesn't asks for any Import.
The Sourcecode for NL_inputformatter is below.
package com.nline_delimiter;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.InputSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordReader;
import org.apache.hadoop.mapred.Reporter;
public class NL_inputformatter extends FileInputFormat<Text, IntWritable>{
#Override
public RecordReader<Text, IntWritable> getRecordReader(InputSplit input,
JobConf job_run, Reporter reporter) throws IOException {
// TODO Auto-generated method stub
System.out.println("I am Inside the NL_inputformatter class");
reporter.setStatus(input.toString());
return new NL_record_reader(job_run, (FileSplit)input);
}
}
Your help would be appreciated.
It's because you are using the FileInputFormat from the old Hadoop API with the new one. You have to change your import and your implementation:
import org.apache.hadoop.mapred.FileInputFormat;
to
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;