简体   繁体   中英

Add a scrollbar to JFrame

I'm having some problem finding a way to add a scrollbar in a JFrame . I found a method to redirect System.{in,out,err} to a JTextArea but I was not successful in adding a scrollbar. I hope this question is not redundant.

public class console extends JTextArea {    

 public static JTextArea console(final InputStream out, final PrintWriter in) {
    final JTextArea area = new JTextArea();
        new SwingWorker<Void, String>() {
        @Override protected Void doInBackground() throws Exception {
            Scanner s = new Scanner(out);
            while (s.hasNextLine()) publish(s.nextLine() + "\n");
            return null;
        }
        @Override protected void process(List<String> chunks) {
            for (String line : chunks) area.append(line);
        }
    }.execute();
    area.addKeyListener(new KeyAdapter() {
        private StringBuffer line = new StringBuffer();
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (c == KeyEvent.VK_ENTER) {
                in.println(line);
                line.setLength(0); 
            } else if (c == KeyEvent.VK_BACK_SPACE) { 
                line.setLength(line.length() - 1); 
            } else if (!Character.isISOControl(c)) {
                line.append(e.getKeyChar());
            }
        }
    });

    return area;
}
public static void main(String[] args) throws IOException {
    PipedInputStream inPipe = new PipedInputStream();
    PipedInputStream outPipe = new PipedInputStream();
    System.setIn(inPipe);
    System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));

    PrintWriter inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);

    JFrame frame = new JFrame("\"Console\"");

    frame.getContentPane().add(console(outPipe, inWriter));
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

   // my code
  }

Create a new JScrollPane object and set the ScrollBarPolicies .

If you do not want to the scollbars to appear always, just delete the policies.

EDIT

Also, don't use KeyListener on text components, use a combination of DocumentListner and key bindings instead. (Thanks to @MadProgrammer)


Solution

JScrollPane jsp = new JScrollPane(console(outPipe, inWriter));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(jsp);

Output

在此处输入图像描述

You can try with,

public class Test {

    public static void main(String [] a) {

        final JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane pane = new JScrollPane(
                           JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        frame.setContentPane(pane);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
   }

}

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