I am trying to add a change listener to a the currentTimeProperty of a JavaFX Timeline. I would like to get the value of the current time, and have it represented as a double, so that I can perform operations on that value etc.
At the moment, this is what it looks like:
public void addAnimationListener()
{
animation.getTimeline().currentTimeProperty().addListener(new ChangeListener(){
#Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
//double curPercentageValue = arg0.getValue(); //My attempt at trying to get the value to be a double. I tried casting it and such...
System.out.println(arg0.getValue());
}
});
}
The values it prints out are e.g.:
128.33333333333334 ms
...So I could just perform String operations to remove the " ms" and then use Double.parseDouble() to get it to the desired data type...But is there a better way? One which gets the value directly?
Thanks in advance for your help!
These things are always easier to figure out if you properly type your generic types (don't ignore compiler/IDE warnings):
public static void addAnimationListener()
{
animation.getTimeline().currentTimeProperty().addListener(new ChangeListener<Duration>(){
#Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
double millis = newValue.toMillis();
System.out.println(millis + " ms");
}
});
}
Related
I can't seem to make this work let alone compile and I am at loss at how to fix it. My teacher gave us the following code (simplified for question's sake):
public static void doing1(String s) {
// add code here
}
public static void doing2(char start, char end) {
// add code here
}
public static int doing3(int num) {
// add code here
}
public static void doing4(Scanner keyboard) {
// add code here
}
I know what needs to go in each method (the work I mean) I just don't know how to print it out in the main method. We cannot change the code given to us, only add to it.
Thank you!
Overloading a method means having the same method name but the method signature (the parameters passed in) is different. So, what you have isn't actually an overload, it is for unique methods because they all have different names. As far as not compiling... what you posted looks fine - perhaps you have an error above or below that code. I believe this is what you are looking for:
public static void doing(String s) {
// add code here
}
public static void doing(char start, char end) {
// add code here
}
public static int doing(int num) {
// add code here
}
public static void doing(Scanner keyboard) {
// add code here
}
Here's a reference on overloads from the almighty John Skeet
i have code like below
ScheExeService.ScheduleId scheduleID = ScheExeService.getOneTimeScheduleId(nl.getCompany(), workItem.getId());
ScheExeService.Schedule schedule = ScheExeService.loadSchedule(nl, scheduleID.getId());
Calendar cal = Calendar.getInstance(java.util.TimeZone.getTimeZone(nl.getTimeZone()));
cal.setTime(schedule.attributes.startDate.toDate());
AND i am trying to mock "schedule.attributes.startDate.toDate()"
and ScheExeService look like below
public class ScheExeService implements blah
{
public final static class Schedule {
public final ScheduleId id;
public final ScheduleAttributes attributes;
public final String callableClassName;
public final long itemId;
public Schedule(ScheduleId id, ScheduleAttributes attributes, String callableClassName, long itemId)
{
this.id = id;
this.attributes = attributes;
this.callableClassName = callableClassName;
this.itemId = itemId;
}
}
public final static class ScheduleAttributes {
public final #Nullable LocalDate startDate;
public final #Nullable LocalTime startTime;
public final #Nullable LocalDate endDate;
public final #Nullable LocalTime endTime;
Method code
}
}
and i tried
doNothing().doThrow(new RuntimeException()).when(mockedCalenderObject).setTime(null);
but i am keep getting NPE.
how i can avoid schedule.attributes.startDate.todate() call..?
It's a good idea not to mock data/value objects like List and String, including Joda objects like LocalDate and LocalTime and your data objects like Schedule and ScheduleAttributes. This is mostly because, unless the instances are hard to create, you can just create instances in your test and have the getters and setters behave as expected.
See also: "Test smell: Everything is mocked"
Based on your code, I'd very much suggest mocking loadSchedule (which may require refactoring ScheExeService away from static methods, refactoring the second half of your system under test to accept an arbitrary Schedule, or using PowerMock) and creating your test Schedule using real calls.
public void yourMethod(NL nl, WorkItem workItem) {
ScheExeService.ScheduleId scheduleID = ScheExeService.getOneTimeScheduleId(nl.getCompany(), workItem.getId());
ScheExeService.Schedule schedule = ScheExeService.loadSchedule(nl, scheduleID.getId());
processSchedule(schedule);
}
/**
* Processes schedule for yourMethod. Exposed for testing: Create as many
* arbitrary Schedules as you want and pass them in to test this thoroughly.
*/
void processSchedule(Schedule schedule) {
Calendar cal = Calendar.getInstance(java.util.TimeZone.getTimeZone(nl.getTimeZone()));
cal.setTime(schedule.attributes.startDate.toDate());
// ...
}
I have the following code in my onCreate method:
Bundle appleData = getIntent().getExtras();
if(appleData==null) {
return;
}
String appleMessage = appleData.getString("appleMessage");
final TextView texttest = (TextView) findViewById(R.id.texttest);
texttest.setText(appleMessage);
I want to use the string stored in appleMessage in a different method on the same activity that is outside of onCreate, how do I go about doing this?
You should declare a String attribute outside of the onCreate method. For example:
private String appleMessage;
Then, inside your onCreate, simply change this line:
appleMessage = appleData.getString("appleMessage");
When you want to use it on other method, just call it. Its value will be the value setted on the onCreated method.
If you need it in several places in the class then you can make it a member variable by declaring it outside of a method
public class SomeClass extends Activity {
String appleMessage = "default text";
but if you only need it in one method you can simply pass it to the method call if you are calling the method in onCreate()
public void onCreate(Bundle bundle) {
...
String appleMessage = appleData.getString("appleMessage");
final TextView texttest = (TextView) findViewById(R.id.texttest);
texttest.setText(appleMessage);
// here
myMethod(appleMessage);
}
assuming myMethod takes a String as a parameter.
I also suggest you read the java docs and some basic tutorials before continuing with Android. It will make your life much easier.
Create a member variable on the class, which is set inside the onCreate method.
See here for more information on instance/member variables.
So:
class SomeClass extends Activity {
private String appleMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
appleMessage = ...
}
public void otherMethod() {
Log.e("LOG", "Here is appleMessage: " + appleMessage);
}
}
Declare it inside your class and no in the method.
e.g.
private String appleMessage;
and use getters and setters for it
public String getAppleMessage(){
return appleMessage;
}
This has baffled me for a while now and I cannot seem to get the grasp of it. I'm using Cell Value Factory to populate a simple one column table and it does not populate in the table.
It does and I click the rows that are populated but I do not see any values in them- in this case String values. [I just edited this to make it clearer]
I have a different project under which it works under the same kind of data model. What am I doing wrong?
Here's the code. The commented code at the end seems to work though. I've checked to see if the usual mistakes- creating a new column instance or a new tableview instance, are there. Nothing. Please help!
//Simple Data Model
Stock.java
public class Stock {
private SimpleStringProperty stockTicker;
public Stock(String stockTicker) {
this.stockTicker = new SimpleStringProperty(stockTicker);
}
public String getstockTicker() {
return stockTicker.get();
}
public void setstockTicker(String stockticker) {
stockTicker.set(stockticker);
}
}
//Controller class
MainGuiController.java
private ObservableList<Stock> data;
#FXML
private TableView<Stock> stockTableView;// = new TableView<>(data);
#FXML
private TableColumn<Stock, String> tickerCol;
private void setTickersToCol() {
try {
Statement stmt = conn.createStatement();//conn is defined and works
ResultSet rsltset = stmt.executeQuery("SELECT ticker FROM tickerlist order by ticker");
data = FXCollections.observableArrayList();
Stock stockInstance;
while (rsltset.next()) {
stockInstance = new Stock(rsltset.getString(1).toUpperCase());
data.add(stockInstance);
}
} catch (SQLException ex) {
Logger.getLogger(WriteToFile.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Connection Failed! Check output console");
}
tickerCol.setCellValueFactory(new PropertyValueFactory<Stock,String>("stockTicker"));
stockTableView.setItems(data);
}
/*THIS, ON THE OTHER HAND, WORKS*/
/*Callback<CellDataFeatures<Stock, String>, ObservableValue<String>> cellDataFeat =
new Callback<CellDataFeatures<Stock, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<Stock, String> p) {
return new SimpleStringProperty(p.getValue().getstockTicker());
}
};*/
Suggested solution (use a Lambda, not a PropertyValueFactory)
Instead of:
aColumn.setCellValueFactory(new PropertyValueFactory<Appointment,LocalDate>("date"));
Write:
aColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
For more information, see this answer:
Java: setCellValuefactory; Lambda vs. PropertyValueFactory; advantages/disadvantages
Solution using PropertyValueFactory
The lambda solution outlined above is preferred, but if you wish to use PropertyValueFactory, this alternate solution provides information on that.
How to Fix It
The case of your getter and setter methods are wrong.
getstockTicker should be getStockTicker
setstockTicker should be setStockTicker
Some Background Information
Your PropertyValueFactory remains the same with:
new PropertyValueFactory<Stock,String>("stockTicker")
The naming convention will seem more obvious when you also add a property accessor to your Stock class:
public class Stock {
private SimpleStringProperty stockTicker;
public Stock(String stockTicker) {
this.stockTicker = new SimpleStringProperty(stockTicker);
}
public String getStockTicker() {
return stockTicker.get();
}
public void setStockTicker(String stockticker) {
stockTicker.set(stockticker);
}
public StringProperty stockTickerProperty() {
return stockTicker;
}
}
The PropertyValueFactory uses reflection to find the relevant accessors (these should be public). First, it will try to use the stockTickerProperty accessor and, if that is not present fall back to getters and setters. Providing a property accessor is recommended as then you will automatically enable your table to observe the property in the underlying model, dynamically updating its data as the underlying model changes.
put the Getter and Setter method in you data class for all the elements.
I was just trying to code the following extension method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4Testing
{
static class ExtensionMethods
{
public static void AssignMe(this int me, int value)
{
me = value;
}
}
}
But it is not working, i mean, can I use an extension method to alter values from extended classes? I don't want to change void return type to int, just changing extended class value. Thanks in advance
Your example uses int, which is a value type. Classes are reference types and behaves a bit differently in this case.
While you could make a method that takes another reference like AssignMe(this MyClass me, MyClass other), the method would work on a copy of the reference, so if you assign other to me it would only affect the local copy of the reference.
Also, keep in mind that extension methods are just static methods in disguise. I.e. they can only access public members of the extended types.
public sealed class Foo {
public int PublicValue;
private int PrivateValue;
}
public static class FooExtensions {
public static void Bar(this Foo f) {
f.PublicValue = 42;
// Doesn't compile as the extension method doesn't have access to Foo's internals
f.PrivateValue = 42;
}
}
// a work around for extension to a wrapping reference type is following ....
using System;
static class Program
{
static void Main(string[] args)
{
var me = new Integer { value = 5 };
int y = 2;
me.AssignMe(y);
Console.WriteLine(me); // prints 2
Console.ReadLine();
}
public static void AssignMe(this Integer me, int value)
{
me.value = value;
}
}
class Integer
{
public int value { get; set; }
public Integer()
{
value = 0;
}
public override string ToString()
{
return value.ToString();
}
}
Ramon what you really need is a ref modifier on the first (i.e. int me ) parameter of the extension method, but C# does not allow ref modifier on parameters having 'this' modifiers.
[Update]
No workaround should be possible for your particular case of an extension method for a value type. Here is the "reductio ad absurdum" that you are asking for if you are allowed to do what you want to do; consider the C# statement:
5.AssignMe(10);
... now what on earth do you think its suppose to do ? Are you trying to assign 10 to 5 ??
Operator overloading cannot help you either.
This is an old post but I ran into a similar problem trying to implement an extender for the String class.
My original code was this:
public static void Revert(this string s)
{
char[] xc = s.ToCharArray();
s = new string(xc.Reverse());
}
By using the new keyword I am creating a new object and since s is not passed by reference it will not be modified.
I changed it to the following which provides a solution to Ramon's problem:
public static string Reverse(this string s)
{
char[] xc = s.ToCharArray();
Array.Reverse(xc);
return new string(xc);
}
In which case the calling code will be:
s = s.Reverse();
To manipulate integers you can do something like:
public static int Increment(this int i)
{
return i++;
}
i = i.Increment();