11. februar 2010 - 10:09
Der er
4 kommentarer
Focus på JTextField i et JPane
Jeg kan ikke få fokus på mit textfield, i det JPane som vises når applikationen startes.
// Must be created, before the button is added to the Panel
btnAdd = new JButton("OK");
inEAN = new EANTextField(btnAdd);
inEAN.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
inEAN.setText("");
focusEAN();
.....
public void focusEAN() {
inEAN.requestFocusInWindow();
}
Håber der er nogen der har noget input.
På forhånd tak.
//DFH
12. februar 2010 - 10:50
#2
Hej Arne. Det har jeg prøvet, men uden held.
Kan lige poste hele koden for klassen:
public class WindowExpedition extends JPanel implements ActionListener {
private static final long serialVersionUID = -292946335325221082L;
// Members
private boolean stateReturn = false;
// UI Components
private JTable table = null;
private InventoryTransactions inv = null;
private JButton btnCancel = null;
private JTextField inEAN = null;
private JButton btnAdd = null;
private JButton btnReturn = null;
private void handleAddTransaction() {
String ean = inEAN.getText().trim();
// Fix problem, where scanner sends two CR after scan resulting in two scans
// one for the original EAN and one with an empty EAN
if (ean.length() == 0) {
focusEAN();
return;
}
while (ean.length() < 13)
ean = "0" + ean;
if (ean.length() > 13) {
String msg = "Den angivne EAN er ikke gyldig. EAN kan højst være 13 tegn.";
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), msg, "Advarsel!",
JOptionPane.ERROR_MESSAGE);
focusEAN();
return;
}
try {
Inventory item = Inventory.get(ean);
// Item was not found. Prompt the user to enter information about the item
if (item == null) {
new NewItemDialog(JOptionPane.getFrameForComponent(this), ean);
item = Inventory.get(ean);
}
// Now, we have found the item scanned
if (item != null) {
if (stateReturn == false) {
// This is a sales transaction
inv.insert(item, 1);
item.decrement();
}
else {
// This is a return transaction
inv.insert(item, -1);
item.increment();
handleReturn();
}
revalidate();
table.setRowSelectionInterval(0, 0);
} else {
// Item not found
getToolkit().beep();
String msg = "Ukendt VMI vare.";
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(this), msg, "Advarsel!", JOptionPane.ERROR_MESSAGE);
}
}
catch (SQLException e) {
e.printStackTrace();
}
inEAN.setText("");
focusEAN();
}
private void handleCancel() {
int row = table.getSelectedRow();
if (row >= 0) {
// Confirm cancel transaction
int result = JOptionPane.showOptionDialog(JOptionPane.getFrameForComponent(this),
"Ønsker du annullere den valgte ekspedition?",
"Annuller ekspedition",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
null,
null);
// Remove transaction
if ( result == JOptionPane.YES_OPTION ) {
try {
String ean = inv.getEAN(row);
int qty = inv.getCount(row);
if (inv.delete(row)) {
Inventory item = Inventory.get(ean);
if (qty == 1)
item.increment();
else if (qty == -1)
item.decrement();
else
RemoteLogger.getInstance().logMessage("Unknown quantity from transaction: " +
ean + " / " + qty);
revalidate();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
focusEAN();
}
public void focusEAN() {
inEAN.requestFocusInWindow();
}
private void handleReturn() {
// Toggle state of the Return button
stateReturn ^= true;
if (stateReturn) {
btnReturn.setForeground(Color.RED);
btnReturn.setFont(btnReturn.getFont());
}
else {
btnReturn.setForeground(btnAdd.getForeground());
btnReturn.setFont(btnAdd.getFont());
}
focusEAN();
}
public WindowExpedition() throws SQLException {
setLayout(new BorderLayout());
// Transaction History
JPanel transactions = new JPanel();
transactions.setLayout(new BoxLayout(transactions, BoxLayout.PAGE_AXIS));
transactions.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Dagens transaktioner"),
BorderFactory.createEmptyBorder(5,5,5,5)));
transactions.setSize(transactions.getMaximumSize());
// Add inventory transaction
inv = new InventoryTransactions();
table = new JTable(inv);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
for (int i=0; i<table.getColumnCount(); i++)
table.getColumnModel().getColumn(i).setPreferredWidth(inv.getColumnWidth(i));
table.setFillsViewportHeight(true);
table.setDefaultRenderer(Date.class, new DateCellRender());
transactions.add(scrollPane);
// Add cancel button
JPanel cancelPane = new JPanel();
cancelPane.setLayout(new BoxLayout(cancelPane, BoxLayout.LINE_AXIS));
cancelPane.add(Box.createHorizontalGlue());
btnCancel = new JButton("Annuller");
btnCancel.setMnemonic(KeyEvent.VK_A);
btnCancel.addActionListener(this);
cancelPane.add(btnCancel);
cancelPane.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
transactions.add(cancelPane);
// New transaction
JPanel transaction = new JPanel();
transaction.setLayout(new BoxLayout(transaction, BoxLayout.LINE_AXIS));
transaction.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Ekspedition"),
BorderFactory.createEmptyBorder(5,5,5,5)));
JLabel ean = new JLabel("EAN");
transaction.add(ean);
transaction.add(Box.createHorizontalStrut(5));
// Must be created, before the button is added to the Panel
btnAdd = new JButton("OK");
inEAN = new EANTextField(btnAdd);
inEAN.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
inEAN.setText("");
focusEAN();
transaction.add(inEAN);
transaction.add(Box.createHorizontalStrut(5));
transaction.add(Box.createHorizontalGlue());
transaction.add(Box.createHorizontalStrut(5));
btnAdd.addActionListener(this);
transaction.add(btnAdd);
transaction.add(Box.createHorizontalStrut(5));
btnReturn = new JButton("Retur");
btnReturn.setToolTipText("Marker før returnering af vare");
btnReturn.addActionListener(this);
transaction.add(btnReturn);
// Add content to pane
add(transactions);
add(transaction, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == btnAdd)
handleAddTransaction();
else if (source == btnReturn)
handleReturn();
else if (source == btnCancel)
handleCancel();
}
}
18. februar 2010 - 09:11
#3
Jeg har nu prøvet at lave en InvokeLater metode på focusEAN()
public void focusEAN() {
Runnable doWorkRunnable = new Runnable() {
public void run() { inEAN.requestFocusInWindow(); }
};
SwingUtilities.invokeLater(doWorkRunnable);
}
_________
Tekstfeltet får nu fokus ved programmets opstart, men skifter jeg til et andet pane og tilbage til WindowExpedition, forsvinder fokus igen.
Nogle idéer?
På forhånd tak.
//DFH