I try to get data in the row that is selected but getSelectedRow()
doesn't work. Actually, I used that method in the other class, but it works in there. When I try to print the row index; the prompt shows -1; as not selected.
我尝试在选中的行中获取数据,但是getSelectedRow()不起作用。实际上,我在另一个类中使用了这个方法,但是它在那里工作。当我尝试打印行索引时;提示显示1;不选中。
I tried most of solutions that are on the internet but they didn't solve my solutions.
我尝试了很多互联网上的解决方案,但并没有解决我的问题。
public Canteen() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout( null );
this.setSize( new Dimension(400, 300) );
this.setTitle( "CANTEEN" );
jScrollPane1.setBounds(new Rectangle(0, 0, 230, 235));
jButton1.setText("REFRESH");
jButton1.setBounds(new Rectangle(0, 235, 100, 20));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("CLOSE");
jButton2.setBounds(new Rectangle(120, 235, 110, 20));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
jTable1 = new JTable (model);
String header [] = {"CUSTOMER", "PRODUCT", "QUANTITY","ORDER NO"};
for (int i = 0; i < 4; i++){
model.addColumn(header[i]);
}
//refresh every 1 minute
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
model.setRowCount(0);
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,username, password);
statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT USER_NAME, QUANTITY, PRD_NAME, ORDER_NUMBER FROM ORDERS ORDER BY ORDER_NUMBER DESC");
int count = rs.getRow();
String user [] = new String [count];
int i = 0;
while (rs.next()){
String name = rs.getString(1);
String prdName = rs.getString(3);
int number = rs.getInt(2);
int orderNumber = rs.getInt(4);
model.insertRow(i,new Object []{name,prdName,number, orderNumber});
}
conn.close();
}
catch (ClassNotFoundException s) {
System.out.println("Couldn't find the database driver");
System.out.println(s);
}
catch (SQLException s) {
System.out.println("Couldn't connect to the database\n" +s);
}
}
},0, 60000);
}
private void jButton1_actionPerformed(ActionEvent e) {
//set from row 0 for refresh
model.setRowCount(0);
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,username, password);
statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT USER_NAME, QUANTITY, PRD_NAME FROM ORDERS ORDER BY ORDER_NUMBER DESC");
int count = rs.getRow();
String user [] = new String [count];
int i = 0;
while (rs.next()){
String name = rs.getString(1);
String prdName = rs.getString(3);
int number = rs.getInt(2);
model.insertRow(i,new Object []{name,prdName,number});
}
conn.close();
}
catch (ClassNotFoundException s) {
System.out.println("Couldn't find the database driver");
System.out.println(s);
}
catch (SQLException s) {
System.out.println("Couldn't connect to the database\n" +s);
}
}
private void jButton2_actionPerformed(ActionEvent e) {
System.out.println(jTable1.getSelectedRow());
}
}
2 个解决方案
#1
4
Look to your code
看你的代码
private void jbInit() throws Exception {
...
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
jTable1 = new JTable (model);
...
You add jTable1
to the JScrollPane
at first and only then you create jTable1
. It's incorrect way. jTable1
variable doen't linked now with table you place at the form. I think it's cause of your problem.
首先在JScrollPane中添加jTable1,然后才创建jTable1。这是不正确的。jTable1变量现在没有链接到表单中。我认为这是你问题的原因。
Move creation of the jTable1
before adding in into JScrollPane
.
在添加到JScrollPane之前,移动jTable1的创建。
...
jTable1 = new JTable (model);
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
...
#2
4
As your other example shows, getSelectedRow()
does work, so you're going to have to debug your code. Comparison with the sscce below may suggest way forward. Both the ActionListener
and the ListSelectionListener
show the selected row.
如您的其他示例所示,getSelectedRow()是有效的,因此您将不得不调试代码。与下面的sscce比较,可能会有进一步的建议。ActionListener和ListSelectionListener都显示所选的行。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
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.DefaultTableModel;
/**
* @see http://*.com/q/12301923/230513
*/
public class TableSelection extends JPanel {
private static final String SHOW = "Show";
private DefaultTableModel model = new DefaultTableModel();
private JTable table = new JTable(model);
private JButton button = new JButton(new AbstractAction(SHOW) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(table.getSelectedRow());
}
});
public TableSelection() {
model.addColumn("Column");
for (int i = 0; i < 16; i++) {
model.addRow(new Object[]{i});
}
table.setPreferredScrollableViewportSize(new Dimension(160, 100));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
button.setText(SHOW + " " + table.getSelectedRow());
}
}
});
this.add(new JScrollPane(table));
table.setRowSelectionInterval(3, 3);
}
private void display() {
JFrame f = new JFrame("TableSelection");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this, BorderLayout.CENTER);
f.add(button, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TableSelection().display();
}
});
}
}
#1
4
Look to your code
看你的代码
private void jbInit() throws Exception {
...
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
jTable1 = new JTable (model);
...
You add jTable1
to the JScrollPane
at first and only then you create jTable1
. It's incorrect way. jTable1
variable doen't linked now with table you place at the form. I think it's cause of your problem.
首先在JScrollPane中添加jTable1,然后才创建jTable1。这是不正确的。jTable1变量现在没有链接到表单中。我认为这是你问题的原因。
Move creation of the jTable1
before adding in into JScrollPane
.
在添加到JScrollPane之前,移动jTable1的创建。
...
jTable1 = new JTable (model);
jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
...
#2
4
As your other example shows, getSelectedRow()
does work, so you're going to have to debug your code. Comparison with the sscce below may suggest way forward. Both the ActionListener
and the ListSelectionListener
show the selected row.
如您的其他示例所示,getSelectedRow()是有效的,因此您将不得不调试代码。与下面的sscce比较,可能会有进一步的建议。ActionListener和ListSelectionListener都显示所选的行。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
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.DefaultTableModel;
/**
* @see http://*.com/q/12301923/230513
*/
public class TableSelection extends JPanel {
private static final String SHOW = "Show";
private DefaultTableModel model = new DefaultTableModel();
private JTable table = new JTable(model);
private JButton button = new JButton(new AbstractAction(SHOW) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(table.getSelectedRow());
}
});
public TableSelection() {
model.addColumn("Column");
for (int i = 0; i < 16; i++) {
model.addRow(new Object[]{i});
}
table.setPreferredScrollableViewportSize(new Dimension(160, 100));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
button.setText(SHOW + " " + table.getSelectedRow());
}
}
});
this.add(new JScrollPane(table));
table.setRowSelectionInterval(3, 3);
}
private void display() {
JFrame f = new JFrame("TableSelection");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this, BorderLayout.CENTER);
f.add(button, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TableSelection().display();
}
});
}
}