HelloWorld 善战者,求之于势,不责于人;故能择人而任势。

知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得。物有本末,事有终始。知所先后,则近道矣。

  BlogJava :: 首页 ::  :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 40 评论 :: 0 Trackbacks

这个例子是JTable可以选择行和列,界面如图


代码如下

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class A7_SelectionModel extends JFrame {
    
public A7_SelectionModel() {
        
super("Selection Model Test");
        setSize(
450350);
        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        TableModel m 
= new AbstractTableModel() {
            
public int getColumnCount() {
                
return 10;
            }

            
public int getRowCount() {
                
return 10;
            }

            
public Object getValueAt(int r, int c) {
                
return "" + (r+1)*(c+1);
            }            
        };
        
        
final JTable jt = new JTable(m);
        
        
this.getContentPane().add(new JScrollPane(jt), BorderLayout.CENTER);
        
        JPanel controlPanel, buttonPanel, columnPanel, rowPanel;
        
        buttonPanel 
= new JPanel();
        
final JCheckBox cellBox, columnBox, rowBox;
        cellBox 
= new JCheckBox("Cells", jt.getCellSelectionEnabled());
        columnBox 
= new JCheckBox("Columns", jt.getColumnSelectionAllowed());
        rowBox 
= new JCheckBox("Rows", jt.getRowSelectionAllowed());
        
        cellBox.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                jt.setCellSelectionEnabled(cellBox.isSelected());
                columnBox.setSelected(jt.getColumnSelectionAllowed());
                rowBox.setSelected(jt.getRowSelectionAllowed());
            }
        });
        columnBox.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                jt.setColumnSelectionAllowed(columnBox.isSelected());
                cellBox.setSelected(jt.getCellSelectionEnabled());
            }
        });
        rowBox.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                jt.setRowSelectionAllowed(rowBox.isSelected());
                cellBox.setSelected(jt.getCellSelectionEnabled());
            }
        });
        
        buttonPanel.add(
new JLabel("Selections allowed:"));
        buttonPanel.add(cellBox);
        buttonPanel.add(columnBox);
        buttonPanel.add(rowBox);
        
        columnPanel 
= new JPanel();
        ListSelectionModel csm 
= jt.getColumnModel().getSelectionModel();
        JLabel columnCounter 
= new JLabel("(Selected Column INdices Go Here)");
        csm.addListSelectionListener(
new SelectionDebugger(columnCounter, csm));
        columnPanel.add(
new JLabel("Selected columns:"));
        columnPanel.add(columnCounter);
        
        rowPanel 
= new JPanel();
        ListSelectionModel rsm 
= jt.getColumnModel().getSelectionModel();
        JLabel rowCounter 
= new JLabel("(Selected Row INdices Go Here)");
        rsm.addListSelectionListener(
new SelectionDebugger(columnCounter, csm));
        rowPanel.add(
new JLabel("Selected Rows:"));
        rowPanel.add(rowCounter);
        
        controlPanel 
= new JPanel(new GridLayout(01));
        controlPanel.add(buttonPanel);
        controlPanel.add(columnPanel);
        controlPanel.add(rowPanel);
        
        
this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
        
    }
    
class SelectionDebugger implements ListSelectionListener  {
        JLabel debugger;
        ListSelectionModel model;
        
public SelectionDebugger(JLabel l, ListSelectionModel m) {
            
this.debugger = l;
            
this.model = m;
        }
        
public void valueChanged(ListSelectionEvent e) {
            
if (!e.getValueIsAdjusting()) {
                StringBuffer buf 
= new StringBuffer();
                
int[] selecton = getSelectonIndices(model.getMinSelectionIndex(),
                        model.getMaxSelectionIndex());
                
if (selecton.length == 0) {
                    buf.append(
"NONE");
                } 
else {
                    
for (int i = 0; i < selecton.length - 1; i++) {
                        buf.append(selecton[i]);
                        buf.append(
",");
                    }
                    buf.append(selecton[selecton.length 
- 1]);
                }
                debugger.setText(buf.toString());
            }
        }
        
        
protected int[] getSelectonIndices(int start, int stop) {
            
if ((start == -1|| (stop == -1)) {
                
return new int[0];
            }
            
            
int guesses[] = new int[stop - start + 1];
            
int index = 0;
            
for (int i = start; i <= stop; i++) {
                
if (model.isSelectedIndex(i)) {
                    guesses[index
++= i;
                }
            }
            
int realthing[] = new int[index];
            System.arraycopy(guesses, 
0, realthing, 0, index);
            
return realthing;
        }
        
        
    }
    
public static void main(String args[]) {
        AbmUtils.setLookAndFeel();
        A7_SelectionModel s 
= new A7_SelectionModel();
        s.setVisible(
true);
    }
}


</script>

posted on 2008-02-29 16:32 helloworld2008 阅读(1305) 评论(0)  编辑  收藏 所属分类: java - swing

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


网站导航: