JavaFX: textArea.setScrollTop(Double.MAX_VALUE) not always works - java

This is the piece of my code.
textArea.setText(someNewText)
textArea.positionCaret(textArea.getText().length());
textArea.setEditable(true);
textArea.setScrollTop(Double.MAX_VALUE);
I use textArea.setScrollTop(Double.MAX_VALUE) to scroll textarea to the bottom (solution I found in internet). It works, but not always. I've noted that it can not work only when vertical scroll bar is not visible before calling this code and visible after the code was executed. When vertical scroll bar is visible before calling this code then scrolling to the bottom works always. How to fix it? Maybe I should make vertical scroll bar always visible? If yes, then how - I didn't find the solution.
EDIT:
This is the sample code:
public class JavaFxApp1 extends Application{
private TextArea textArea;
#Override
public void start(Stage stage) throws Exception {
Button button=new Button("Press here");
textArea=new TextArea();
VBox vbox = new VBox(button,textArea);
button.setOnAction((event)->{
textArea.appendText("###This is a very long string:some text some text some text some text some"
+ " text some text some text some text some text some text"
+ " text some text some text some text some text some text"
+ " text some text some text some text some text some text .\n");
textArea.selectEnd();
textArea.deselect();
textArea.setScrollTop(Double.MAX_VALUE);
});
textArea.setEditable(true);
textArea.setWrapText(true);
textArea.setStyle("-fx-font-size:14px;-fx-focus-color: transparent;-fx-font-family: monospace;");
Scene scene=new Scene(vbox);
stage.setTitle("SomeTitle");
stage.setScene(scene);
stage.setMinHeight(400);
stage.setMinWidth(800);
stage.show();
}
}
This is the result when I pressed button 4 times:
As you see it didn't scroll to the bottom. After I press button again (the fifth time) I have the following result:
Now, as you see it was scrolled to the bottom.
I tried to add:
ScrollPane scrollPane = (ScrollPane) textArea.lookup(".scroll-pane");
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
to make scrollbar visible always - it is visible but after 4 times anyway doesn't scroll to the bottom.
How to fix it?

if you want to scroll to the Top your code will be this way
ta.selectHome();
ta.deselect();
if you want to scroll to the Bottom your code will be this way
ta.selectEnd();
ta.deselect();
now let the TextArea worry about its visible areas
edit
maybe i didnt get your requirement, cause honestly this is the first time i have seen that method so to give an answer based on your hypothesis
Maybe I should make vertical scroll bar always visible? If yes, then how
well thats easy TextArea uses ScrollPane, when there is a visible scroll
this line TextArea.getChildrenUnmodifiable().size(); will check out to be 1, so you fish it out when you create your TextArea
TextArea.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>() {
#Override
public void onChanged(javafx.collections.ListChangeListener.
Change<? extends Node> c) {
while(c.next()){
if(c.wasAdded()){
for(Node n : TextArea.getChildrenUnmodifiable()){
if(n.getClass().isAssignableFrom(ScrollPane.class)){
//just trying to be cool here ^^
ScrollPane sp = (ScrollPane) n;
sp.setVbarPolicy(ScrollBarPolicy.ALWAYS);
}
}
}
}
}
});
actually you can keep a reference to the ScrollPane and anytime you want to scroll to the bottom ScrollPane.setVvalue(1.0); to the top ScrollPane.setVvalue(0.0); etc etc.
you can get the value with ScrollPane.getVvalue().

This is a bug. See the following link https://bugs.openjdk.java.net/browse/JDK-8189732

Related

gxt text area programatically scroll down not working

I'm trying to set an text to text area and to scroll it to bottom, and the following code seems to not work..
TextArea logTextArea = new TextArea();
logTextArea.setValue(result , true, true);
final int scrollHeight = logTextArea.getElement().getFirstChildElement().getScrollHeight();
logTextArea.getElement().getFirstChildElement().setScrollTop(scrollHeight);
Tried also another examples with setCursorPos(), also not working. What is wrong with this example?

Make scrollpane always on top

