4 //web_AppletName= MyTest1
5 //web_Description= A test
6 //web_JavaVersion= http://www.dmitry
7 //web_AppletImage= dddd
10 //web_Features= graphics, AWT-to-Swing
12 import java.awt.Adjustable;
13 import java.awt.Color;
14 import java.awt.Dimension;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.AdjustmentEvent;
17 import java.awt.event.AdjustmentListener;
18 import java.awt.event.FocusEvent;
19 import java.awt.event.FocusListener;
20 import java.awt.event.MouseEvent;
21 import java.awt.event.MouseListener;
22 import java.awt.event.MouseMotionListener;
23 import java.awt.event.MouseWheelEvent;
24 import java.awt.event.MouseWheelListener;
25 import java.text.DecimalFormat;
27 import javax.swing.ButtonGroup;
28 import javax.swing.JApplet;
29 import javax.swing.JComponent;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollBar;
33 import javax.swing.JScrollPane;
34 import javax.swing.JSlider;
35 import javax.swing.JTextField;
36 import javax.swing.JToggleButton;
37 import javax.swing.JViewport;
38 import javax.swing.SwingConstants;
39 import javax.swing.event.ChangeEvent;
40 import javax.swing.event.ChangeListener;
41 import javax.swing.plaf.ViewportUI;
43 public class Test_Applet_Scroll extends JApplet implements ChangeListener {
49 * thisApplet.__Info.width = 500;
50 * thisApplet.__Info.height = 400;
51 * thisApplet.__Info.isResizable = true;
54 static DecimalFormat df = new DecimalFormat("0.00");
56 boolean preferred = true;
58 private JScrollBar hsb;
60 private JScrollBar sbar;
62 void setSize(JComponent c, int x, int y) {
64 c.setPreferredSize(new Dimension(x, y));
72 final JLabel label = new JLabel("hello");
73 // label.setBounds(0, 60, 200, 60);
74 setSize(label, 80, 50);
75 label.setBackground(Color.yellow);
76 label.setForeground(Color.BLUE);
77 label.setOpaque(true);
78 label.setHorizontalAlignment(SwingConstants.RIGHT);
79 label.setVerticalAlignment(SwingConstants.CENTER);
81 final JTextField tf = new JTextField("12.5", 8);
82 tf.setBackground(Color.black);
83 tf.setForeground(Color.yellow);
86 tf.addActionListener(new java.awt.event.ActionListener() {
88 public void actionPerformed(ActionEvent event) {
89 label.setBackground(Color.white);
90 label.setText(tf.getText());
94 tf.addFocusListener(new FocusListener() {
97 public void focusGained(FocusEvent e) {
98 tf.setBackground(Color.BLUE);
102 public void focusLost(FocusEvent e) {
103 tf.setBackground(Color.BLACK);
107 tf.addMouseWheelListener(new MouseWheelListener() {
109 public void mouseWheelMoved(MouseWheelEvent e) {
110 int n = e.getWheelRotation();
111 tf.setText("" + (Float.parseFloat(tf.getText()) + n));
112 // e.consume(); not necessary for scrollbars
115 final JToggleButton button = new JToggleButton("test");
116 setSize(button, 80, 20);
117 button.setBackground(Color.orange);
118 button.addActionListener(new java.awt.event.ActionListener() {
120 public void actionPerformed(ActionEvent event) {
121 label.setBackground(button.isSelected() ? Color.green : Color.yellow);
122 tf.setBackground(Color.black);
123 label.setText("test");
127 final JToggleButton button2 = new JToggleButton("btn2");
128 button2.addMouseListener(new MouseListener() {
131 public void mouseClicked(MouseEvent e) {
133 System.out.println("BTN2 clicked " + e.getClickCount());
138 public void mousePressed(MouseEvent e) {
139 // TODO Auto-generated method stub
144 public void mouseReleased(MouseEvent e) {
145 System.out.println("BTN2 released");
150 public void mouseEntered(MouseEvent e) {
151 System.out.println("in button2");
152 // TODO Auto-generated method stub
157 public void mouseExited(MouseEvent e) {
158 System.out.println("out button2");
163 // BasicToggleButtonUI us; just using this to get access to code for
164 // BasicToggleButtonUI
165 setSize(button2, 80, 20);
166 button2.setBackground(Color.orange);
167 button2.addActionListener(new java.awt.event.ActionListener() {
169 public void actionPerformed(ActionEvent event) {
170 label.setBackground(button2.isSelected() ? Color.green : Color.yellow);
171 tf.setBackground(Color.green);
172 label.setText("btn2");
177 button2.addMouseMotionListener(new MouseMotionListener() {
180 public void mouseDragged(MouseEvent e) {
181 System.out.println("btn2 DRAG " + e);
185 public void mouseMoved(MouseEvent e) {
186 // TODO Auto-generated method stub
192 // the first two buttons act like radio buttons; only one is ever ON
194 ButtonGroup bg = new ButtonGroup();
198 // the third button is not part of the group
199 // note that JButtonUI does not need to know anything about the groups
201 final JToggleButton button3 = new JToggleButton("btn3");
202 // BasicToggleButtonUI us; just using this to get access to code for
203 // BasicToggleButtonUI
204 setSize(button3, 80, 20);
205 button3.setBackground(Color.red);
206 button3.addActionListener(new java.awt.event.ActionListener() {
208 public void actionPerformed(ActionEvent event) {
209 label.setBackground(button3.isSelected() ? Color.green : Color.yellow);
210 tf.setBackground(Color.black);
211 label.setText("btn3");
216 JPanel p = new JPanel();
218 p.addMouseListener(new MouseListener() {
221 public void mouseClicked(MouseEvent e) {
223 System.out.println("PANEL clicked " + e.getClickCount());
229 public void mousePressed(MouseEvent e) {
230 // TODO Auto-generated method stub
235 public void mouseReleased(MouseEvent e) {
236 // TODO Auto-generated method stub
241 public void mouseEntered(MouseEvent e) {
242 // TODO Auto-generated method stub
244 System.out.println("in panel");
249 public void mouseExited(MouseEvent e) {
250 // TODO Auto-generated method stub
252 System.out.println("out panel");
259 p.addMouseMotionListener(new MouseMotionListener() {
262 public void mouseDragged(MouseEvent e) {
263 System.out.println("panel DRAG " + e);
267 public void mouseMoved(MouseEvent e) {
268 System.out.println("panel Move " + e);
274 p.setToolTipText("this is the panel");
275 // p.setLayout(new GridLayout(2, 2, 2, 2));
276 JScrollPane sp = new JScrollPane();
277 sp.getViewport().add(p);
278 getContentPane().add(sp);
279 sp.getViewport().addChangeListener(this);
280 hsb = sp.getHorizontalScrollBar();
281 button2.setToolTipText("this is hsb");
283 mkBar(p, tf, Adjustable.VERTICAL, 20, 200).setToolTipText("this is scrollbar 1");
284 mkSlider(p, tf, Adjustable.VERTICAL, 20, 200).setToolTipText("this is slider 2");
286 mkSlider(p, tf, Adjustable.VERTICAL, 20, 200).setInverted(true);
288 label.setToolTipText("this is label");
290 tf.setToolTipText("this is tf");
294 p.setBackground(Color.blue);
295 button2.setToolTipText("this is Button 2");
296 button3.setToolTipText("this is Button 3");
297 mkBar(p, tf, Adjustable.HORIZONTAL, 100, 20);
298 mkSlider(p, tf, Adjustable.HORIZONTAL, 100, 20);
299 mkSlider(p, tf, Adjustable.HORIZONTAL, 100, 20).setInverted(true);
303 JScrollBar mkBar(JPanel p, final JTextField tf, int orient, int x, int y) {
304 final JScrollBar bar = new JScrollBar(orient, 500, 10, 300, 1000);
305 bar.addAdjustmentListener(new AdjustmentListener() {
308 public void adjustmentValueChanged(AdjustmentEvent e) {
309 tf.setText(df.format(e.getValue() / 100.0));
313 bar.addMouseWheelListener(new MouseWheelListener() {
315 public void mouseWheelMoved(MouseWheelEvent e) {
316 int n = e.getWheelRotation();
317 bar.setValue(bar.getValue() + n * 5);
318 // e.consume(); not necessary for scrollbars
322 bar.setBackground(Color.orange);
323 bar.setForeground(Color.green);
327 bar.setVisibleAmount(80);
331 JSlider mkSlider(JPanel p, final JTextField tf, int orient, int x, int y) {
332 final JSlider bar = new JSlider(orient, 300, 1000, 500);
333 bar.addChangeListener(new ChangeListener() {
335 public void stateChanged(ChangeEvent e) {
336 tf.setText(df.format(((JSlider) e.getSource()).getValue() / 100.0));
339 bar.addMouseWheelListener(new MouseWheelListener() {
341 public void mouseWheelMoved(MouseWheelEvent e) {
342 int n = e.getWheelRotation();
343 bar.setValue(bar.getValue() + n * 5);
344 // e.consume(); not necessary for sliders
348 bar.setBackground(Color.orange);
349 bar.setForeground(Color.green);
356 public void stateChanged(ChangeEvent e) {
357 // Viewport has scrolled
358 // JViewport v = (JViewport) e.getSource();
359 // System.out.println("extent " +v.getExtentSize() + " " + v.getViewPosition());
360 // if (v.getViewRect().x > 0)
361 // System.out.println("view change: " + v.getViewRect());
362 // System.out.println(v.getWidth() + " " + v.getHeight() + " " + v.getView().getBounds());
363 // System.out.println(sbar.getValue() + " "+ sbar.getVisibleAmount() + " " + sbar.getMaximum());