swing - Scroll Bar not working in Java -


i writing program in java, reason scroll bar in bottom component isn't working. have no idea why. should apply vertical scroll bar bottom text area. doing wrong?

import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; import javax.swing.timer; import java.text.*; import java.lang.*; import java.util.*;    public class timergui extends jframe { private static final int frame_height = 300; private static final int frame_width = 600;  private jpanel toppanel; private jpanel leftpanel; private jpanel bottompanel;  private jlabel toplabel; private jlabel leftlabel;  private jtextfield settimer; private jtextarea displaytimer;  private jscrollpane scrollpane;  private jbutton startbutton;  //no-arg constructor public timergui() {     createtoppanel();     createleftpanel();     createbottompanel();     setsize(frame_width, frame_height); }  //creates top panel, including label , text field  private void createtoppanel() {     toppanel = new jpanel();     toplabel = new jlabel("timer lab:");      final int field_width = 10;     settimer = new jtextfield(field_width);      toppanel.add(toplabel);     toppanel.add(settimer);      add(toppanel, borderlayout.north); }  //creates left panel, single text label private void createleftpanel() {     leftpanel = new jpanel();     leftlabel = new jlabel("(1000 milliseconds = 1 second)");      add(leftpanel, borderlayout.west);      leftpanel.add(leftlabel); }   //the listener start button class clicklistener implements actionlistener {     public void actionperformed(actionevent event)     {         int speed = integer.parseint(settimer.gettext());          mylistner listener = new mylistner();         timer timer = new timer(speed, listener);         timer.start();      } }  class mylistner implements actionlistener {     public void actionperformed(actionevent event)     {                  simpledateformat simpletime = new simpledateformat("hh:mm:ss.sss");         date = new date();         string time = simpletime.format(now);         displaytimer.append(time + string.format("%n"));     } }  //creates button, action listener attached private void createstartbutton() {     startbutton = new jbutton("start");      actionlistener listener = new clicklistener();     startbutton.addactionlistener(listener); }  //creates bottom panel, start button , display text field private void createbottompanel() {     final int field_width = 10;      bottompanel = new jpanel();     displaytimer = new jtextarea(10, 15);     displaytimer.seteditable(false);     scrollpane = new jscrollpane(displaytimer);        scrollpane.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always);      add(bottompanel, borderlayout.south);      createstartbutton();     bottompanel.add(startbutton);     bottompanel.add(displaytimer);     bottompanel.add(scrollpane); }  } 

you're adding both displaytimer , jscrollpane holds gui. don't this. add jscrollpane since can add component once gui, end adding jtextarea without jscrollpane empty jscrollpane (look right of jtextarea). change this:

    createstartbutton();     bottompanel.add(startbutton);     bottompanel.add(displaytimer);     bottompanel.add(scrollpane); 

to this:

    createstartbutton();     bottompanel.add(startbutton);     // !! bottompanel.add(displaytimer);     bottompanel.add(scrollpane); 

other issues:

  • avoid setting sizes.
  • instead call pack() , let gui set own size.

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -