展开全部
public javax.swing.JTable sNameTable;//定义table
private DefaultTableModel tableModel = new JTableUtil.DefaultTableModel();//定义Model
UpctableModel.setColumnIdentifiers(new String[] {"ID" ,"NAME" ,"AGE"});//定义表头
sNameTable.setModel(tableModel);//设置表格tableModel
//添加值
tableModel .addRow(new Object[]{"1","CC","20"});
tableModel .addRow(new Object[]{"2","DD","21"});
tableModel .addRow(new Object[]{"3","DD","22"});
int row = sNameTable.getSelectedRow();//取得表格选中行
int sID = tableModel .getValueAt(row, 2).toString());//取得表格选中行的AGE字段,如果选中第三行则,sID = "22"(表格从0标签开始).
谢谢,纯手敲,望采纳!
private DefaultTableModel tableModel = new JTableUtil.DefaultTableModel();//定义Model
UpctableModel.setColumnIdentifiers(new String[] {"ID" ,"NAME" ,"AGE"});//定义表头
sNameTable.setModel(tableModel);//设置表格tableModel
//添加值
tableModel .addRow(new Object[]{"1","CC","20"});
tableModel .addRow(new Object[]{"2","DD","21"});
tableModel .addRow(new Object[]{"3","DD","22"});
int row = sNameTable.getSelectedRow();//取得表格选中行
int sID = tableModel .getValueAt(row, 2).toString());//取得表格选中行的AGE字段,如果选中第三行则,sID = "22"(表格从0标签开始).
谢谢,纯手敲,望采纳!
展开全部
选择行 点一下按钮试一下。
-----------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class App extends JFrame implements ActionListener {
private JTable table;
private DefaultTableModel model = null;
private String[] columnnames = { "no", "name" };
private String[][] data = { { "1", "name1" }, { "2", "name2" },
{ "3", "name3" } };
public App() {
getContentPane().setLayout(null);
JButton btnNewButton = new JButton("change");
btnNewButton.setBounds(12, 10, 93, 23);
btnNewButton.addActionListener(this);
getContentPane().add(btnNewButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 43, 428, 270);
getContentPane().add(scrollPane);
model = new DefaultTableModel(data, columnnames);
table = new JTable(model);
scrollPane.setViewportView(table);
setSize(460, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
System.out.println(table.getValueAt(row, 0));
System.out.println(table.getValueAt(row, 1));
}
}
-----------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class App extends JFrame implements ActionListener {
private JTable table;
private DefaultTableModel model = null;
private String[] columnnames = { "no", "name" };
private String[][] data = { { "1", "name1" }, { "2", "name2" },
{ "3", "name3" } };
public App() {
getContentPane().setLayout(null);
JButton btnNewButton = new JButton("change");
btnNewButton.setBounds(12, 10, 93, 23);
btnNewButton.addActionListener(this);
getContentPane().add(btnNewButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 43, 428, 270);
getContentPane().add(scrollPane);
model = new DefaultTableModel(data, columnnames);
table = new JTable(model);
scrollPane.setViewportView(table);
setSize(460, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
System.out.println(table.getValueAt(row, 0));
System.out.println(table.getValueAt(row, 1));
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在需要的地方加入如下代码:
Java代码
IViewPart vp = getViewSite().getPage().findView(YOUR_VIEW_ID);
TreeViewer viewer = ((YOUR_VIEW)vp).getTreeViewer();
Object o = ((IStructuredSelection)viewer.getSelection()).getFirstElement();
NodeEntity nodeWanted = (NodeEntity) o;
IViewPart vp = getViewSite().getPage().findView(YOUR_VIEW_ID);
TreeViewer viewer = ((YOUR_VIEW)vp).getTreeViewer();
Object o = ((IStructuredSelection)viewer.getSelection()).getFirstElement();
NodeEntity nodeWanted = (NodeEntity) o;
YOUR_VIEW_ID是指你的视图类YOUR_VIEW在工程中的ID号。
思路:首先获取一个IViewPar接口t,然后转换成自己的视图类。再通过getSelection()).getFirstElement();
得到所需的这个实体。
Java代码
IViewPart vp = getViewSite().getPage().findView(YOUR_VIEW_ID);
TreeViewer viewer = ((YOUR_VIEW)vp).getTreeViewer();
Object o = ((IStructuredSelection)viewer.getSelection()).getFirstElement();
NodeEntity nodeWanted = (NodeEntity) o;
IViewPart vp = getViewSite().getPage().findView(YOUR_VIEW_ID);
TreeViewer viewer = ((YOUR_VIEW)vp).getTreeViewer();
Object o = ((IStructuredSelection)viewer.getSelection()).getFirstElement();
NodeEntity nodeWanted = (NodeEntity) o;
YOUR_VIEW_ID是指你的视图类YOUR_VIEW在工程中的ID号。
思路:首先获取一个IViewPar接口t,然后转换成自己的视图类。再通过getSelection()).getFirstElement();
得到所需的这个实体。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询