Binary XML file line #11: Error inflating class fragment - android-activity

I am getting two Errors in my Code.
Caused by: java.lang.IllegalStateException: Fragment com.example.dfoley.write_to_file.topFragment did not create a view.
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment both pointing to Line MainActivity.java:21 which is the following setContentView(R.layout.activity_main);
bottomFragment
package com.example.dfoley.write_to_file;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
public class bottomFragment extends ListFragment {
private ArrayAdapter<StateUser> adapter;
#Override
public void onActivityCreated(Bundle saveInstanceState){
ArrayList<StateUser> flight = MainContoller.getInstance().getFlights();
this.adapter = new ArrayAdapter<StateUser>(getActivity(), android.R.layout.simple_list_item_1, flight);
setListAdapter(this.adapter);
super.onActivityCreated(saveInstanceState);
}
public void refreshList(){
this.adapter.notifyDataSetChanged();
}
}
Top Fragment
package com.example.dfoley.write_to_file;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import.android.util.Log;
import android.view.View;
import.android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class topFragment extends Fragment{
private FlightSearcher searcher;
EditText text1;
public interface FlightSearcher {
public void refreshFlightList();
}
#Override
public void onAttach(Activity activity) {
searcher = (FlightSearcher) activity;
super.onAttach(activity);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
setupListeners();
super.onActivityCreated(savedInstanceState);
}
public void setupListeners() {
Button addUser = (Button)getActivity().findViewById(R.id.button);
addUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
writeToFile();
searcher.refreshFlightList();
}
});
}
private void writeToFile() {
text1=(EditText)getActivity().findViewById(R.id.editText);
String AddUsers = text1.getText().toString();
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(getActivity().openFileOutput("UserList", Context.MODE_PRIVATE));
outputStreamWriter.write(AddUsers);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
}
Main Activity
package com.example.dfoley.write_to_file;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity implements topFragment.FlightSearcher{
public void refreshFlightList() {
FragmentManager mgr = getFragmentManager();
bottomFragment bottomFragmentRef =(bottomFragment) mgr.findFragmentById(R.id.bottom_fragment);
bottomFragmentRef.refreshList();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activiy_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.dfoley.write_to_file.topFragment"
android:id="#+id/top_fragment"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:layout="#layout/topfragment" />
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.dfoley.write_to_file.bottomFragment"
android:id="#+id/bottom_fragment"
android:layout_weight="1"
android:layout_below="#+id/top_fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:layout="#layout/bottomfragment" />

Change fragment to FrameLayout
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

For both of your fragments, you are not telling it how to create a view. I see that you are using the tools:layout tag, but according to the Tools doc, that is only a hint to the designer; it does not actually inflate that layout:
"This attribute is typically set in a tag and is used to record which layout you want to see rendered at designtime (at runtime, this will be determined by the actions of the fragment class listed by the tag)."
Thus you need to override onCreateView, inflate your view hierarchy, and then return that:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.topfragment, container, false);
}

Related

How To Open New Activity And View The Details When We Click RecyclerView Items

