java中JTable标签怎么添加监听事件

java中JTable标签怎么添加监听事件,第1张

这个可以用MouseListener来监听

table.addMouseListener(....)

//括号内的东西根据实际填写

……

public

void

mouseClicked(MouseEvent

e)

{

int

index=table.getSelectedRow()

String

code=(String)table.getValueAt(index,0)//这一句是取出表格选中那行的第一列

text1.setText(code)

//其他列的数据类似,不逐一说明,自己研究即可。

}

鼠标事件要添加mouselistener,捕获mouseevent。mouselistener里的几个事件的参数都是mouseevent,mouseevent提供了获得点击了哪个键的方法getbutton。比如下面这个点击事件处理。

public

void

mouseclicked(mouseevent

arg0)

{

if(arg0.getbutton()

==

mouseevent.button1)

{

//

左键点击

}

else

if(arg0.getbutton()

==

mouseevent.button2)

{

//

中键点击

}

else

if(arg0.getbutton()

==

mouseevent.button3)

{

//

右键点击

}

}

添加监听是addmouselistener方法,取消监听当然就是removemouselistener方法了。

简单的例子:

import java.applet.*

import java.awt.*

import java.awt.event.*

public class TestPanel extends Applet implements ActionListener,ContainerListener

{

Panel p1,p2,p3

Label prompt1,prompt2,prompt3

Button button1

public void init()

{

setLayout(new BorderLayout())

p1=new Panel()

p1.setBackground(Color.gray)

p2=new Panel()

p2.setBackground(Color.red)

p3=new Panel()

p3.setBackground(Color.cyan)

button1=new Button("input 3panel")

button1.addActionListener(this)

prompt1=new Label("1panel")

prompt2=new Label("2panel")

prompt3=new Label("3panel")

p1.add(prompt1)

p2.add(prompt2)

p3.add(prompt3)

p3.add(button1)

p1.add(p3)

p1.addContainerListener(this)

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==button1)

p1.remove(p3)

}

public void componentRemoved(ContainerEvent e)

{

showStatus("移除3panel")

}

public void componentAdded(ContainerEvent e)

{

}

}

可以捕获监听器的内容,并进行处理,参数也可以为空,空代表什么都不执行。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11850402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存