How to set ImageView and TextView in OnPostMethod of AsyncClass - android-layout

I want to set image and text in imageview and textview in the OnPostMethod of AsyncClass, like I am doing some network operations in "do in background" method, and on a particular response I just want to populate imageviews and textviews with particular images and texts.
Can someone tell me how to set image in OnPostMethod. I wonder if someone can tell me the mistake I am making in the following code
public class testing_async extends Activity {
private ProgressDialog progressDialog;
String s;
ImageView im;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing_async);
new GetCategoryList().execute(s);
im = (ImageView) findViewById(R.id.im1);
}
//
// THE AsyncTask Class for GetCategoryList....
//
class GetCategoryList extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// showDialog(progress_bar_type);
progressDialog = ProgressDialog.show(testing_async.this, "",
"Loading...");
}
#Override
protected String doInBackground(String... f_url) {
/*
* LinearLayout layout = new LinearLayout(testing_async.this);
* layout = new LinearLayout(testing_async.this);
* layout.setLayoutParams(new ViewGroup.LayoutParams(
* ViewGroup.LayoutParams.FILL_PARENT,
* ViewGroup.LayoutParams.FILL_PARENT));
*
* RelativeLayout inner_layout = new
* RelativeLayout(testing_async.this); inner_layout = new
* RelativeLayout(testing_async.this); layout.setLayoutParams(new
* ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT,
* ViewGroup.LayoutParams.FILL_PARENT));
*
* ImageView im = new ImageView(testing_async.this);
* im.setImageResource(R.drawable.logo_background);
* RelativeLayout.LayoutParams layoutParams90 = new
* RelativeLayout.LayoutParams( 225, 250); layoutParams90.leftMargin
* = 45; layoutParams90.topMargin = 45;
* im.setLayoutParams(layoutParams90); inner_layout.addView(im);
* layout.addView(inner_layout);
*/
return null;
}
protected void onProgressUpdate(String... progress) {
}
#Override
protected void onPostExecute(String file_url) {
im.setBackgroundResource(R.drawable.back);
progressDialog.dismiss();
}
}
}

Context mContext;
ImageView im;
public GetCategoryList(Context context, ImageView myIm) {
this.mContext = context;
this.im = myIml;
}
Add the custom constructor above to your async task. This way you have a context and a reference to your imageView. It will be needed in order to get the resource.
Second, all the commented code should be placed inside "onCreate".
Finally, you can assign a drawable to your imageView using
im.setBackgroundResource(mContext.getResources().getDrawable(R.drawable.bBack));
Hope this helps.
P.S. NEVER change layout in "doInBackground". Your app will crash.

if you are setting image
then you have to give im.setBackgroundResource(R.drawable.bBack);
you are setting id not image in your code.

Related

Recycler Adapter inserting Audio files

Am just starting to learn Adroind and am coming up with an Audio player app and am stuck, I dont know how am going to insert the audio files in my Recycler Adapter for it to play. can someone help me out please?
public class AudioRecyclerViewAdapter extends RecyclerView.Adapter<AudioRecyclerViewAdapter.ViewHolder> {
// Global variables
ArrayList audios;
MainActivity mainActivity;
public AudioRecyclerViewAdapter(
// Local variables
MainActivity mainActivity, ArrayList<Audio> audios
) {
this.audios = audios;
this.mainActivity = mainActivity;
}
#NonNull
#Override
public AudioRecyclerViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// Link to audio item layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_item, parent, false);
return new ViewHolder(v);
}
/**
* Change item layout views
* #param viewHolder
* #param position
*/
#Override
public void onBindViewHolder(#NonNull AudioRecyclerViewAdapter.ViewHolder viewHolder, int position) {
// Get audio
Audio audio = audios.get(position);
// Update views
viewHolder.titleTextView.setText(audio.getTitle());
viewHolder.artworkHolder.setImageResource(audio.getImage());
viewHolder.audioLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Check if clicked song is active song.
if(audio.equals(mainActivity.activeAudio)) {
if(audio.isPlaying) {
viewHolder.playIcon.setImageResource(R.drawable.ic_pause_circle_outline_24);
mainActivity.pauseAudio(mainActivity.activeAudio);
} else {
viewHolder.playIcon.setImageResource(R.drawable.ic_play_circle_outline_24);
mainActivity.playAudio(mainActivity.activeAudio);
}
} else {
// Pause previous song
mainActivity.pauseAudio(audio);
// Update to current song
viewHolder.playIcon.setImageResource(R.drawable.ic_pause_circle_outline_24);
mainActivity.setActiveAudio(audio);
}
}
});
}
#Override
public int getItemCount() {
return audios.size();
}
/**
* Access item layout views
*/
public class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout audioLinearLayout;
TextView titleTextView;
ImageView playIcon;
ImageView artworkHolder;
RecyclerView audioRecyclerView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
audioLinearLayout = itemView.findViewById(R.id.playing_audio_linear_layout);
titleTextView = itemView.findViewById(R.id.playing_title_text_view);
playIcon = itemView.findViewById(R.id.playing_play_pause_image_button);
artworkHolder = itemView.findViewById(R.id.playing_audio_image_view);
audioRecyclerView=itemView.findViewById(R.id.audio_recycler_view);
}
}
}

ImageButton to show image of ImageView onClick

I want an ImageButton to change its image when clicked on it. But the image that it changes to has to be the image from an ImageView. Is this possible?
EDIT:
in the first activity where you chose your character to play with, there´s an ImageView. The Image of it is being changed to the Image of the chosen character via onClickListener that is set for an ImageButton:
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
View.OnClickListener onClickButton1 = view -> {
imgV3.setImageDrawable(
((ImageButton) view).getDrawable()
);
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.plOneChar1);
imageButton1.setOnClickListener(onClickButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.plOneChar2);
imageButton2.setOnClickListener(onClickButton);
...
Until here everything's working just fine.
In the 2nd activity, where the two players play against each other, I need the current Image of ImageView3 (and imageView4 for the 2nd character, didn't post it above because it's the same code). this current image is what i want to be shown on several ImageButtons when clicked on them.
This is how i tried to solve it:
public class PlayActivity extends AppCompatActivity {
String random;
String pl1;
String pl2;
List<String> list;
Random rand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
String pl1 = "Player One";
String pl2 = "Player Two";
List<String> list = new ArrayList<String>();
list.add(pl1);
list.add(pl2);
Random rand = new Random();
String random = list.get(rand.nextInt(list.size()));
Toast.makeText(this, "It´s your turn: " + random, Toast.LENGTH_SHORT).show();
}
public void onClickImgBtn (View view) {
ImageButton imgBtn = (ImageButton) view;
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
ImageView imgV4 = (ImageView) findViewById(R.id.imageView4);
if((random == pl1) && (imgBtn.getDrawable() == null)){
imgBtn.setImageDrawable(imgV3.getDrawable());
random = list.get(rand.nextInt());
} else if((random == pl2) && (imgBtn.getDrawable() == null)) {
imgBtn.setImageDrawable(imgV4.getDrawable());
random = list.get(rand.nextInt());
} else {
Toast.makeText(this,"Chose other section!", Toast.LENGTH_SHORT).show();
}
}
}
I hope this will help to find what I'm doing wrong. Let me know if more info needed.
EDIT SOLUTION:
I found a solution. I used getTag() & setTag() and global variables.
At first i created a new class MyApplication which extends Application to declare 3 global variables:
public class MyApplication extends Application {
Integer defaultTag = R.drawable.white;
Integer resImgV3;
public Integer getResImgV3() {
return resImgV3;
}
public void setResImgV3(int resImgV3) {
this.resImgV3 = resImgV3;
}
Integer resImgV4;
public Integer getResImgV4() {
return resImgV4;
}
public void setResImgV4(int resImgV4) {
this.resImgV4 = resImgV4;
}
}
Then i set an onClickListener to all ImageButtons that changes the Image of the ImageViews (imgV3 and imgV4) by getting the Tags of the ImageButtons which are set to the id of the drawable in each ImageButton and it also sets the value of the global variables resImgV3 and resImgV4 to the id of the now set drawables of imgV3 and imgV4:
ImageView imgV3 = (ImageView) findViewById(R.id.imageView3);
imgV3.setTag(R.drawable.kid1);
imgV3.setImageResource(R.drawable.kid1);
((MyApplication)this.getApplication()).setResImgV3((Integer) imgV3.getTag());
View.OnClickListener onClickButton = view -> {
imgV3.setTag((Integer) view.getTag());
imgV3.setImageResource((Integer) view.getTag());
((MyApplication) this.getApplication()).setResImgV3((Integer) imgV3.getTag());
};
ImageView imgV4 = (ImageView) findViewById(R.id.imageView4);
imgV4.setTag(R.drawable.kid2);
imgV4.setImageResource(R.drawable.kid2);
((MyApplication)this.getApplication()).setResImgV4((Integer) imgV4.getTag());
View.OnClickListener onClickButton1 = view -> {
imgV4.setTag((Integer) view.getTag());
imgV4.setImageResource((Integer) view.getTag());
((MyApplication) this.getApplication()).setResImgV4((Integer) imgV4.getTag());
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.plOneChar1);
imageButton1.setImageResource(R.drawable.kid1);
imageButton1.setTag(R.drawable.kid1);
imageButton1.setOnClickListener(onClickButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.plOneChar2);
imageButton2.setImageResource(R.drawable.kid2);
imageButton2.setTag(R.drawable.kid2);
imageButton2.setOnClickListener(onClickButton); //and 6 more ImageButtons
And finally in the 2nd activity, where i needed the images of imageView3 and imageView4, i can easily get the resId of each drawable by just accessing the MyApplication-class like following:
ImageView imgV5 = (ImageView) findViewById(R.id.imageView5);
imgV5.setImageResource(((MyApplication) this.getApplication()).getResImgV3());
imgV5.setTag(((MyApplication) this.getApplication()).getResImgV3());
ImageView imgV6 = (ImageView) findViewById(R.id.imageView6);
imgV6.setImageResource(((MyApplication) this.getApplication()).getResImgV4());
imgV6.setTag(((MyApplication) this.getApplication()).getResImgV4());
I hope this will help other ones with the same problem in the future.
Try this:
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
}
}

"java.lang.IllegalArgumentException: view must not be null" Program Closes when navigated

The app closes whenever I navigate to ProfileActivity.
FATAL EXCEPTION: main Process: hfad.com.hallofmemesprototype, PID:
19092 java.lang.RuntimeException: Unable to start activity
ComponentInfo{hfad.com.hallofmemesprototype/hfad.com.hallofmemesprototype.Profile.ProfileActivity}:
java.lang.IllegalArgumentException: view must not be null
Here's the "ProfileActivity" code.
public class ProfileActivity extends AppCompatActivity {
private static final int ACTIVITY_NUM = 3;
private Context mContext = ProfileActivity.this;
private ProgressBar mProgressBar;
private ImageView profilePhoto;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setupBottomNavigationView();
setupToolbar();
setupActivityWidgets();
setProfileImage();
}
private void setProfileImage(){
String imgURL = "www.androidzone.org/wp-content/uploads/2013/02/android-musical2.jpg";
UniversalImageLoader.setImage( imgURL, profilePhoto, mProgressBar, "https://");
}
private void setupActivityWidgets(){
mProgressBar = findViewById(R.id.profileProgressBar);
mProgressBar.setVisibility(View.GONE);
profilePhoto = findViewById(R.id.profile_photo);
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.profileToolBar);
setSupportActionBar(toolbar);
ImageView profileMenu = findViewById(R.id.profileMenu);
profileMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, AccountSettingsActivity.class);
startActivity(intent);
}
});
}
/**
* BottomNavigationView setup
*/
private void setupBottomNavigationView() {
BottomNavigationViewEx bottomNavigationViewEx = findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.bottomNavigationView(bottomNavigationViewEx);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationViewEx);
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
}
You must check that all the views that you are getting using findViewById() really exist into the activity_profile.xml layout.
One or more views doesn´t really exist, and you have a null value gettin the reference.
findViewById(R.id.profileProgressBar);
findViewById(R.id.profile_photo);
findViewById(R.id.profileToolBar);
findViewById(R.id.profileMenu);
findViewById(R.id.bottomNavViewBar);
initially you are not getting any error tryin to find the references of this views because they are really exist but in another layout but not in activity_profile.xml that you are loading via setContentView() in your Activity.
This type of exception rise at the time of the wrong variable declaration and
initialization.
Try this
replace your weighs declare and initialize val with var.
If you have to use java
final TextView helloTextView = (TextView)findViewById(R.id.text_view_id);
For Kotlin
val helloTextView = findViewById(R.id.text_view_id) as TextView
you can refer this link for more details

