Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable enter key

Tags:

java

swing

jtable

I am developing an application using jTable for inventory management.

The action is, by typing the item code in a jTextField and by pressing Enter key, the details of that code should come to jTable. And there I have to type the quantity and press Enter to calculate the amount. But now by giving item code the details come to the jTable, and I can type the quantity, but there by pressing Enter key jTable focus goes to the next row and no calculation is being done. But by again pressing Enter key on the jTextField the last entered amount is getting calculated. I don't know how to solve this problem as I am a beginner in Java. I am using MySQL and Java in Netbeans.

I am giving that code below..

Thank You..

jTable1.editCellAt(serialNumber, 2);
jTable1.getCellSelectionEnabled();

value1 = new Double(jTable1.getValueAt(serialNumber, 2).toString());
value = new Double(jTable1.getValueAt(serialNumber, 3).toString());
double result = value1 * value;

jTable1.setValueAt(result, serialNumber, 4);
like image 723
Luna Avatar asked Feb 01 '12 05:02

Luna


People also ask

What is the use of JTable in Java?

The JTable is used to display and edit regular two-dimensional tables of cells. See How to Use Tables in The Java Tutorial for task-oriented documentation and examples of using JTable .

What is JTable renderer?

A JTable automatically creates renderers for cells which can be called as default renderers. These renderers are based on the data type the cell displays. For example, for a numeric column, JTable creates renderers which display the value as right-aligned.

What is JTable in advanced Java?

The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.


1 Answers

You can use Java 8 lambda functions:

final String tustakmaad = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        panel.getTblBelgetarihiliste().getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
            enter, tustakmaad);
        panel.getTblBelgetarihiliste().getActionMap().put(tustakmaad, new DelegateAction(
            ae -> eventMytable_enterkey()));

and DelegateAction class should be like this:

package com.ozpas.entegre.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;

public class DelegateAction extends AbstractAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    ActionListener myaction = (ae) -> {
        System.out.println("empty action");
    };

    public DelegateAction(ActionListener customaction) {
        this.myaction = customaction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        myaction.actionPerformed(e);
    }

    public ActionListener getMyaction() {
        return myaction;
    }

    public void setMyaction(ActionListener myaction) {
        this.myaction = myaction;
    }

}
like image 106
engtuncay Avatar answered Oct 11 '22 13:10

engtuncay