How to Open New Activity When We Click Recycler View Items And Show The Item Data In New Activity
MainActivity.java: This is My MainActivity java class
package com.codinginflow.firebaseui_firestoreexample;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.Toast;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class MainActivity extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("Notebook");
private NoteAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton buttonAddNote = findViewById(R.id.button_add_note);
buttonAddNote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NewNoteActivity.class));
}
});
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = notebookRef.orderBy("priority", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
.setQuery(query, Note.class)
.build();
adapter = new NoteAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.deleteItem(viewHolder.getAdapterPosition());
}
}).attachToRecyclerView(recyclerView);
adapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
#Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Note note = documentSnapshot.toObject(Note.class);
String id = documentSnapshot.getId();
String path = documentSnapshot.getReference().getPath();
Toast.makeText(MainActivity.this,
"Position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
activity_main.xml: This Is My Activity Main Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/button_add_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="#drawable/ic_add" />
</android.support.design.widget.CoordinatorLayout>
note_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:cardBackgroundColor="#FFFFE8">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="#+id/text_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="#id/text_view_priority"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:id="#+id/text_view_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="1"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:id="#+id/text_view_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/text_view_title"
android:text="Description" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Note.java:
package com.codinginflow.firebaseui_firestoreexample;
public class Note {
private String title;
private String description;
private int priority;
public Note() {
//empty constructor needed
}
public Note(String title, String description, int priority) {
this.title = title;
this.description = description;
this.priority = priority;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public int getPriority() {
return priority;
}
}
NoteAdapter.java:
package com.codinginflow.firebaseui_firestoreexample;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.DocumentSnapshot;
public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> {
private OnItemClickListener listener;
public NoteAdapter(#NonNull FirestoreRecyclerOptions<Note> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull NoteHolder holder, int position, #NonNull Note model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
holder.textViewPriority.setText(String.valueOf(model.getPriority()));
}
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,
parent, false);
return new NoteHolder(v);
}
public void deleteItem(int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewDescription;
TextView textViewPriority;
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null) {
listener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
}
public interface OnItemClickListener {
void onItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
}
ActivityDetail.java: Here Is My Second Activity Detail.java And I want to Open Full Details Of An Item In This Activity
package com.codinginflow.firebaseui_firestoreexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ActivityDeatail extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
}
}
activiy_detail.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityDetail">
<TextView
android:id="#+id/D_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.084" />
<TextView
android:id="#+id/D_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/D_title"
app:layout_constraintVertical_bias="0.002" />
<TextView
android:id="#+id/D_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
If you want to open detailActivity bu clicking on each item of your recyclerView you can use of Intent to approach that. Something like this in your itemView.setOnClickListener
Intent intent = new Intent(view.getContext() , DetailActivity.class);
intent.putExtra("Your_key" ,
yourList.get(view.getVerticalScrollbarPosition()))
view.getContext().startActivity(intent);
And in your DetailActivity get your data like this:
YourModel model = getIntent().getSerializableExtra("Your_key");
Do not forget to Serialized your Model.

How to call a class from another activity?

I want to call MainActivity class to another activity. This is my code for the MainActivity.java:
package com.blinkedup.geolocationchat;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
LocationManager locationManager;
MyLocationListener locationListener = new MyLocationListener();
Criteria criteria;
String bestProvider;
String listOfBestProviders;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
criteria = new Criteria();
textView = (TextView) findViewById(R.id.textView1);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
bestProvider = locationManager.getBestProvider(criteria, true);
Toast.makeText(getApplicationContext(), bestProvider, 3).show();
}
protected void onPause(){
super.onPause();
locationManager.removeUpdates(locationListener);
}
private class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
textView.setText("Latitude: " + location.getLatitude() +
"Longitude: " + location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
In another activity I wanted to call the lat and long of the above code but don't want to rewrite the code to the activity. I just want to call it and display the result in another activity. please help. thanks
make your variable static and define it before onCreate() method use that variable in another activity by call like this.
YourMainActivity.yourstaticvariable

Eclipse doesn't recognize import

I've been trying to import a very simple project I've started on a differnt laptop but for some reson the project now is full of errors.
Eclipse does not recognize imports being made and thus refuse to recognize simple classes as ActionBarActivity and methods like onCreate.
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Button bEnter=(Button) getActivity().findViewById(R.id.bEnter);
Button bSignup=(Button) getActivity().findViewById(R.id.bSignup);
bEnter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String toastText="It's Working!!!";
int toastDuration=Toast.LENGTH_SHORT;
Toast toast=Toast.makeText(getActivity(), toastText, toastDuration);
toast.show();
}
});
}
}
}
What am I missing here??
What errors are you getting ?
Trick1:
If the appcompat_v7 project(library) is causing the problem, remove it from the project properties, then perform the following steps.
1) Right click on the main.
2) Hover over Android Tools.
3) Click 'Add Support Library'.
It will download the required library and the clean the project. It should work.
if doesn't work, let me know.
Trick2: Update to the latest Revisions ( Tools and SDKs using SDK manager)
Happy Coding.

Passing User input from mainactivity to a second activity in eclipse

I am bran new to this but I have successfully created a functional button.
I am attempting to pass user input (editText1 on activity_main) to Recipe (my second activity) The first code is my functional button. What am I doing wrong in the second round of code in my attempt at passing information to be displayed on the second activity layout?
package com.example.andrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Recipe.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Second round of code: My attempt at passing information. First activity
package com.example.andrew;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit Text et = (Edit Text) findViewById(R.id.editText1);
String theText = et.getText().toString();
Intent i = new Intent(this, Recipe.class);//Recipe is my second class
i.putExtra("text_label", theText);//what is "text_label"? Where should it be?
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Recipe.class));
}
});
}
}
And in my second activity I have:
public class Recipe extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_layout);
Intent i = getIntent();
uriStringi = i.getStringExtra("text_label");//is text_label on actv. 1?
startActivity(i);
}
You are adding data in the intent but while starting the activity you are not using it. Instead creating new intent.
Solution:
In your current Activity, create a new Intent:
Intent i = new Intent(this, Recipe.class);
i.putExtra("text_label", theText);
startActivity(i); // Write this in onClick()
Then in the Recipe, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("text_label");
}

How to listen for WindowEvent.WINDOW_SHOWN in the nodes of the scene graph?

It seems WindowEvent.WINDOW_SHOWN never gets dispatched on any of the nodes in the scene graph, nor is there anyway (that I could find) to know when a node is visible/rendered/shown. For example:
TestLauncher.java
package com.example.javafx.event;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TestLauncher extends Application
{
public static void main(String[] args)
{
Application.launch(TestLauncher.class, args);
}
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(TestController.class.getResource("TestView.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
TestController.java
package com.example.javafx.event;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.TextField;
import javafx.stage.WindowEvent;
public class TestController implements Initializable
{
#FXML private Parent root;
#FXML private TextField serverAddressInput;
#FXML private TextField usernameInput;
#Override
public void initialize(URL url, ResourceBundle rb)
{
serverAddressInput.setText("127.0.0.1");
//won't work because stage isn't visible yet
trySetFocusOnUsernameInput1();
//apparently Stage never passes on any WindowEvents to the children...
trySetFocusOnUsernameInput2();
}
private void trySetFocusOnUsernameInput1()
{
usernameInput.requestFocus();
}
private void trySetFocusOnUsernameInput2()
{
root.addEventFilter(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent window)
{
usernameInput.requestFocus();
}
});
root.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent window)
{
usernameInput.requestFocus();
}
});
}
public void handleWindowShownEvent()
{
usernameInput.requestFocus();
}
}
TestView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox
xmlns:fx="http://javafx.com/fxml"
fx:id="root"
fx:controller="com.example.javafx.event.TestController"
prefHeight="150"
prefWidth="200"
>
<children>
<TextField fx:id="serverAddressInput" />
<TextField fx:id="usernameInput" />
</children>
</VBox>
So, actually, how else can a node become aware of the fact that it's visible/rendered/shown?
I guess one of the possible solutions is to add the following method to TestController.java
public void handleWindowShownEvent()
{
usernameInput.requestFocus();
}
and then change the start method in TestLauncher to the following:
#Override
public void start(Stage stage) throws Exception
{
FXMLLoader loader = new FXMLLoader();
Parent root = (Parent)loader.load(TestController.class.getResourceAsStream("TestView.fxml"));
final TestController controller = (TestController)loader.getController();
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent window)
{
controller.handleWindowShownEvent();
}
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
I would really welcome other solutions as this one seems too clunky...
Another solution that admittedly isn't very sexy but decouples the node from the application:
root.sceneProperty().addListener(new ChangeListener<Scene>() {
#Override
public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene newValue) {
newValue.windowProperty().addListener(new ChangeListener<Window>() {
#Override
public void changed(ObservableValue<? extends Window> observable, Window oldValue, Window newValue) {
newValue.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
usernameInput.requestFocus();
}
});
}
});
}
});
Made more sense in my case.