光標在JTable
中有問題。 我試着在論壇上找到答案,但無法找到我期望的答案。 這是我的Java可運行:光標在jtable中自動閃爍,無需點擊鼠標
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTable;
public class Fpos extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Fpos frame = new Fpos();
frame.setVisible(true);
frame.setLocationRelativeTo(null); //make frame center of screen
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Fpos() {
//create Jpanel
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 1300, 700);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//create label TOTAL
JLabel lblTotal = new JLabel("TOTAL : Rp.");
lblTotal.setFont(new Font("Wide Latin", Font.PLAIN, 30));
lblTotal.setBounds(33, 25, 312, 31);
contentPane.add(lblTotal);
//create label Total Amount
JLabel lblNewLabel = new JLabel("123,456,789");
lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
lblNewLabel.setFont(new Font("Wide Latin", Font.PLAIN, 60));
lblNewLabel.setBounds(571, 6, 659, 61);
contentPane.add(lblNewLabel);
//create jtable in scrollpane
JTable table = new JTable(new MyTableModel());
JScrollPane sp=new JScrollPane(table);
sp.setBounds(20,76,1240,572);
contentPane.add(sp);
}
//tablemodel
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"PLU", "NAME", "UOM", "QTY", "PRICE","AMOUNT"};
private Object[][] data = {{"", "", "", new Double(0), new Integer(0), new Integer(0)}};
public int getColumnCount() {return columnNames.length;}
public int getRowCount() {return data.length;}
// public String getColumnName(int col) {return columnNames[col];}
public Object getValueAt(int row, int col) {return data[row][col];}
//auto formating table: string=left alignment, numeric=right alignment, checkbox=check box not true/false
public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
//make table editable only for just first column
public boolean isCellEditable(int row, int col) {if (col == 0) {return true;} else{return false;}}
//make table can change value
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
}
輸出僅僅是罰款,但該表沒有準備好輸入。 我必須雙擊列PLU的第一行才能輸入。 我想要的是隻要我運行它,光標在PLU第一行閃爍,準備好輸入而無需雙擊它。
有關如何實現此目的的任何建議?
看一看['的JTable#editCellAt'](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html# editCellAt-int-int-) – MadProgrammer
感謝MadProgrammer,我找到了解決方案。我在這裏發佈了新的問題https://stackoverflow.com/questions/47447780/key-binding-in-jtable-editor。其鍵綁定綁定到表編輯器而不是表。希望你也能幫忙。 – Hendra