2017-11-18 210 views
0

光标在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第一行闪烁,准备好输入而无需双击它。

有关如何实现此目的的任何建议?

+2

看一看['的JTable#editCellAt'](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html# editCellAt-int-int-) – MadProgrammer

+0

感谢MadProgrammer,我找到了解决方案。我在这里发布了新的问题https://stackoverflow.com/questions/47447780/key-binding-in-jtable-editor。其键绑定绑定到表编辑器而不是表。希望你也能帮忙。 – Hendra

回答

0

的基础是:

table.changeSelection(0, 0, false, false); 

if (table.editCellAt(0, 0)) 
{ 
    Component editor = table.getEditorComponent(); 
    editor.requestFocusInWindow(); 
    //((JTextComponent)editor).selectAll(); 
} 

changeSelection(...)就像点击行(所以整行被高亮显示),然后editCellAt(...)地方在编辑模式下的单元格。

然后,您需要将重点放在编辑器上,并可以选择所有文本,以便在键入时替换它。

编辑:

光标仍然不闪烁

裹的代码在SwingUtilities.invokeLater(...)以确保代码被执行后的帧是可见:

SwingUtilities.invokeLater(new Runnable() 
{ 
    public void run() 
    { 
     table.changeSelection(0, 1, false, false); 

     if (table.editCellAt(0, 1)) 
     { 
      Component editor = table.getEditorComponent(); 
      editor.requestFocusInWindow(); 
      //((JTextComponent)editor).selectAll(); 
     } 
    } 
}); 
+0

感谢你Camickr的快速回复。 – Hendra

+0

谢谢Camickr的快速回复。我已经尝试过您的解决方案,只要我运行它就可以立即输入(无需双击)。 Tho光标在第0行col 0上仍然不闪烁,但我认为现在可以。我的目标是开发一种新的软件,如收银员在超市中使用的POS软件,他们的桌子随时可以在col 0上闪烁的光标输入。对于那个先生/夫人/女士Camickr可能吗? – Hendra

+0

@Hendra,见编辑。 – camickr

0

是的,添加的代码会使光标在所选单元格上闪烁,使其准备好输入。

非常感谢你Camickr。

我把可运行的代码放在下面,以防万一有人遇到同样的情况。

import java.awt.Component;

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.SwingUtilities;

import javax.swing。JTable中;

公共类测试延伸的JFrame {

public static void main(String[] args) { 
      EventQueue.invokeLater(new Runnable() { 
        public void run() { 
          try { 
           test frame = new test(); 
           frame.setVisible(true); 
           frame.setLocationRelativeTo(null); //make frame center of screen     
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
        } 
      }); 
    } 


    public test() { 
      //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); 
      //make cursor blinking on selected cell 
      SwingUtilities.invokeLater(new Runnable(){ 
        public void run() { 
          table.changeSelection(0, 0, false, false); 
          if (table.editCellAt(0, 0)){ 
           Component editor = table.getEditorComponent(); 
           editor.requestFocusInWindow(); 
           //((JTextComponent)editor).selectAll(); 
          } 
        } 
      }); 

    } 

//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;} 

//公共字符串getColumnName(INT COL){返回COLUMNNAMES [COL];} 公共对象getValueAt(INT行,INT COL){返回数据[行 //自动生成表格:字符串=左对齐,数字=右对齐,复选框=复选框不是真/假 public class getColumnClass(int c){return getValueAt(0,c).getClass( );} // make table only only for first first column
public boolean isCellEditable(int row,int col){if(col == 0) {return true;} else {return false;}} // make table可以改变值
public void setValueAt(Object value,int row,int col){[row] [col] = value; fireTableCellUpdated(row,col); } }

}