|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import java.text.*; |
| 4 | +import java.util.Date; |
| 5 | +import javax.swing.*; |
| 6 | +import javax.swing.border.*; |
| 7 | +import javax.swing.text.*; |
| 8 | + |
| 9 | +public class SmartScrollerDemo extends JPanel |
| 10 | + implements ActionListener |
| 11 | +{ |
| 12 | + private JRadioButton horizontal; |
| 13 | + private JRadioButton vertical; |
| 14 | + private JRadioButton start; |
| 15 | + private JRadioButton end; |
| 16 | + |
| 17 | + private JScrollPane textAreaSP; |
| 18 | + private JScrollPane listSP; |
| 19 | + private JTextArea textArea; |
| 20 | + private JList list; |
| 21 | + |
| 22 | + private Timer timer; |
| 23 | + private SmartScroller textAreaScroller; |
| 24 | + private SmartScroller listScroller; |
| 25 | + |
| 26 | + public SmartScrollerDemo() |
| 27 | + { |
| 28 | + setBorder( new EmptyBorder(5, 5, 5, 5) ); |
| 29 | + setLayout( new BorderLayout(10, 10) ); |
| 30 | + |
| 31 | + add(createNorthPanel(), BorderLayout.NORTH); |
| 32 | + add(createCenterPanel()); |
| 33 | + add(createSouthPanel(), BorderLayout.SOUTH); |
| 34 | + |
| 35 | + resetScrolling(); |
| 36 | + } |
| 37 | + |
| 38 | + private JPanel createNorthPanel() |
| 39 | + { |
| 40 | + JPanel panel = new JPanel( new GridLayout(1, 0, 5, 5) ); |
| 41 | + |
| 42 | + JLabel textArea = new JLabel( "JTextArea" ); |
| 43 | + textArea.setHorizontalAlignment( JLabel.CENTER ); |
| 44 | + panel.add( textArea ); |
| 45 | + |
| 46 | + JLabel list = new JLabel( "JList" ); |
| 47 | + list.setHorizontalAlignment( JLabel.CENTER ); |
| 48 | + panel.add( list ); |
| 49 | + |
| 50 | + return panel; |
| 51 | + } |
| 52 | + |
| 53 | + private JPanel createCenterPanel() |
| 54 | + { |
| 55 | + JPanel panel = new JPanel( new GridLayout(1, 0, 5, 5) ); |
| 56 | + |
| 57 | + JTextArea textArea = new JTextArea(); |
| 58 | + textArea.setEditable( false ); |
| 59 | + textAreaSP = new JScrollPane( textArea ); |
| 60 | + panel.add( textAreaSP ); |
| 61 | + |
| 62 | + JList<String> list = new JList<String>( new DefaultListModel<String>() ); |
| 63 | + list.setVisibleRowCount(1); |
| 64 | + listSP = new JScrollPane( list ); |
| 65 | + panel.add( listSP ); |
| 66 | + |
| 67 | + return panel; |
| 68 | + } |
| 69 | + |
| 70 | + private JPanel createSouthPanel() |
| 71 | + { |
| 72 | + JPanel panel = new JPanel( new GridLayout(1, 0, 5, 5) ); |
| 73 | + TitledBorder border = new TitledBorder("Change properties then click Reset"); |
| 74 | + border.setTitleJustification( TitledBorder.CENTER ); |
| 75 | + panel.setBorder( border ); |
| 76 | + |
| 77 | + JPanel r1 = new JPanel( new GridLayout(0, 1) ); |
| 78 | + r1.setBorder( new TitledBorder("Scrolling Direction:") ); |
| 79 | + horizontal = new JRadioButton("Horizontal"); |
| 80 | + r1.add( horizontal ); |
| 81 | + vertical = new JRadioButton("Vertical"); |
| 82 | + vertical.setSelected( true ); |
| 83 | + r1.add( vertical ); |
| 84 | + ButtonGroup vbGroup = new ButtonGroup(); |
| 85 | + vbGroup.add( horizontal ); |
| 86 | + vbGroup.add( vertical ); |
| 87 | + panel.add( r1 ); |
| 88 | + |
| 89 | + JPanel r2 = new JPanel( new GridLayout(0, 1) ); |
| 90 | + r2.setBorder( new TitledBorder("Viewport Position:") ); |
| 91 | + start = new JRadioButton("Start"); |
| 92 | + r2.add( start ); |
| 93 | + end = new JRadioButton("End"); |
| 94 | + end.setSelected( true ); |
| 95 | + r2.add( end ); |
| 96 | + ButtonGroup huGroup = new ButtonGroup(); |
| 97 | + huGroup.add( start ); |
| 98 | + huGroup.add( end ); |
| 99 | + panel.add( r2 ); |
| 100 | + |
| 101 | + JButton button = new JButton("Reset Smart Scroller"); |
| 102 | + button.addActionListener( this ); |
| 103 | + panel.add( button ); |
| 104 | + |
| 105 | + return panel; |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + private void resetScrolling() |
| 110 | + { |
| 111 | + if (timer != null) |
| 112 | + timer.stop(); |
| 113 | + |
| 114 | + if (textAreaScroller != null) |
| 115 | + { |
| 116 | + textAreaSP.getVerticalScrollBar().removeAdjustmentListener(textAreaScroller); |
| 117 | + textAreaSP.getHorizontalScrollBar().removeAdjustmentListener(textAreaScroller); |
| 118 | + listSP.getVerticalScrollBar().removeAdjustmentListener(listScroller); |
| 119 | + listSP.getHorizontalScrollBar().removeAdjustmentListener(listScroller); |
| 120 | + } |
| 121 | + |
| 122 | + JTextArea textArea = (JTextArea)textAreaSP.getViewport().getView(); |
| 123 | + textArea.setText(""); |
| 124 | + final Document doc = textArea.getDocument(); |
| 125 | + final String prefix = vertical.isSelected() ? "\nplus some longer text and longer and longer" : "\tand some more text"; |
| 126 | + |
| 127 | + JList list = (JList)listSP.getViewport().getView(); |
| 128 | + list.setLayoutOrientation(vertical.isSelected() ? JList.VERTICAL : JList.HORIZONTAL_WRAP); |
| 129 | + final DefaultListModel model = (DefaultListModel)list.getModel(); |
| 130 | + model.clear(); |
| 131 | + |
| 132 | + int scrollDirection = vertical.isSelected() ? SmartScroller.VERTICAL : SmartScroller.HORIZONTAL; |
| 133 | + int position = end.isSelected() ? SmartScroller.END : SmartScroller.START; |
| 134 | + |
| 135 | + textAreaScroller = new SmartScroller(textAreaSP, scrollDirection, position); |
| 136 | + listScroller = new SmartScroller(listSP, scrollDirection, position); |
| 137 | + |
| 138 | + timer = new Timer(1000, new ActionListener() |
| 139 | + { |
| 140 | + public void actionPerformed(ActionEvent e) |
| 141 | + { |
| 142 | + try |
| 143 | + { |
| 144 | + Format formatter = DateFormat.getTimeInstance(); |
| 145 | + String now = formatter.format( new Date() ); |
| 146 | + |
| 147 | + int offset = end.isSelected() ? doc.getLength() : 0; |
| 148 | + doc.insertString(offset, prefix + now.toString(), null); |
| 149 | + |
| 150 | + int index = end.isSelected() ? model.getSize() : 0; |
| 151 | + model.insertElementAt(now + " ", index); |
| 152 | + |
| 153 | + } |
| 154 | + catch (BadLocationException e1) {} |
| 155 | + } |
| 156 | + }); |
| 157 | + timer.start(); |
| 158 | + } |
| 159 | + |
| 160 | + public void actionPerformed(ActionEvent e) |
| 161 | + { |
| 162 | + resetScrolling(); |
| 163 | + } |
| 164 | + |
| 165 | + public static void main(String[] args) |
| 166 | + { |
| 167 | + SwingUtilities.invokeLater(new Runnable() { |
| 168 | + public void run() { |
| 169 | + createAndShowGUI(); |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + public static void createAndShowGUI() |
| 175 | + { |
| 176 | + JFrame frame = new JFrame("Smart Scroller"); |
| 177 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 178 | + frame.add( new SmartScrollerDemo() ); |
| 179 | + frame.setSize(560, 260); |
| 180 | + frame.setLocationRelativeTo(null); |
| 181 | + frame.setVisible(true); |
| 182 | + } |
| 183 | +} |
0 commit comments