简体   繁体   中英

JScrollPane not appearing on JTextArea

I'm trying to add the JScrollPane to my JTextArea, but somehow, it won't appear. I've tried resizing it according to the dimension of the JTextArea, but it doesn't seem to work. Also, notice that I'm using the null layout because I want the full-on flexibility of displaying certain buttons and panels at a pinpoint location.

import java.awt.Dimension;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTextArea;

public class PaneTester{

  private static JFrame frame;
  private static JPanel panel;
  private static JScrollPane scrollPane;
  private static JTextArea notificationBox;

  public static void main (String [] args){
    stage1(); 
    stage2();
  }

  private static void stage1(){
    createFrame();
    createPanel();
    frame.getContentPane().add(panel);
    panel.setVisible(true);
    frame.setVisible(true);
  }

  private static void stage2(){
    generateNotificationBox();
  }

  private static void createFrame(){
    frame = new JFrame();
    frame.setSize(new Dimension(900,700)); 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);
  }

  private static void createPanel(){
    panel = new JPanel();
    panel.setLayout(null);
    generateGridButtons();
  }

  private static void generateGridButtons(){
    short y = 0; 
    for(short i=0;i<4;i++){
      y += 60;
      short x = 500;
      for(short j=0;j<5;j++){
        JButton gridButton = new JButton();
        gridButton.setBounds(x, y,120,60);
        panel.add(gridButton);
        x += 140;
      }
    }    
  }
  public static void generateNotificationBox(){
    notificationBox = new JTextArea(10,10);
    notificationBox.setBounds(25, 25, 200, 400);
    scrollPane = new JScrollPane(notificationBox, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ); 

    Dimension d = new Dimension(notificationBox.getPreferredSize());
    scrollPane.getViewport().setPreferredSize(d);
    scrollPane.getViewport().add(notificationBox);
    panel.add(notificationBox);
    panel.repaint();
  }

}

Stop mucking with setBounds and setPreferredSize , you're just making live more difficult for your self.

If you want to affect the size of JTextArea (and the viewable area of the JScrollPane ) have a look at the JTextArea constructor JTextArea(int rows, int columns ) , which will allow you to specify the number of rows/columns you want the JTextArea to default to, and which will allow the JTextArea to calculate it's preferredSize based on the current font's metrics in more stable cross platform way

Your core problem, however, is right here...

scrollPane.getViewport().add(notificationBox);
panel.add(notificationBox);

You add the notificationBox to the JScrollPane s JViewport , which is good, but then you add notificationBox to the panel , which will remove it from the JScrollPane 's JViewport , which is bad

Instead, add the JScrollPane to the panel

scrollPane.getViewport().add(notificationBox);
panel.add(scrollPane);

You're also making overuse of static . I'd highly recommend you take the time to reduce static down to it's absolute minimum required usage, this will probably mean that rather then constructing the UI in the main method, you have a "main" class which you can insatiate (from main ) which will perform the initial setup - IMHO

I've tried that. I think someone else suggested that from another post, but when I tried that, it just took away the JTextArea completely from the panel

Get rid of panel.setLayout(null); and start making use of appropriate layout managers and compound layouts. Start by having look at Laying Out Components Within a Container for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM