วิธีการ Synchronize 2 ScrollPane

เทคนิคการเขียนโปรแกรมด้วยภาษา Java
Post Reply
brid.surapol
Posts: 156
Joined: 11 Apr 2013, 11:43

Re: วิธีการ Synchronize 2 ScrollPane

Post by brid.surapol »

Code: Select all

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
public class ScrollPaneTest
{
    public ScrollPaneTest()
    {
        JScrollPane scrollPane1 = new JScrollPane(getPanel());
        JScrollPane scrollPane2 = new JScrollPane(getPanel());
   
        Synchronizer synchronizer = new Synchronizer(scrollPane1, scrollPane2);
        scrollPane1.getVerticalScrollBar().addAdjustmentListener(synchronizer);
        scrollPane1.getHorizontalScrollBar().addAdjustmentListener(synchronizer);
        scrollPane2.getVerticalScrollBar().addAdjustmentListener(synchronizer);
        scrollPane2.getHorizontalScrollBar().addAdjustmentListener(synchronizer);
   
        JPanel panel = new JPanel(new GridLayout(1,0));
        panel.add(scrollPane1);
        panel.add(scrollPane2);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.setSize(500,300);
        f.setLocation(200,200);
        f.setVisible(true);
    }
  
    private JPanel getPanel()
    {
        JPanel panel = new JPanel()
        {
            GradientPaint gradient;
  
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D)g;
                if(gradient == null)
                    init();
                g2.setPaint(gradient);
                g2.fillRect(0, 0, getWidth(), getHeight());
            }
  
            private void init()
            {
                gradient = new GradientPaint(0, 0, Color.red,
                                     getWidth(), getHeight(), Color.blue);
            }
        };
        panel.setPreferredSize(new Dimension(400,400));
        return panel;
    }
  
    public static void main(String[] args)
    {
        new ScrollPaneTest();
    }
}
  
class Synchronizer implements AdjustmentListener
{
    JScrollBar v1, h1, v2, h2;
  
    public Synchronizer(JScrollPane sp1, JScrollPane sp2)
    {
        v1 = sp1.getVerticalScrollBar();
        h1 = sp1.getHorizontalScrollBar();
        v2 = sp2.getVerticalScrollBar();
        h2 = sp2.getHorizontalScrollBar();
    }
  
    public void adjustmentValueChanged(AdjustmentEvent e)
    {
        JScrollBar scrollBar = (JScrollBar)e.getSource();
        int value = scrollBar.getValue();
        JScrollBar target = null;
  
        if(scrollBar == v1)
            target = v2;
        if(scrollBar == h1)
            target = h2;
        if(scrollBar == v2)
            target = v1;
        if(scrollBar == h2)
            target = h1;
  
        target.setValue(value);
    }
}
brid.surapol
Posts: 156
Joined: 11 Apr 2013, 11:43

Re: วิธีการ Synchronize 2 ScrollPane

Post by brid.surapol »

Code: Select all

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
class Testing extends JFrame
{
  public Testing()
  {
    JScrollPane sp1 = new JScrollPane(new ScrollPanel());
    JScrollBar sBar1 = sp1.getVerticalScrollBar();
    JScrollPane sp2 = new JScrollPane(new ScrollPanel());
    JScrollBar sBar2 = sp2.getVerticalScrollBar();
    sBar2.setModel(sBar1.getModel()); //<--------------synchronize
    getContentPane().setLayout(new GridLayout(1,2));
    getContentPane().add(sp1);
    getContentPane().add(sp2);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(400,300);
    setLocation(400,300);
  }
  public static void main(String[] args) {new Testing().setVisible(true);}
}
class ScrollPanel extends JPanel
{
  public ScrollPanel()
  {
    setLayout(new GridLayout(50,1));
    for(int x = 0; x < 50; x++) add(new JLabel(""+(x+1)));
  }
}
Post Reply

Return to “Java Programming Techinics”