So, in my program, at the bottom, I've got a ScrollPane which has a TextFlow inside it to display a information in when the user clicks on items in a list. The ScrollPane was rather small, so I added an expand button which makes it larger so you can see more. But now, if there is a button or anything within the area that the expanded ScrollPane takes up, it is always on top of the pane and makes it hard to see anything. Is there a way I could force the ScrollPane to be on top and cover things? Attempting to change the z-index did nothing.
Should I just set anything within that area to be not visible while expanded instead?
This is what I'm doing to expand the ScrollPane:
Image expand_img = new Image(getClass().getResourceAsStream("expand.png"));
Button expand = new Button();
expand.setGraphic(new ImageView(expand_img));
expand.setLayoutX(630);
expand.setLayoutY(415);
expand.setStyle("-fx-background-color: transparent");
expand.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
++clicked;
if (clicked % 2 == 0){
scroll_desc.setTranslateY(-100);
scroll_desc.setPrefHeight(208);
expand.setLayoutY(315);
}else if (clicked % 2 == 1){
scroll_desc.setTranslateY(0);
scroll_desc.setPrefHeight(108);
expand.setLayoutY(415);
}
}
});
I just want it so that when the ScrollPane is larger, anything inside the new area is covered up by the ScrollPane rather than being overlayed on top and accessible, thus obscuring content of the descriptions and the like displayed.

How to align text position for two different components?

I am trying to align the position of text within a JTextArea and a JButton, but with everything I tried, either nothing happens, or the alignment is still slightly off.
Here is what is looks like:
(You can see with the highlighted option that the JButton (center) is slightly lower than the two JTextAreas on either side.)
Here is some code:
categoryFile[i][j] = tempButton;
categoryFile[i][j].setBackground(Color.white);
categoryFile[i][j].setForeground(Color.black);
categoryFile[i][j].setOpaque(true);
categoryFile[i][j].setFocusable(false);
categoryFile[i][j].setBorderPainted(false);;
categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP);
categoryFile[i][j].setPreferredSize(new Dimension(500,10));
categoryFile[i][j].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
openPDFWithOptions(filePath,fileName);
}
});
JPanel listRow = new JPanel();
listRow.setBackground(Color.white);
listRow.setLayout(new BorderLayout());
listRow.setPreferredSize(new Dimension(800, 40));
JTextArea category = new JTextArea(fileElements[0]);
category.setEditable(false);
JTextArea parent = new JTextArea(fileElements[1]);
parent.setEditable(false);
listRow.add(parent,BorderLayout.WEST);
listRow.add(categoryFile[i][j],BorderLayout.CENTER);
listRow.add(category,BorderLayout.EAST);
categoryLists[i].add(listRow,c);
Right now I am using categoryFile[i][j].setVerticalAlignment(SwingConstants.TOP) to change the position of the JButton, which ALMOST works. I've also tried changing the vertical alignment of the JTextAreas, but nothing changed.
How can I align the text within these components?
Quickest way to fix this would probably be to just add some padding on the 1st and third columns to set all the text to the same height. See Jpanel Padding

No vertical scroll in browser

