swing storm

java桌面应用

导航

<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(15)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜

swing components-windows ScrollPane

windows的scrollPane与table头持平,这个功能以前我一直都不直到怎么实现,最近茅舍顿开,时间不充裕,做的还不完善,有兴趣可以看一下。

效果:
AzWindowsScrollPane.gif

代码:
package azure.ui.swing.components.scrollPane;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

/**
 * 
@author Azure
 * 
@version 1.0 31/03/07
 
*/
public class AzWindowsScrollFrame extends JFrame implements AdjustmentListener {
    JScrollBar vScrollBar 
= new JScrollBar();

    JScrollBar hScrollBar 
= new JScrollBar();

    JTable table;

    
public AzWindowsScrollFrame() {
        init();
    }

    
public static void main(String[] args) {
        
try {
            UIManager
                    .setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } 
catch (Exception e) {
            System.err.println(
"Something went wrong!");
        }
        AzWindowsScrollFrame sf 
= new AzWindowsScrollFrame();
        sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sf.setLocationRelativeTo(
null);
        sf.setSize(
new Dimension(300200));
        sf.setVisible(
true);
    }

    
private void init() {
        
this.setLayout(new BorderLayout());

        Object data[][] 
= { { "one""1""swing" }, { "two""2""is" },
                { 
"three""3""very" }, { "four""4""nice" },
                { 
"five""5""swing" }, { "six""6""is" },
                { 
"seven""7""very" }, { "eight""8""nice" },
                { 
"nine""9""swing" }, { "ten""10""is" } };
        Object headers[] 
= { "col1""col2""col3" };
        table 
= new JTable(data, headers);
        table.setPreferredSize(
new Dimension(500300));

        JPanel panel 
= new JPanel();
        panel.setLayout(
new BorderLayout());
        JScrollPane scrollPane 
= new JScrollPane(table);
        scrollPane
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setBorder(
new EmptyBorder(0000));
        panel.setBorder(
new EmptyBorder(0000));
        panel.add(scrollPane, BorderLayout.CENTER);

        JPanel hPanel 
= new JPanel(new BorderLayout());

        hScrollBar.setOrientation(JScrollBar.HORIZONTAL);
        
int visibleAmount = 100;
        hScrollBar.setVisibleAmount(visibleAmount);
        hPanel.add(hScrollBar, BorderLayout.CENTER);

        JLabel vConerlabel 
= new JLabel();
        vConerlabel.setMinimumSize(
new Dimension(1510));
        vConerlabel.setPreferredSize(
new Dimension(1510));
        vConerlabel.setBorder(
new EmptyBorder(0000));
        hPanel.add(vConerlabel, BorderLayout.EAST);

        vScrollBar.addAdjustmentListener(
this);
        
int hVisibleAmount = 60;
        vScrollBar.setVisibleAmount(hVisibleAmount);
        vScrollBar.setUnitIncrement(
60);
        vScrollBar.setValueIsAdjusting(
true);

        vScrollBar.addMouseWheelListener(
new MouseWheelListener() {
            
public void mouseWheelMoved(MouseWheelEvent e) {
                vScrollBar.setValue(vScrollBar.getValue()
                        
+ e.getWheelRotation() * 60);
            }
        });
        hScrollBar.addAdjustmentListener(
this);
        hScrollBar.addMouseWheelListener(
new MouseWheelListener() {
            
public void mouseWheelMoved(MouseWheelEvent e) {
                hScrollBar.setValue(hScrollBar.getValue()
                        
+ e.getWheelRotation() * 60);
            }
        });

        
this.add(hPanel, BorderLayout.SOUTH);
        
this.add(panel, BorderLayout.CENTER);
        
this.add(vScrollBar, BorderLayout.EAST);
    }

    
public void adjustmentValueChanged(AdjustmentEvent e) {
        
if (e.getSource() == vScrollBar) {
            scrollToTableV(table, e.getValue());

        } 
else if (e.getSource() == hScrollBar) {
            scrollToTableH(table, e.getValue());
        }
    }

    
public void scrollToTableH(JTable table, int value) {
        
if (!(table.getParent() instanceof JViewport)) {
            
return;
        }
        JViewport viewport 
= (JViewport) table.getParent();
        Rectangle viewRect 
= viewport.getViewRect();
        Rectangle rect 
= new Rectangle(value * table.getWidth() / 90,
                (
int) viewRect.getY(), 1010);

        rect.setLocation(rect.x 
- viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    }

    
public void scrollToTableV(JTable table, int value) {
        
if (!(table.getParent() instanceof JViewport)) {
            
return;
        }
        JViewport viewport 
= (JViewport) table.getParent();
        Rectangle viewRect 
= viewport.getViewRect();
        Rectangle rect 
= new Rectangle((int) viewRect.getX(), value
                
* table.getHeight() / 901010);

        rect.setLocation(rect.x 
- viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    }

}

posted on 2007-03-31 01:37 azure 阅读(1888) 评论(2)  编辑  收藏

评论

# re: swing components-windows ScrollPane 2007-04-01 14:36 turbochen

这样做的意义何在?  回复  更多评论   

# re: swing components-windows ScrollPane[未登录] 2007-04-01 17:27 azure

研究一下而已,呵呵  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: