How to display content in a JTextArea or ScrollPane - java

I input into a text field "C". What should be displayed should be the next 10 letters that come after C. What I could do is convert all these letters into strings and then set the strings text to a JLabels text, but instead I want to be able to show them on a textArea with a scroll pane so that it doesn't run off the page and instead stays within a window I can scroll down from.
Is there any way to accomplish this? I have a TextArea fitted with a scroll pane setup, but I don't know how to output any data here.

Related

Which parameter of this JTextArea should I change?

I'd like to know which parameter of my JTextArea I should change in order to remove this:
I want the text area to be blank and I don't want the possibility to navigate it with this brackets (?)
If you want to prevent the Scroll Bars from appearing for a JTextArea then change the Horizontal and Vertical Scroll Bar Policies for the JScrollPane that wraps that JTextArea:
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

Adding text to jTextarea in jTabbedPane

I have a desktop program with 3 tabs i'm building in netbeans. There is 1 text input field below the three tabs. each tab has a jtextarea component. When i type in the text input field and hit enter i want it to add that text to the textarea contained by the currently selected tab.
I've been playing around with
jTabbedPane1.getSelectedIndex()
jTabbedPane1.getTabComponentAt()
How can i use the index to reference it back to the textarea associated with that tab?
Big java noob here.
The simplest way would be to use an array of JTextArea components corresponding to the order that they appear on the JTabbedPane.
JTextArea[] textArea = new JTextArea[3];
// assign textAreas...
You could then set the text using the appropriate index:
int index = jTabbedPane1.getSelectedIndex();
textArea[index].setText(myField.getText());

Scroll text area to top after it has been filled

I have a few text areas that are filled with loops of information. Is it possible to make it 'jump' or scroll back to the top automatically after the loop has completed, so that the user sees it from the start and not the end?
You can use setCaretPosition() to set the cursor at position 0 of the TextArea. That usually works for me.
Use setUpdatePolicy with NEVER_UPDATE, and make sure the setUpdatePolicy comes before setting any text to the JTextArea. In my example I initialize a JTextArea without any text and call setUpdatePolicy right after.
JTextArea jTextArea = new JTextArea();
((DefaultCaret)jTextArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
I have solved this problem by creating a new TextArea which acts as a sort of buffer (and isn't displayed). The loop writes to this area and when completed gets the text from that and sets it to the required TextArea.

Scrollbar in html component in lwuit java

I want to create chat window with textarea and textbox in lwuit. Textarea must capable to show smiley and coloring text. I have used HtmlComponent of lwuit and facing problem in scrollbar. As text content grows in size, whole screen scroll including textbox. I want only content of textarea to be scrolled.
How to solve it?
well, you can forbid scrolling for the whole form using
Form.setScrollable(false).
Then you just set your HTMLcomponent scrollable "true" with the same method and finally put these two components (TextArea and HTMLComponent) to a different cosntraints (e.g. TextArea in BorderLayout.NORTH and HTMLComponent in BorderLayout.CENTER)

Scroll through JLabel held in JScrollPane

I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
list.ensureIndexIsVisible(list.getSelectedIndex());
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.

Categories

Resources