public void setframe(Frame2 frame) { frame2=frame; } public void valueChanged(ListSelectionEvent e) { if(frame2==null){System.out.println("null");return;}
super.valueChanged(e);
int r1=getSelectedRow(); int c1=getSelectedColumn(); if(!frame2.IsSelected(r1,c1)) { //System.out.println("unselected"); setSelectionBackground(Color.white); frame2.set1(r1,c1,true); }
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Vector;
public class Frame2 extends JFrame {
String column[]={"name","sex","phone"};
String row[][]={{"name","sex","phone"},{"people1","Male","12234"},{"people2","Female","3423"},{"people2","Female","34435"}};
boolean date[][]=new boolean[100][100];
Vector vector=new Vector() ;
MyTable jTable1=new MyTable(row,column);
JButton Button1 = new JButton();
JButton Button2 = new JButton(); public Frame2() {
try {
MyInit();
}
catch(Exception e) {
e.printStackTrace();
}
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
Frame2 frame2 = new Frame2();
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void MyInit() throws Exception {
this.getContentPane().setLayout(null);
jTable1.setCellSelectionEnabled(true);
jTable1.setframe(this);
clear(); jTable1.setBounds(new Rectangle(28, 57, 350, 223));
Button1.setBounds(new Rectangle(59, 9, 81, 32));
Button1.setText("Button1");
Button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Button2.setEnabled(true);
Button1.setEnabled(false);
jTable1.clearSelection() ;
clear();
}
}
);
Button2.setBounds(new Rectangle(226, 13, 112, 28));
Button2.setText("Button2");
Button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
jTable1.clearSelection();
Button1.setEnabled(true);
Button2.setEnabled(false);
clear();
}
}
);
this.getContentPane().add(jTable1, null);
this.getContentPane().add(Button1, null);
this.getContentPane().add(Button2, null);
}
public void clear()
{
for(int i=0;i<date.length;i++)
for(int j=0;j<date[0].length;j++)
date[i][j]=false;
}
public void set1(int r1,int c1,boolean state)
{
date[r1][c1]=state;
if(state)
{
vector.add(jTable1.getValueAt(r1,c1));
Button1.setEnabled(false);
Button2.setEnabled(true);
}
}
public boolean IsSelected(int r1,int c1)
{
return date[r1][c1];
}
}class MyTable extends JTable
{
Frame2 frame2;
MyTable(Object[][] rowData, Object[] columnNames)
{
super(rowData,columnNames);
}
public boolean isCellEditable(int row, int column)
{
return false;
}
public void setframe(Frame2 frame)
{
frame2=frame;
}
public void valueChanged(ListSelectionEvent e)
{
if(frame2==null){System.out.println("null");return;}
super.valueChanged(e);
int r1=getSelectedRow();
int c1=getSelectedColumn();
if(!frame2.IsSelected(r1,c1))
{
//System.out.println("unselected");
setSelectionBackground(Color.white);
frame2.set1(r1,c1,true);
}
else
{
//System.out.println("selected");
setSelectionBackground(Color.blue);
frame2.set1(r1,c1,false);
}
}
}
等我在双击表格的其它某一cell时~
这一cell变蓝~
前面双击过的cell的颜色也是蓝色~
如果我双击蓝色的cell~此蓝色cell变回原色~这个~上面的~
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Vector;
public class Frame2 extends JFrame {
String column[]={"name","sex","phone"};
String row[][]={{"name","sex","phone"},{"people1","Male","12234"},{"people2","Female","3423"},{"people2","Female","34435"}};
boolean date[][]=new boolean[100][100];
Vector vector=new Vector() ;
MyTable jTable1=new MyTable(row,column);
JButton Button1 = new JButton();
JButton Button2 = new JButton(); public Frame2() {
try {
MyInit();
}
catch(Exception e) {
e.printStackTrace();
}
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
Frame2 frame2 = new Frame2();
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void MyInit() throws Exception {
this.getContentPane().setLayout(null);
jTable1.setCellSelectionEnabled(true);
jTable1.setframe(this);
clear(); jTable1.setBounds(new Rectangle(28, 57, 350, 223));
Button1.setBounds(new Rectangle(59, 9, 81, 32));
Button1.setText("Button1");
Button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Button2.setEnabled(true);
Button1.setEnabled(false);
try{
jTable1.clearSelection() ;
}
catch(Exception ex)
{
ex.printStackTrace();
}
clear();
}
}
);
Button2.setBounds(new Rectangle(226, 13, 112, 28));
Button2.setText("Button2");
Button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Button1.setEnabled(true);
Button2.setEnabled(false);
try{
jTable1.clearSelection();
}
catch(Exception ex)
{
ex.printStackTrace();
}
clear();
}
}
);
this.getContentPane().add(jTable1, null);
this.getContentPane().add(Button1, null);
this.getContentPane().add(Button2, null);
}
public void clear()
{
for(int i=0;i<date.length;i++)
for(int j=0;j<date[0].length;j++)
date[i][j]=false;
}
public void set1(int r1,int c1,boolean state)
{
date[r1][c1]=state;
if(state)
{
if(!vector.contains(jTable1.getValueAt(r1,c1)))
vector.add(jTable1.getValueAt(r1,c1));
Button1.setEnabled(false);
Button2.setEnabled(true);
}
}
public boolean IsSelected(int r1,int c1)
{
return date[r1][c1];
}
}class MyTable extends JTable implements MouseListener
{
Frame2 frame2;
MyTable(Object[][] rowData, Object[] columnNames)
{
super(rowData,columnNames);
addMouseListener(this);
}
public boolean isCellEditable(int row, int column)
{
return false;
}
public boolean isCellSelected(int row, int column)
{
boolean m_boolean=super.isCellSelected(row,column);
if(frame2==null)return m_boolean;
boolean m_boolean2=frame2.IsSelected(row,column);
//System.out.println(m_boolean2);
return m_boolean2;
}
public void setframe(Frame2 frame)
{
frame2=frame;
}
public void mouseClicked(MouseEvent e)
{
if(frame2==null){return;}
int r1=getSelectedRow();
int c1=getSelectedColumn();
if(!isCellSelected(r1,c1))
{
//System.out.println("unselected");
frame2.set1(r1,c1,true);
setSelectionBackground(Color.blue);
repaint();
}
else
{
//System.out.println("selected");
frame2.set1(r1,c1,false);
setSelectionBackground(Color.blue);
repaint();
}
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}}
谢谢,我看看
{
if(frame2==null){return;}
int r1=getSelectedRow();
int c1=getSelectedColumn();
if(!isCellSelected(r1,c1))
{
//System.out.println("unselected");
frame2.set1(r1,c1,true);
setSelectionBackground(Color.blue);
repaint();
}
else
{
//System.out.println("selected");
frame2.set1(r1,c1,false);
setSelectionBackground(Color.blue);
repaint();
}
}
改为————————————————————————————
public void mouseClicked(MouseEvent e)
{
if(Frame1==null){return;}
if(e.getClickCount()==2) {
int r1=getSelectedRow();
int c1=getSelectedColumn();
if(!isCellSelected(r1,c1))
{
//System.out.println("unselected");
Frame1.set1(r1,c1,true);
setSelectionBackground(Color.blue);
repaint();
} else
{
//System.out.println("selected");
Frame1.set1(r1,c1,false);
setSelectionBackground(Color.blue);
repaint(); }}
}
if(frame2==null){return;}
后加上一句if(e.getClickCount()!=2)return;
这样就可以了。
if(Frame1==null||e.getClickCount!=2){return;}
最简单