Java by API/javax.swing/JViewport

Материал из Java эксперт
Версия от 14:19, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

JViewport: setView(Component com)

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
public class MainClass {
  public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" },
        { "three", "3" } };
    final Object headers[] = { "English", "Digit" };
    
    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JTable table = new JTable(rows,headers);
    
    JScrollPane scrollPane = new JScrollPane(table);
    JViewport viewport = new JViewport();
    viewport.setView(table);
    scrollPane.setColumnHeaderView( new JLabel("table header here"));
    scrollPane.setRowHeaderView(viewport);
    
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





JViewport: setViewPosition(Point p)

 
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.event.MouseInputAdapter;
public class Main extends JFrame {
  public Main() {
    super("Grab and drag Demo");
    ImageIcon ii = new ImageIcon("largejexpLogo.jpg");
    JScrollPane jsp = new JScrollPane(new GrabAndScrollLabel(ii));
    getContentPane().add(jsp);
    setSize(300, 250);
    setVisible(true);
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public static void main(String[] args) {
    new Main();
  }
}
class GrabAndScrollLabel extends JLabel {
  public GrabAndScrollLabel(ImageIcon i) {
    super(i);
    MouseInputAdapter mia = new MouseInputAdapter() {
      int xDiff, yDiff;
      Container c;
      public void mouseDragged(MouseEvent e) {
        c = GrabAndScrollLabel.this.getParent();
        if (c instanceof JViewport) {
          JViewport jv = (JViewport) c;
          Point p = jv.getViewPosition();
          int newX = p.x - (e.getX() - xDiff);
          int newY = p.y - (e.getY() - yDiff);
          int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
          int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
          if (newX < 0)
            newX = 0;
          if (newX > maxX)
            newX = maxX;
          if (newY < 0)
            newY = 0;
          if (newY > maxY)
            newY = maxY;
          jv.setViewPosition(new Point(newX, newY));
        }
      }
      public void mousePressed(MouseEvent e) {
        setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        xDiff = e.getX();
        yDiff = e.getY();
      }
      public void mouseReleased(MouseEvent e) {
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      }
    };
    addMouseMotionListener(mia);
    addMouseListener(mia);
  }
}





new JViewport()

 
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
public class MainClass {
  public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" },
        { "three", "3" } };
    final Object headers[] = { "English", "Digit" };
    
    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JTable table = new JTable(rows,headers);
    
    JScrollPane scrollPane = new JScrollPane(table);
    JViewport viewport = new JViewport();
    viewport.setView(table);
    scrollPane.setColumnHeaderView( new JLabel("table header here"));
    scrollPane.setRowHeaderView(viewport);
    
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}