-1

Helloo! In the Java Application I have a JScrollPane and in that scroll I have a JTextArea

JTextArea TextArea = new JTextArea("Text");
scroll = new JScrollPane(TextArea);
scroll.setBounds(150,100,250,100);

And I got the scroll. But If the user clicks a JButton the location of the JScrollPane should change... I have this code and it works if the scroll doesn't have the TextArea

scroll.setBounds(50,100,250,100);

but if the scroll has the TextArea it doesn't move at all

Any idea what is happening?

J Woodchuck
  • 2,265
  • 24
  • 44
albita
  • 31
  • 6
  • Have you tried calling `repaint`? – MadProgrammer May 02 '16 at 02:09
  • What kind of layout are you using? – lkq May 02 '16 at 02:40
  • Variable names should NOT start with an upper case character. One of your variables is correct the other isn't. Be consistent and follow Java conventions!!! – camickr May 02 '16 at 02:41
  • repaint is not working, i'm not using layouts and the variables are just an example of my code, is not the real one (: – albita May 02 '16 at 02:45
  • set the layout of your container to null e.g. `setLayout(null)` and then try `setBounds` – Muhammad May 02 '16 at 03:00
  • 2
    (1-) `the variables are just an example of my code, is not the real one` - well how do you expect us to help when we don't know what your "real" code is. We can't guess what you are actually doing. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr May 02 '16 at 03:16
  • 1
    @Muhammad: Please do not promote using a null layout. – trashgod May 02 '16 at 06:35
  • 1
    @albita: For [example](http://stackoverflow.com/a/14011536/230513). – trashgod May 02 '16 at 06:36
  • @trashgod it's not my recommendation but it is the recommendation of Oracle to use null layout for absolute positioning. https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html – Muhammad May 02 '16 at 06:46
  • 1
    @Muhammad: Thank you for providing the tutorial link, which suggests that "you should use a layout manager if at all possible." I believe it is possible in this case. – trashgod May 02 '16 at 06:56
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. .. – Andrew Thompson May 02 '16 at 08:09
  • .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 02 '16 at 08:09

2 Answers2

2

Change bounds of a JScrollPane ..

The bounds come down to the position and size of the component.

The best way to change the size of a scroll pane is to change the size of the component it is displaying. A text area can be resized by setting the number of rows & columns (easily specified in the constructor), or by setting a different font size.

The best way to position the scroll pane is to use layouts, along with layout padding and borders for white space.

Community
  • 1
  • 1
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
  • Thanks, I used a scroll.setLocation(int x, int y) – albita May 06 '16 at 02:00
  • *"I used a scroll.setLocation(int x, int y)"* By doing so, you are trying to deploy software that will break on other machines. – Andrew Thompson May 06 '16 at 02:27
  • To repeat the comment I made to the question: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 06 '16 at 02:28
0

Use vScrollPane.setValue() hScrollPane.setValue() methods.

Like this

import java.awt.event.*;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

class JScrollPaneToTopAction implements ActionListener {
  JScrollPane scrollPane;
  public JScrollPaneToTopAction(JScrollPane scrollPane) {
    if (scrollPane == null) {
      throw new IllegalArgumentException(
        "JScrollPaneToTopAction: null JScrollPane");
    }
    this.scrollPane = scrollPane;
  }
  public void actionPerformed(ActionEvent actionEvent) {
    JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
    JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
    verticalScrollBar.setValue(20);
    horizontalScrollBar.setValue(100);
  }
}


public class JScrollPaneToTopActionDemo {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = new JLabel("Label");
    label.setPreferredSize(new Dimension(1000, 1000));
    JScrollPane jScrollPane = new JScrollPane(label);

    JButton bn = new JButton("Move");

    bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));

    frame.add(bn, BorderLayout.SOUTH);
    frame.add(jScrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}
shimbu shambu
  • 127
  • 2
  • 10