-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFormScrollerDemo.java
More file actions
165 lines (138 loc) · 4.43 KB
/
FormScrollerDemo.java
File metadata and controls
165 lines (138 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class FormScrollerDemo extends JPanel
implements ActionListener, ChangeListener, ItemListener
{
private FormScroller scroller;
private JSpinner top;
private JSpinner bottom;
private JSpinner left;
private JSpinner right;
FormScrollerDemo()
{
setLayout( new BorderLayout(10, 10) );
setBorder( new EmptyBorder(10, 10, 10, 10) );
JComponent left = createLeftPanel();
JComponent right = createRightPanel();
add(left, BorderLayout.CENTER);
add(right, BorderLayout.EAST);
}
private JComponent createLeftPanel()
{
Box content = new Box( BoxLayout.Y_AXIS );
Random random = new Random();
int groups = random.nextInt(10) + 5;
for (int i = 0; i < groups; i++)
{
content.add( createGroup(i) );
}
JScrollPane scrollPane = new JScrollPane( content );
scroller = new FormScroller( scrollPane );
scroller.setScrollInsets( new Insets(5, 0, 5, 0) );
return scrollPane;
}
private JComponent createGroup(int title)
{
Box group = new Box( BoxLayout.Y_AXIS );
group.setBorder( new CompoundBorder(new LineBorder(Color.RED), new TitledBorder("Group " + title)) );
Random random = new Random();
int details = random.nextInt(3) + 1;
for (int i = 0; i < details; i++)
{
group.add( createDetail(i) );
}
return group;
}
private JComponent createDetail(int title)
{
JPanel detail = new JPanel( new GridLayout(0, 2, 5, 5) );
detail.setBorder( new TitledBorder("Detail " + title) );
Random random = new Random();
int rows = random.nextInt(3) + 1;
for (int i = 0; i < rows; i++)
{
detail.add(new JLabel("Label " + i));
detail.add(new JTextField(5));
}
return detail;
}
public JPanel createRightPanel()
{
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setAlignment(RelativeLayout.LEADING);
rl.setFill( true );
JPanel main = new JPanel( rl );
JPanel type = new JPanel();
type.setBorder( new TitledBorder("Scroll Type") );
JComboBox<FormScroller.Type> comboBox = new JComboBox<>();
comboBox.addItem( FormScroller.Type.COMPONENT );
comboBox.addItem( FormScroller.Type.PARENT );
comboBox.addItem( FormScroller.Type.CHILD );
comboBox.addActionListener( this );
type.add( comboBox );
main.add( type );
JPanel insets = new JPanel( new GridLayout(0, 2, 5, 5) );
insets.setBorder( new TitledBorder("Scroll Insets:") );
top = new JSpinner(new SpinnerNumberModel(5, 0, 100 ,5));
top.addChangeListener( this );
insets.add( top );
insets.add( new JLabel("Top") );
bottom = new JSpinner(new SpinnerNumberModel(5, 0, 100 ,5));
bottom.addChangeListener( this );
insets.add( bottom );
insets.add( new JLabel("Bottom") );
left = new JSpinner(new SpinnerNumberModel(0, 0, 100 ,5));
left.addChangeListener( this );
insets.add( left );
insets.add( new JLabel("Left") );
right = new JSpinner(new SpinnerNumberModel(0, 0, 100 ,5));
right.addChangeListener( this );
insets.add( right );
insets.add( new JLabel("Right") );
main.add( insets );
JCheckBox scrollingEnabled = new JCheckBox("Scrolling Enabled");
scrollingEnabled.setSelected( true );
scrollingEnabled.addItemListener( this );
main.add( scrollingEnabled );
return main;
}
public void actionPerformed(ActionEvent e)
{
JComboBox comboBox = (JComboBox)e.getSource();
scroller.setType( (FormScroller.Type)comboBox.getSelectedItem() );
}
public void itemStateChanged(ItemEvent e)
{
JCheckBox checkBox = (JCheckBox)e.getSource();
scroller.setScrollingEnabled( checkBox.isSelected() );
}
public void stateChanged(ChangeEvent e)
{
Insets scrollInsets = new Insets(
(Integer)top.getValue(), (Integer)left.getValue(), (Integer)bottom.getValue(), (Integer)right.getValue());
scroller.setScrollInsets( scrollInsets );
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("Form Scroller");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new FormScrollerDemo() );
frame.setSize(400, 300);
// frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}