I'm developing a Vaadin application and am having extreme difficulty getting some aspects of the layout as I want. The major problem right now is that I can't seem to get a vertical scroll in my layout no matter how big the size of the content is or how small the browser window is..
I have read up on the subject, I know that the hLayout and the vLayout doesn't support scrollbars but the Panel do. I've tried in many different combinations to make it work but I've only managed to get a horizontal scrollbar to generate but never a vertical one.
Another problem is that I'm building the application inside an existing "template" provided by the company. This template contains a footer containing some copyright information. This footer doesn't seem to occupy any space in the browser window with regards to the content I'm adding, which causes when viewing on smaller screens the horizontal scrollbar to appear "underneath" the footer, non-accessible... I'll provide some of the code of how it looks now.
public class InventorySimCardTable extends M2MViewBase { //M2MViewBase extends VerticalLayout
private final SPanel mainContent = Cf.panel("");
private final SPanel tabPanel = Cf.panel("");
private final SVerticalLayout tabcontent = Cf.vLayout();
protected InventoryFilterPanel inventoryFilterPanel;
#Override
protected void initComponent() {
setSizeFull();
tabPanel.setSizeFull();
tabPanel.getContent().setSizeUndefined();
Table simCardTable = new Table();
simCardTable.setWidth("1898px");
simCardTable.setPageLength(15);
tableContainer.setSizeUndefined();
tableContainer.addComponent(simCardTable);
mainContent.setWidth("99%");
mainContent.setHeight("100%");
mainContent.setContent(tableContainer);
mainContent.setScrollable(true);
centeringlayout.setSizeFull();
centeringlayout.addComponent(mainContent);
centeringlayout.setComponentAlignment(mainContent, Alignment.MIDDLE_CENTER);
tabPanel.addComponent(centeringlayout);
addComponent(tabPanel);
}
}
I would love to know if anyone sees any obvious errors in my code. And if anyone knows what property I can set on the footer CSS to have it occupy space in the content view so that the horizontal scroll doesn't appear underneath it. Thank you!
What I did to solve this issue was to structure the code as follows. This will create a vertical and horizontal scroll bar for the Panel holding my filter component and the table. Hopefully this can help someone with a similar problem.
#Override
protected void initComponent() {
super.initComponent();
if(!tableCreated) {
createSimCardsTable();
tableCreated = true;
}
mainWindow = this.getWindow();
Panel basePanel = new Panel("");
basePanel.addComponent(inventoryFilterPanel);
AbstractComponent separatorLine = Cf.horizontalLine(); //Of no signficance
separatorLine.addStyleName("m2m-horizontal-line-list-separator");
separatorLine.setWidth("99%");
basePanel.addComponent(separatorLine);
basePanel.addComponent(simCardTable);
basePanel.setSizeFull();
basePanel.getContent().setSizeUndefined(); // <-- This is the important part
addComponent(basePanel);
setExpandRatio(basePanel, 1);
}
All Vaadin components have size undefined by default, so usually there is no need to call method setSizeUndefined(). Also there is no need to call setScrollable(true), because it enables only programmatic scrolling possibility.
When I was trying to make a sense of scrolling appearance I wrote a simple skeleton of layout. Try this out as a content of the main window:
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
public class Skeleton extends VerticalLayout {
public Skeleton() {
setSizeFull();
addComponent(new Label("Header component"));
HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
Panel leftComponent = new Panel();
Panel rightComponent = new Panel();
splitPanel.setFirstComponent(leftComponent);
splitPanel.setSecondComponent(rightComponent);
for (int i = 0 ; i < 200 ; i ++) {
leftComponent.addComponent(new Label("left"));
rightComponent.addComponent(new Label("right"));
}
leftComponent.setSizeFull();
rightComponent.setSizeFull();
addComponent(splitPanel);
setExpandRatio(splitPanel, 1);
addComponent(new Label("Footer component"));
}
}
You should see scrollbars inside the nested panels. But if setSizeFull() is removed from Skeleton layout, then it is not limited in size (by default) and grows downwards - then only the scrollbar of the whole window appears.
Add this to your styles.css
.v-verticallayout > div {
overflow-y: auto ! important;
}
First of all try to make your panel scrollable by calling setScrollable(true) method, but this will not work if you set some custom layout with setSizeFull() as this panel new layout.
If you exactly know that you application will be opened in device with small screen resolution, you simple can set for your "primary"/"main" layout some fixed width and height, or add some CSS style with params like min-width: {some value} px, min-height: {some value} px.
Based on this post, I added vertical.setSizeUndefined(); and started seeing vertical scrollbars.
setMainWindow(new Window(title ));
vertical.setSizeFull();
vertical.setHeight("100%");
toolbar = createToolbar();
vertical.addComponent(toolbar);
vertical.setExpandRatio(toolbar, 0.03f);
Component tree = buildTree();
vertical.addComponent(tree);
vertical.setExpandRatio(tree, 0.97f);
vertical.setSizeUndefined();
getMainWindow().setContent(vertical);>
The only way I could fix this now (v6.8.14) is to specify the height in px values in stead of %
Use CustomLayout, always. It's faster, more efficient and by controlling html and css easily you can acieve a graphically consistent result

Swing: Positioning a popup from within a JScrollPane

I have a JTable inside of a JScrollPane. I am creating a custom cell editor for one of the columns of the table, and I want this editor to pop up a scrolling JList. I've done this by using a Popup to show a new JScrollPane containing the JList.
Everything is working, except for the position of the Popup. My custom component for the editor looks basically like this:
public class CustomPanel extends JPanel {
JTextField text = new JTextField();
JList list = new JList();
JScrollPane scroll = new JScrollPane(list);
Component owner = null;
public CustomPanel(Component owner) {
this.owner = owner;
add(text);
}
public void showPopup() {
Popup p = PopupFactory.getPopup(owner, scroll, getX(), getY()+getHeight());
p.show();
}
}
What is happening is that getX() and getY() are returning the position of the table cell relative to the JScrollPane holding it, and Popup is wanting absolute screen position. Even if I pass in owner the JScrollPane that they are relative to, it doesn't work. I get the same problem if I use text.getX() / text.getY().
How can I position my Popup directly below the TextBox?
Just a bit more background: The end goal is a multiple-select combobox that displays all of the selected items as a comma-separated list. If something else like this already exists, please don't hesitate to point me to it.
Edit: owner.getLocationOnScreen().y + getY() doesn't work when the scroll pane is anywhere but scrolled all the way up. However, just plain getLocationOnScreen().y DOES work. Problem solved, thank you.
You can query the absolute screen position with Component.getLocationOnScreen(). Is that what you're looking for?

Categories

Resources