swing horizontal scroll bar problem
public TaskGraphComponent(ProjectFrame proFrame,TaskGraphModel model, painter) {
this.proFrame = proFrame;
_painter = painter;
setModel(model);
_mouseHandler = new GraphMouseHandler(this);
_verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
this.add(_verticalScroll, BorderLayout.EAST);
_verticalScroll.addAdjustmentListener(this);
_verticalScroll.setVisible(true);
_horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
add(_horizontalScroll, BorderLayout.SOUTH);
_horizontalScroll.addAdjustmentListener(this);
_horizontalScroll.setVisible(tru开发者_开发问答e);
setBorder(BorderFactory.createEmptyBorder(0,0,15,0));
this.addMouseMotionListener(_mouseHandler);
this.addMouseListener(_mouseHandler);
this.addMouseWheelListener(_mouseHandler);
this.addKeyListener(_mouseHandler);
this.addComponentListener(this);
this.setFocusable(false);
ToolTipManager.sharedInstance().setDismissDelay(8000);
ToolTipManager.sharedInstance().setReshowDelay(3000);
}
I'm extending from JComponent here vertical scroll bar working fine but horizontal scroll is not showing.
Here TaskGraphComponent is jcomponent
..not using any layout..
That is the problem. Use layouts.
I tested this code please run it and let me know. I change the code from the above link: http://www.java2s.com/Code/Java/Swing-JFC/AquickdemonstrationofJScrollBarbothverticalandhorizontal.htm
import java.awt.BorderLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
public class SwingScrollBarExample extends JComponent implements AdjustmentListener {
JLabel label;
public SwingScrollBarExample() {
label = new JLabel();
setLayout(new BorderLayout());
JScrollBar _verticalScroll = new JScrollBar(JScrollBar.VERTICAL);
this.add(_verticalScroll);
_verticalScroll.addAdjustmentListener(this);
_verticalScroll.setVisible(true);
JScrollBar _horizontalScroll = new JScrollBar(JScrollBar.HORIZONTAL);
this.add(_horizontalScroll);
_horizontalScroll.addAdjustmentListener(this);
_horizontalScroll.setVisible(true);
this.add(_verticalScroll, BorderLayout.EAST);
this.add(_horizontalScroll, BorderLayout.SOUTH);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
label.setText(" New Value is " + e.getValue() + " ");
repaint();
}
public static void main(String s[]) {
JFrame frame = new JFrame("Scroll Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SwingScrollBarExample());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
精彩评论