javafx tableview auto scroll to the last - javafx-2

I want to show the last item added in the table view when appears a scroll bar. Is there any way for the same and it will be grateful if can.
I have checked scroll pane with horizontal and vertical setValue() property. Is there any similar way for the table view scroll bar?

Check the TableView.scrollTo() out.

Here is a full generic solution:
public static <S> void addAutoScroll(final TableView<S> view) {
if (view == null) {
throw new NullPointerException();
}
view.getItems().addListener((ListChangeListener<S>) (c -> {
c.next();
final int size = view.getItems().size();
if (size > 0) {
view.scrollTo(size - 1);
}
}));
}

Related

How to Scroll pdfView automatically with button click or volume buttons

I'm using barteksc pdf viewer library to load pdf in my application.
pdfView = findViewById(R.id.pdfView);
pdfView.fromAsset(getResources().getString(R.string.pdfname))
.enableDoubletap(true)
.enableSwipe(true)
.defaultPage(pageNumber)
.onPageChange(mainreading.this)
.pageFitPolicy(FitPolicy.WIDTH)
.pageFling(true)
.linkHandler(null)
.enableAnnotationRendering(true)
.swipeHorizontal(true)
.scrollHandle(new DefaultScrollHandlenew(mainreading.this))
.enableAntialiasing(true)
.load();
}
I want pdf to start scroll automatically when user click the button of volume up and down buttons to start stop. I tried with below code while wrapping it in the handler with handler.performClick(); but it shows blank screen while scrolling up and down.
scrollbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pdfView.scrollTo(0, pdfView.getScrollY() + 24);
}
});
Example :
https://play.google.com/store/apps/details?id=com.emptysheet.pdfreader_autoscroll&hl=en&gl=US
I want to make as like this. Can anyone help please.
Also tried with this. But it shows blank page after some scrolls.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
pdfView.scrollTo(0, pdfView.getScrollY() -24);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
pdfView.scrollTo(0, pdfView.getScrollY() + 24);
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
You can simply use this PDF viewer from github.
It's based on the same 'barsteksc' pdf viewer with the feature to jump to any pages.
It's MagicalPdfViewer and you can use 'jumpTo(pageNo)' method to simply jump to the specific page. It also gives you the option to animate to the specific page with the same method, just pass 'true' as the 2nd parameter.
Moreover, if you pass the values like '-1' and 'bigger than pageNo', It will automatically scroll to the 0 & last page respectively.
Give it a try & let me know if you got what you wanted.

Scroll after view becomes visible

I am trying to scroll to the bottom of my scrollView after a view becomes visible with button click. The problem is the scrollTo function is applied before the view is actually visible. I know this because when the button is pressed twice, it scrolls to the bottom on the second click.
So, is there a way to scroll after the view becomes visible?
button.setOnClickListener(v -> {
constraintLayout.setVisibility(View.VISIBLE);
scrollView.smoothScrollTo(0, constraintLayout.getBottom());
}
button.setOnClickListener(v -> {
constraintLayout.setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(() -> {
scrollView.smoothScrollTo(0, constraintLayout.getBottom());
}, 100);
}
I just figured out this works, but I was hoping to not use a delay.
Another option is to use a listener.
ViewTreeObserver.OnPreDrawListener viewVisibilityChanged = new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if (my_view.getVisibility() == View.VISIBLE) {
scroll_view.smoothScrollTo(0, scroll_view.getHeight());
}
return true;
}
};
You can add it to your view this way :
my_view.getViewTreeObserver().addOnPreDrawListener(viewVisibilityChanged);

jTabbedPane set active tab text properties

I have a jTabbedPane with multiple tabs in it. I am trying to make the title text of the selected/active tab bold. Is there a simple way to do this?
JTabbedPane pane = new JTabbedPane();
pane.addChangeListener(new ChangeListener(){
#Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
// Set all tabs to PLAIN font
for(int i = 0; i < source.getTabCount(); i++) {
Component c = source.getTabComponentAt(i);
c.setFont(c.getFont().deriveFont(Font.PLAIN));
}
Component selectedComp = source.getTabComponentAt(source.getSelectedIndex());
// Set selected component to BOLD
selectedComp.setFont(selectedComp.getFont().deriveFont(Font.BOLD));
}
});
Try this, i wrote it quickly, maybe you need to do some adjustments for the initial tab, don't know for sure.
Also not so sure if you need JTabbedPane.getTabComponentAt(int idx) or JTabbedPane.getComponentAt(int idx) although i suppose first version is correct.
I know that this question was asked a long time ago, but I recently wanted to do this as well. I found the answer here. The solution was one of two things:
Use tabbedPane.setTitleAt(currSelextedIdx, "<html><b>My tab title</b></html>");
Create your own UI implementation for the tabbed pane
I personally used the first option and set all the other tabs back to the regular tab title on a change. I also made the initial tab bold after initializing all the tabs (this can be done upon initialization).
The simplest solution I've found until now. In a subclass of JTabbedPane, override two methods as below. When the tab is added, the raw title is persisted as a client property of the tab component. When the selected tab changes, the current tab title is restored to the raw title and the next tab title is set to bold with HTML.
public class MyTabbedPane extends JTabbedPane {
...
#Override
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
((JComponent)component).putClientProperty("title", title);
super.insertTab(title, icon, component, tip, index);
}
#Override
public void setSelectedIndex(int index) {
int currentIndex = getSelectedIndex();
if (currentIndex >= 0) {
JComponent previous = (JComponent) getComponentAt(currentIndex);
String title = (String) previous.getClientProperty("title");
setTitleAt(currentIndex, title);
}
super.setSelectedIndex(index);
JComponent current = getSelectedComponent();
String title = (String) current.getClientProperty("title");
setTitleAt(index, "<html><b>" + title + "</b></html>");
}
}

JavaFX scroll events handling for TableView

I have a problem with my JavaFX project. There is a moment I can't understand. As far as I understand the following code should be able to handle all scrolling events of a table, which is an instance of TableView
table.setOnScroll(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent scrollEvent) {
System.out.println("Hello!");
int i = 0;
int length = table.getItems().size();
for(Node n: table.lookupAll("TableRow")) {
if (n instanceof TableRow) {
TableRow row = (TableRow) n;
if(table.getItems().get(i).getType() == "fwfx") {
row.setStyle("-fx-background-color: forestgreen;");
}
i++;
}
if(i == length) {
break;
}
}
}
}
);
Whenever I launch the application it highlights row correctly only for visible rows. I found it out because
table.lookupAll("TableRow")
returns the set of only 17 nodes for me. although
table.getItems().size()
shows the correct number of rows. If I scroll down the table I see unapproipriate rows highlighted. I'm lost a bit.
So the question is how do I correctly handle the scroll events for my table? I need to process all rows of the table, not only visible.
So finally I found the way to handle scrolling events and I would like to share my experience. Using the Scenic View I found that TableView setOnScroll event is fired only when you scroll the mouse wheel with cursor over the column headers. But to be able to handle ScrollEvent when the cursor is above table data (what I needed in my example) one needs to use the EventFilter to be sure. For example, the colde below will handle all scrolling events of TableView's instance
table.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent scrollEvent) {
System.out.println("Scrolled.");
}
});
Scenic View also gave me a hint about what TableView consists of after
stage.show()
has worked.
Althougth I still have incorrectly highlighted rows)...
Listen to scroll bar value changes
for (Node node : dataTable.lookupAll(".scroll-bar"))
{
if (node instanceof ScrollBar && ((ScrollBar) node).getOrientation().equals(Orientation.VERTICAL))
{
((ScrollBar) node).valueProperty().addListener(formatBUIScrollChangeEventHandler);
}
}
This will be triggered in any form of scrolling on the table.
Have been spending hours then finally come out with this idea when solving my own situation.
The question is old, but you haven't posted/accepted an answer yet.
So, this schould help: tableView.refresh(). It's available in JavaFX since Java 8u60.
You have to call it inside your ScrollEvent. Also perhaps by sorting and dragging ScrollBar (see my answer here).

ListView clipping

i working little bit with the ListView from JavaFx2. I´m running into one issue.
Is it possible to turn off the clipping of the ListCell/ListView?
I add an ImageView that has to be wider than the ListView and JavaFx2 shows automatically a scrollbar.
This my code snipped how i add the ImageView to my List:
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> blub = new ListCell<String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
StackPane p = new StackPane();
Label label = new Label(item);
p.getChildren().addAll(img, label);
setGraphic(p);
p.setAlignment(Pos.CENTER_LEFT);
}
}
};
blub.setStyle("-fx-background-color:transparent");
return blub;
}
});
Big thanks!
I don't think it's possible.
Maybe try to play with the Skin of the ListView. It seems that the scroll bar are managed in this class. It do not use a scroll pane.
Another solution could be replacing the ListView by a VBox in a ScrollPane.
Finally, you could try to modify img (by the way, where it come from, and what Class is it ?) to only show what you need.
Anyway, I'm interested by the solution you will use.

Resources