Set Background Image to Relative Layout using Glide in Android

How can I do this using Glide? I want to cache image to use it another time also. Thanks in advance.
You can load an image in a RelativeLayout like this. I'm just showing you the hard part which is setting an image to the background.
For Glide version before 4.X
Glide.with(this).load(imageViewPath).asBitmap().into(new SimpleTarget<Bitmap>(relLayoutWidth, relLayoutHeight) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Drawable drawable = new BitmapDrawable(context.getResources(), resource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
yourRelativeLayout.setBackground(drawable);
}
}
});
For caching, refer to this page: Caching and Cache Invalidation.
Update for Glide v4 onward:
GlideApp.with(this).load(R.drawable.backgroundimage).into(new SimpleTarget<Drawable>() {
#Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
yourRelativeLayout.setBackground(resource);
}
}
});
Currently, at version Glide version 4.9.0, you can set a background for Relative Layout as:-
Glide.with(MainActivity.this)
.load(IMAGE_URL)
.into(new CustomTarget<Drawable>() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onResourceReady(#NonNull Drawable resource, #Nullable Transition<? super Drawable> transition) {
mLinearLayout.setBackground(resource);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
I think the best way to achieve it's works with your own ViewTarget implementation, because this class has specific methods to be handled by Glide automatically in different scenarios.
The abstract implementation for ViewGroup (LinearLayout, RelativeLayout and so on).
public abstract class ViewGroupTarget<Z> extends ViewTarget<ViewGroup, Z> implements GlideAnimation.ViewAdapter {
public ViewGroupTarget(ViewGroup view) {
super(view);
}
/**
* Returns the current {#link android.graphics.drawable.Drawable} being displayed in the view using
* {#link android.widget.ImageView#getDrawable()}.
*/
#Override
public Drawable getCurrentDrawable() {
return view.getBackground();
}
/**
* Sets the given {#link android.graphics.drawable.Drawable} on the view using
* {#link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* #param drawable {#inheritDoc}
*/
#Override
public void setDrawable(Drawable drawable) {
view.setBackground(drawable);
}
/**
* Sets the given {#link android.graphics.drawable.Drawable} on the view using
* {#link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* #param placeholder {#inheritDoc}
*/
#Override
public void onLoadStarted(Drawable placeholder) {
view.setBackground(placeholder);
}
/**
* Sets the given {#link android.graphics.drawable.Drawable} on the view using
* {#link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* #param errorDrawable {#inheritDoc}
*/
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
view.setBackground(errorDrawable);
}
/**
* Sets the given {#link android.graphics.drawable.Drawable} on the view using
* {#link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* #param placeholder {#inheritDoc}
*/
#Override
public void onLoadCleared(Drawable placeholder) {
view.setBackground(placeholder);
}
#Override
public void onResourceReady(Z resource, GlideAnimation<? super Z> glideAnimation) {
this.setResource(resource);
}
protected abstract void setResource(Z resource);
}
The specific implementation, in this case for LinearLayout.
public class LinearLayoutTarget extends ViewGroupTarget<Bitmap> {
private Context context;
public LinearLayoutTarget(Context context, LinearLayout linearLayout) {
super(linearLayout);
this.context = context;
}
/**
* Sets the {#link android.graphics.Bitmap} on the view using
* {#link android.widget.ImageView#setImageBitmap(android.graphics.Bitmap)}.
*
* #param resource The bitmap to display.
*/
#Override
protected void setResource(Bitmap resource) {
view.setBackground(new BitmapDrawable(context.getResources(), resource));
}
}
To work with.
Glide.with(this.getApplicationContext())
.load(R.drawable.your_image)
.asBitmap()
.into(new LinearLayoutTarget(this.getApplicationContext(), (LinearLayout) yourLinearLayoutInstanceHere));
Or even more simple working without Bitmap.
The specific implementation.
public class LinearLayoutTarget extends ViewGroupTarget<Drawable> {
public LinearLayoutTarget(LinearLayout linearLayout) {
super(linearLayout);
}
/**
* Sets the {#link android.graphics.Bitmap} on the view using
* {#link android.widget.ImageView#setImageBitmap(android.graphics.Bitmap)}.
*
* #param resource The bitmap to display.
*/
#Override
protected void setResource(Drawable resource) {
view.setBackground(resource);
}
}
To work with.
Glide.with(this.getApplicationContext())
.load(R.drawable.your_image)
.into(new LinearLayoutTarget((LinearLayout) yourLinearLayoutInstanceHere));
Here's the code for Kotlin
Glide 4.9.x
Since the SimpleTarget is deprecated now we have to use CustomTarget as below
Glide.with(this).load(UCrop.getOutput(data)).into(object :
CustomTarget<Drawable>() {
override fun onLoadCleared(placeholder: Drawable?) {
TODO("Not yet implemented")
}
override fun onResourceReady(
resource: Drawable,
transition: Transition<in Drawable>?
) {
ib_pet_profile_image.background=resource
}
})
(SimpleTarget class is Deprecated So using CustomTarget class to Load image)
You can load an image in a RelativeLayout like this. I'm just showing you the hard part which is setting an image to the background.
For Glide version before 4.X (Without Deprecation)
Glide.with(this).load(ServiceGenerator.BASE_URL + url).into(new CustomTarget<Drawable>() {
#Override
public void onResourceReady(#NonNull Drawable resource, #Nullable Transition<? super Drawable> transition) {
yourRelativeLayout.setBackground(resource);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Glide.with(ctx).asBitmap().load(url).centerCrop().into(object: CustomTarget<Bitmap>(){
override fun onLoadCleared(placeholder: Drawable?) {
}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val wRatio = view.width/resource.width.toFloat()
val hRatio = view.height / resource.height.toFloat()
val minRatio = min(wRatio,hRatio)
val destBitmap = Bitmap.createScaledBitmap( resource,(resource.width*minRatio).toInt(), (resource.height*minRatio).toInt(),false)
view.background = BitmapDrawable(resources,destBitmap)
}
})
This works for me
PS: There would be an error when the bitmap is too large
java.lang.RuntimeException: Canvas: trying to draw too large bitmap.
So you'd better scale that bitmap like what i did in above code
Thanks all. All of your answers helped me out. I also find useful solutions with Glide's official documentation. I have switched over to Glide App Module in order to have common usage of it.
https://medium.com/#nuhkocaa/manage-all-your-glides-in-a-single-class-with-glidemodule-on-android-4856ee4983a1
I needed to set the background of an ImageView (so I could set the foreground to a different image).
I eventually found https://github.com/bumptech/glide/issues/938#issuecomment-176150770, but that's a bit out of date. Through experimentation I altered it to the following to work with Glide 4 (similar to some of the other answers here):
/** #see ImageViewTarget
*/
abstract class ViewBackgroundTarget<Z>(view: View) : CustomViewTarget<View?, Z>(view) {
override fun onLoadFailed(errorDrawable: Drawable?) {
}
override fun onResourceCleared(placeholder: Drawable?) {
// This line is ESSENTIAL or else there will be occasional crashes like:
// "java.lang.NullPointerException: Attempt to invoke virtual method
// 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference"
view!!.background = placeholder
}
}
/** #see BitmapImageViewTarget
*/
class BitmapViewBackgroundTarget(view: View) : ViewBackgroundTarget<Bitmap>(view) {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
view!!.background = BitmapDrawable(view.resources, resource)
}
}
/** #see DrawableImageViewTarget
*/
class DrawableViewBackgroundTarget(view: View) : ViewBackgroundTarget<Drawable>(view) {
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
view!!.background = resource
if (resource is GifDrawable) {
resource.setLoopCount(LOOP_FOREVER)
resource.start()
}
}
}
use this with
Glide.with(myImageView)
.load(imageUrl)
.into(DrawableViewBackgroundTarget(myImageView))
I assume this would work easily for a RelativeLayout or other View types as well.

JavaFx ProgressBar Bind SimpleDoubleProperty

I am using a progressbar in a tableview, where I'm using bind on his property so that when the value is changed, progressbar is started:
private SimpleDoubleProperty progressBar;
public Tabela(Double progressBar) {
this.progressBar = new SimpleDoubleProperty(progressBar);
}
public Double getProgressBar() {
return progressBar.get();
}
public DoubleProperty getProgressBarProperty() {
return progressBar;
}
public void setProgressBar(Double progressBar) {
this.progressBar.set(progressBar);
}
public void setProgressBar(SimpleDoubleProperty progressBar) {
this.progressBar = progressBar;
}
And in my Hbox am using progressbar as follows:
final ProgressBar progress = new ProgressBar();
progress.setMinWidth(324.0);
progress.progressProperty().bind(tabela.getProgressBarProperty());
So I'm using a so that after a certain time the value is changed, ie I will control the progressbar. The value is changed, but in my tableview nothing happens, but when I change the column position to another, the progressbar is running.
The same happens with "label", i changed for "text" and it work, but if the progressbar I have to use.
have a way to force the 'tableview' refresh?
You need to follow the JavaFX naming standard to get TableViews and other widgets to properly refresh when an observable object changes. For instance, you should rename getProgressBarProperty() to progressBarProperty().
See: How to update TableView Row using javaFx
There would be something wrong in this source?
class table
...
public Tabela(String nome, Double progressBar, String etapa) {
this.nome = nome;
this.progressBar = new SimpleDoubleProperty(progressBar);
this.etapa = new SimpleStringProperty(etapa);
}
....
Add new line.
private void preencheListaNomeTabelas() {
getLista().add(new Tabela("Test", 0.0, "Test Text"));
Add hbox in table.
columTabela.setCellValueFactory(new PropertyValueFactory<Tabela, String>("nome"));
columSituacao.setCellFactory(new Callback<TableColumn<Tabela, Double>, TableCell<Tabela, Double>>() {
public TableCell<Tabela, Double> call(TableColumn<Tabela, Double> p) {
final HBox box = new HBox();
box.setPrefHeight(25.0);
final ProgressBar progressBar = new ProgressBar(-1);
final Text text = new Text();
**text.textProperty().bind(..); //I would use here the**
BorderPane border = new BorderPane();
border.setTop(text);
border.setBottom(progressBar);
BorderPane.setAlignment(text, Pos.CENTER);
box.getChildren().add(border);
final TableCell cell = new TableCell<Tabela, Double>() {
#Override
protected void updateItem(Double t, boolean bln) {
super.updateItem(t, bln);
if (bln) {
setText(null);
setGraphic(null);
} else {
progressBar.setProgress(t);
progressBar.prefWidthProperty().bind(this.widthProperty());
setGraphic(box);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
};
cell.setAlignment(Pos.CENTER);
return cell;
}
});
columSituacao.setCellValueFactory(new PropertyValueFactory<Tabela, Double>("progress"));
columSituacao.setText("Progresso");
tableView.getItems().addAll(lista);
tableView.getSelectionModel().selectFirst();
task
Task t = new Task() {
#Override
protected Object call() throws Exception {
Thread.sleep(10000);
getLista().get(0).setEtapa("Lendo PostgreSQL");
getLista().get(0).setProgressBar(-1.0);
return null;
}
};
new Thread(t).start();

Resources