O
O
olololosh2015-04-09 15:26:49
Java
olololosh, 2015-04-09 15:26:49

Scrolling pages with JTextAre?

I was told that the JTextArea class has a method, or constructor, that allows you to organize "page scrolling". The meaning is this. In the text area, of course, all the text does not fit. I was told that it is possible to do this so that a certain number of characters would be displayed in the text zone. Well, this is understandable, there is simply a JTextArea (int, int) constructor for this, but I don’t know what to do to display a new portion of these characters. I want that when the user finishes reading the text, he could press the button and get a new portion of the text on the screen, press the button again - new text, and so on. This is where I can't think of anything. I reviewed all the methods and constructors of the class, but did not find anything like that. Maybe I was looking badly ... But a person told me that this should be. Can you tell me something?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita, 2015-04-10
@jkotkot

Well, put it in JScrollPane and change the current scrolling position by some value by pressing the button.

T
Timur, 2015-04-09
@timych

olololosh I don't see the problem. You break the text into fragments of the desired length. Putting it into an array. By pressing the "forward / backward" button, you take the desired piece of text from the array at the current index and replace it with a text field. You change the index for each click (within the size of the array, of course).

E
Evgeny Kornachev, 2015-04-11
@zelan

For swing

public static void main(String[] args) {
    
    JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(300, 300));
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    JTextArea textArea  = new JTextArea();
    
    /*
     * установи какой-нибудь текст или вбей прямо в окне
     */
//		textArea.setText();
    
    /*
     * использование следующих 2 методов
     * отключает горизонтальный скроллинг компонента
     * тем самым пролистываение возможно только вертикально
     */
    textArea.setLineWrap(true); 			//перенос только по символам символам
    textArea.setWrapStyleWord(true);		//перенос по словам и символам (если слово не влезает по ширине окна)
    JScrollPane scrollPane_TextArea = new JScrollPane(textArea);
    
    /**
     * просто тестовый размер
     */
    scrollPane_TextArea.setPreferredSize(new Dimension(200, 200));
    
    /*
     * если вертикальный скрол не нужен то раскоментируй
     */
//		scrollPane_TextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 
    
    JButton button_PrevPage = new JButton("PrevPage");
    JButton button_NextPage = new JButton("NextPage");
    
    
    button_PrevPage.setActionCommand("prev");
    button_NextPage.setActionCommand("next");
    

    class ChangePageListeners implements ActionListener{
      
      JScrollPane scrollPane;
      public ChangePageListeners(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        
        switch (command) {
        case "prev":
          changePage(false);
          break;
        case "next":
          changePage(true);
          break;
        default:
          break;
        }
        
      }
      
      private void changePage(boolean next){
        if(next == true){
          int heigthPage = scrollPane.getHeight();
          int currentPosition =  scrollPane.getVerticalScrollBar().getValue();				
          scrollPane.getVerticalScrollBar().setValue(currentPosition + heigthPage);
        }
        
        else{
          int heigthPage = scrollPane.getHeight();
          int currentPosition =  scrollPane.getVerticalScrollBar().getValue();				
          scrollPane.getVerticalScrollBar().setValue(currentPosition - heigthPage);
        }
      }
      
    }
    
    ChangePageListeners changePageListeners = new ChangePageListeners(scrollPane_TextArea);
    
    /*
     * устанавливаем на кнопки слушатели
     */
    button_NextPage.addActionListener(changePageListeners);
    button_PrevPage.addActionListener(changePageListeners);
    
    
    JPanel panel = new JPanel();
    panel.add(scrollPane_TextArea);
    panel.add(button_NextPage);
    panel.add(button_PrevPage);
    
    frame.add(panel);
    
    frame.setVisible(true);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question