我的textField在textField.setBounds(27, 60, 150, 25)
时可见,但我想在textField.setBounds(27, 120, 150, 25)
时看到它。如何将textField
放在JTable上(悬停在JTable之上)并且仍然可见?
这是我的代码:
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)
}
}