summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing3801.java
blob: 8a783267d04cd14360f94ba52892770782d479df (plain)
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
/* Listing3801.java */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Listing3801
extends JFrame
{
  public Listing3801()
  {
    super("JScrollPane");
    addWindowListener(new WindowClosingAdapter(true));
    //Dialogpanel erzeugen
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(10, 10));
    for (int i = 1; i <= 100; ++i) {
      panel.add(new JCheckBox("Frage " + i));
    }
    //JScrollPane erzeugen
    JScrollPane scroll = new JScrollPane(panel);
    scroll.setCorner(
      JScrollPane.UPPER_RIGHT_CORNER,
      new JLabel("1", JLabel.CENTER)
    );
    scroll.setCorner(
      JScrollPane.LOWER_RIGHT_CORNER,
      new JLabel("2", JLabel.CENTER)
    );
    scroll.setColumnHeaderView(new ColumnHeader(panel, 10));
    //JScrollPane zur ContentPane hinzuf�gen
    getContentPane().add(scroll, BorderLayout.CENTER);
  }

  public static void main(String[] args)
  {
    Listing3801 frame = new Listing3801();
    frame.setLocation(100, 100);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

class ColumnHeader
extends JComponent
{
  JComponent component;
  int        columns;

  public ColumnHeader(JComponent component, int columns)
  {
    this.component = component;
    this.columns   = columns;
  }

  public void paintComponent(Graphics g)
  {
    int width = component.getSize().width;
    int height = getSize().height;
    int colwid = width / columns;
    for (int i = 0; i < columns; ++i) {
      g.setColor(i % 2 == 0 ? Color.yellow : Color.gray);
      g.fillRect(i * colwid, 0, colwid, height);
    }
    g.setColor(Color.black);
    g.drawLine(0, height - 1, width, height - 1);
  }

  public Dimension getPreferredSize()
  {
    return new Dimension(component.getSize().width, 20);
  }
}