问题
My textField is visible when textField.setBounds(27, 60, 150, 25)
but I want it visible when textField.setBounds(27, 120, 150, 25)
. How to put textField
over JTable (hovering on top of JTable) and still visible?
Here is my code:
import java.awt.EventQueue;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import java.awt.Color;
public class Fpos extends JFrame
{
private JTextField textField;
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 lbl1 = new JLabel("TOTAL : Rp.");
lbl1.setBounds(33, 25, 312, 31);
lbl1.setFont(new Font("Wide Latin", Font.PLAIN, 30));
contentPane.add(lbl1);
//create label Total Amount
JLabel lblTotal = new JLabel("123,456,789");
lblTotal.setBounds(583, 19, 659, 61);
lblTotal.setHorizontalAlignment(SwingConstants.RIGHT);
lblTotal.setFont(new Font("Wide Latin", Font.PLAIN, 60));
contentPane.add(lblTotal);
//create jtable in scrollpane
String[] columnNames = {"PLU", "NAME", "UOM", "QTY", "PRICE","AMOUNT"};
Object[][] data = {{"", "", "", "", "", ""}};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model);
table.setFont(new Font("Tahoma", Font.PLAIN, 20));
table.setRowHeight(25);
JScrollPane sp=new JScrollPane(table);
sp.setBounds(25, 100, 1240, 556);
contentPane.add(sp);
//set column width
TableColumnModel columnModel = table.getColumnModel();
short a[] = {150,540,50,150,150,200};
for(byte i=0;i<6;i++) { columnModel.getColumn(i).setPreferredWidth(a[i]); }
//virtual table cell editor
textField = new JTextField();
textField.setBackground(Color.CYAN);
textField.setBounds(27, 60, 150, 25);
contentPane.add(textField);
textField.requestFocus();
//keybinding bound to texField starting here
//keybinding for enter key
Action enter = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
/* It'll be quite lot of code here later, let me summarize them:
* 1. query from database with textfield value
* 2. setvalue table from resultset
* 3. sum all value from column 5 and set its value to lblTotal
* 4. adding row
* 5. move textfield over new row column 0 of the jtable (new item is ready for input by barcode scanner again)
*/
//add new blank row at last row
model.addRow(data);
model.setValueAt("", model.getRowCount()-1, 0);
}
};
textField.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_ENTER), "enter");
textField.getActionMap().put("enter", enter);
//keybinding for up key
Action up = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
//move textfield to row-1 column 0
}
};
textField.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_UP), "up");
textField.getActionMap().put("up", up);
//keybinding for down key
Action down = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
//move textfield to row+1 column 0
}
};
textField.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_DOWN), "down");
textField.getActionMap().put("down", down);
//keybinding for F3 key
Action f3 = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
//delete current row (delete item) and recalculate for lblTotal
}
};
textField.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE), "f3");
textField.getActionMap().put("f3", f3);
//keybinding for F1 key, show new frame/jpane for cashier search item by name when item code (textfield) not available
//keybinding for F9 key, show new frame/jpane for payment method whether cash or card
//keybinding for F7 key, show new frame/jpane for reprint history transaction by input transaction id (with new textfield)
}
}
回答1:
With a little more thought, I think all you need to do is replace the default key binding on the editor with a key binding of your own:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableEnter extends JPanel
{
public TableEnter()
{
JTable table = new JTable(5, 5);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
Action enterAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
JTextField textField = (JTextField)e.getSource();
JTable table = (JTable)textField.getParent();
if (table.isEditing())
table.getCellEditor().stopCellEditing();
// add your custom logic here
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
System.out.println(row + " : " + column);
}
};
DefaultCellEditor editor = (DefaultCellEditor)table.getDefaultEditor(Object.class);
JTextField textField = (JTextField)editor.getComponent();
final String enterKey = "VK_ENTER";
KeyStroke enter = KeyStroke.getKeyStroke("pressed ENTER");
textField.getInputMap().put(enter, enterKey);
textField.getActionMap().put(enterKey, enterAction);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new TableEnter() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
来源:https://stackoverflow.com/questions/47505423/text-field-and-keybinding