Hot questions for Using JTextField in jtable

Question:

How can I set caret position in a JTable?

I know JTextField has setCaretPosition(n) function. But I cannot access JTextField in the JTable.

I would like the Table text caret position equal text length. It is possible to mouseclick event but it should be normal position.

My code:

public class TableTest extends javax.swing.JFrame
{

    public TableTest()
    {
        javax.swing.JTable jTable1 = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]
                {
                    {
                        "This is too long text!", "This is too long text!",
                    },
                    {
                        "This is too long text!", "This is too long text!",
                    }
                },
                new String[]
                {
                    "Title 1", "Title 2",
                }
        )); 

        add(jTable1);

        pack();

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLocationRelativeTo(null);
    }
    public static void main(String args[])
    {
        new TableTest().setVisible(true);
    }

}

This table cells are shown: [This is too...] but it should be [..long text!]


Answer:

This table cells are shown: [This is too...] but it should be [..long text!]

This the renderer that displays the text in a cell. The default renderer is a JLabel and it will display ... when the text is truncated. If you want the ... at the start of the cell then you need to create a custom renderer.

Check out the Left Dot Renderer for a renderer that does this.

Question:

I need to validate a JTextField by allowing the user to input only cnic number according to this format 12345-1234567-1, i am using this regular expression but it is not worked. this is my function

private void idSearchKeyPressed(java.awt.event.KeyEvent evt) {
    String cnicValidator = idSearch.getText();

    if (cnicValidator.matches("^[0-9+]{5}-[0-9+]{7}-[0-9]{1}$")) {
        idSearch.setEditable(true);
    }
    else {
        idSearch.setEditable(false);
    }        
}

kindly provide me some guidence how to validate my jtextfield...thanks


Answer:

An alternative solution to validating post-input would be to use a JFormattedTextField combined with a MaskFormatter to restrict the input to begin with.

The code to create one of these would be something along the lines of:

import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.text.MaskFormatter;

public class Demo {

    public static void main(String[] args){

        final JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final MaskFormatter mask;
        try {
            mask = new MaskFormatter("#####-#######-#");
        } catch (ParseException e) {
            throw new RuntimeException("Invalid format mask specified", e);
        }

        // You can optionally set a placeholder character by doing the following:
        mask.setPlaceholderCharacter('_');
        final JFormattedTextField formattedField = new JFormattedTextField(mask);

        frame.setSize(100, 100);
        frame.add(formattedField);
        frame.setVisible(true);
    }
}

The format mask # only accepts digits (specifically, it accepts any character where Character.isDigit(char) returns true), and forbids any non-digit from being entered.

Question:

I have Implemented an AutoSuggest for JTextField.It's working fine for JTextField.But When I try to use it for JTable CellEditor.It's throwing

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
    at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2048)
    at java.awt.Component.getLocationOnScreen(Component.java:2022)
    at javax.swing.JPopupMenu.show(JPopupMenu.java:942)

AutoSuggestTable.java

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
 *
 * @author Phystem
 * @param <T>
 */

public class AutoSuggestTable<T> extends JTextField {

private List<T> items;

private final JComboBox combobox = new JComboBox();

private final DefaultComboBoxModel comboBoxModel;

private final BasicComboPopup suggestPopup;

public AutoSuggestTable() {
    this(new ArrayList<T>());
}

public AutoSuggestTable(List<T> items) {
    this.items = items;
    Collections.sort(items, new Compare());
    comboBoxModel = new DefaultComboBoxModel();
    combobox.setModel(comboBoxModel);
    suggestPopup = new BasicComboPopup(combobox);
    install();
}

private void addToSuggestList(List<Object> list) {
    comboBoxModel.removeAllElements();
    if (list.isEmpty()) {
        suggestPopup.hide();
    } else {
        for (Object elm : list) {
            comboBoxModel.addElement(elm);
        }
        suggestPopup.show(this, 0, getHeight());
        suggestPopup.setPopupSize(getWidth(), suggestPopup.getHeight());
    }
}

public void hidePopup() {
    suggestPopup.hide();
}

/**
 * The value to search for I'm checking for case insensitive contains Modify
 * it if you want
 *
 * @param value
 */
private void filter(String value) {
    List<Object> tempList = new ArrayList<>();
    for (Object item : items) {
        if (item.toString().toLowerCase().contains(value.toLowerCase())) {
            tempList.add(item);
        }
    }
    addToSuggestList(tempList);
}

public List<T> getItems() {
    return items;
}

public void withItems(List<T> items) {
    this.items = items;
    Collections.sort(items, new Compare());
}

public void addItem(T item) {
    items.add(item);
}

public void clearItems() {
    items.clear();
}

private void install() {
    setBorder(null);
    setFocusTraversalKeysEnabled(false);
    getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            onTextChange(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            onTextChange(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            onTextChange(e);
        }

    });
    getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "commit");
    getActionMap().put("commit", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setSelectedItemFromList();
        }
    });
    getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "traversedown");
    getActionMap().put("traversedown", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            traversedown();
        }
    });
    getInputMap().put(KeyStroke.getKeyStroke("UP"), "traverseup");
    getActionMap().put("traverseup", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            traverseup();
        }
    });
}

private void setSelectedItemFromList() {
    if (comboBoxModel.getSize() > 0) {
        setText(suggestPopup.getList().getSelectedValue().toString());
        suggestPopup.hide();
    }
}

private void traverseup() {
    if (comboBoxModel.getSize() > 0) {
        int index = suggestPopup.getList().getSelectedIndex() - 1;
        if (index >= 0) {
            suggestPopup.getList().setSelectedIndex(index);
            suggestPopup.getList().ensureIndexIsVisible(index);
        }
    }
}

private void traversedown() {
    if (comboBoxModel.getSize() > 0) {
        int index = suggestPopup.getList().getSelectedIndex() + 1;
        if (index < comboBoxModel.getSize()) {
            suggestPopup.getList().setSelectedIndex(index);
            suggestPopup.getList().ensureIndexIsVisible(index);
        }
    }
}

private void onTextChange(DocumentEvent e) {
    Document source = e.getDocument();
    int length = source.getLength();
    try {
        filter(source.getText(0, length));
    } catch (BadLocationException be) {
        System.out.println("Contents: Unknown");
    }
}

private static class Compare implements Comparator {

    @Override
    public int compare(Object o1, Object o2) {
        return Objects.toString(o1, "").compareTo(Objects.toString(o2, ""));
    }

}
}

MCVE

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Phystem
 */
public class TestClass {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame = new JFrame("AutoSuggest Test");
            JTable table = getTable();
            AutoSuggestTable autoSuggest = new AutoSuggestTable(getSampleList());
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(new JScrollPane(table), BorderLayout.CENTER);
            panel.add(autoSuggest, BorderLayout.NORTH);
            frame.add(panel);
            table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(autoSuggest));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

private static List<String> getSampleList() {
    List<String> list = new ArrayList<>();
    list.add("Apples");
    list.add("Bananas");
    list.add("Grapes");
    list.add("Oranges");
    list.add("PineApples");
    list.add("Watermelon");
    list.add("Lemon");
    return list;
}

private static JTable getTable() {
    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("Test1");
    model.addColumn("Test2");
    for (int i = 0; i < 5; i++) {
        model.addRow(new Object[]{"xx" + i, "yy" + i});
    }
    JTable table = new JTable(model);
    return table;
}
}

For JTable It's throwing the above mentioned error for the first edit and working fine for the second edit. I mean if I double click on the first column it's throwing that error but when I again double click it's showing the autosuggest.


Answer:

I have used the isShowing method to check if is the editor is visible before showing the popup and it solved the issue.

      if (isShowing()) {
            suggestPopup.show(this, 0, getHeight());
            suggestPopup.setPopupSize(getWidth(), suggestPopup.getHeight());
        }

There are still some modifications that needs to be done.If anyone wants it here is the Gist

Question:

I am creating a inventory program that involves the cashier and the admin mode. The admin mode can change the item name, price and stocks of an item. All items are stored in a txt file.

The content of the txt file (it has slashes to separate names, prices etc):

 ID     Item Name                 Price   Stocks
00001 / Xbox 360                 / 7999  / 10
00002 / Xbox Series X            / 25999 / 20
00003 / Playstation 4 Pro        / 22999 / 15
00004 / Nintendo Switch          / 17999 / 27
00005 / Nintendo Switch Lite     / 11999 / 15

I read and display the items using a JTable. The column headers are added in the code:

String filePath = "C:\\Users\\zagad\\IdeaProjects\\DATABYTES\\stock\\consoles\\consoles.txt";
                File file = new File(filePath);
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    DefaultTableModel model = (DefaultTableModel) productTable.getModel();
                    Object[] tableLines = br.lines().toArray();

                    for (int i = 0; i < tableLines.length; i++) {
                        String line = tableLines[i].toString().trim();
                        String[] dataRow = line.split("/");
                        productTable.getColumnModel().getColumn(0).setPreferredWidth(21);
                        productTable.getColumnModel().getColumn(1).setPreferredWidth(120);
                        productTable.getColumnModel().getColumn(2).setPreferredWidth(30);
                        productTable.getColumnModel().getColumn(3).setPreferredWidth(30);
                        model.addRow(dataRow);
                    }

Here is a reference image of the layout:

It has a JTable and four JTextFields. Is there any idea that I can edit the txt file directly what the user inputs from the JTextFields with the corresponding location of items in the txt file?

Any idea is appreciated. Thank you!


Answer:

Because your file data lines (data records) are formatted in a specific way with various spacing, you should be trimming off any leading or trailing white-spaces from the columnar data as it is read to be placed within the JTable. Currently, you split each data file row with:

String[] dataRow = line.split("/"); 

This will not remove all the white-space padding applied to each column of data that was placed upon that data when it was formatted and saved to the file. This padding will need to be dealt with (removed) when you actually need to work with that data. It is far better to remove al this white-space padding when you carry out the split, like this:

String[] dataRow = line.split("\\s{0,}/\\s{0,}");

You have disclosed very little information with regards to what your particular application really contains like data types (although most are obvious), what is already created (classes, member/instance variables, fields, statics, etc), the code used to save your data from JTable to file, etc, etc. This therefore leaves the whole works open to assumptions or 101 questions which I in particular will not ask. Because of this, the code I present below will be basic and more geared to a Console styled inventory system rather than a GUI styled system. You will need to apply the code to specific areas of your application where you see fit.

To start with, you need an Items class. Since each Inventory Item contains specific attributes such as ID, Item Name, Price, and Quantity (in Stock), you need a means to hold this data in memory so that it can be worked with in a fashion where any piece of data can be quickly acquired and manipulated. Your class might look like this:

public class InventoryItems {

    private String id;
    private String itemName;
    private double price;
    private int quantity;

    public InventoryItems() {    }

    public InventoryItems(String id, String itemName, double price, int quantity) {
        this.id = id;
        this.itemName = itemName;
        this.price = price;
        this.quantity = quantity;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return new StringBuffer("").append("ID: ").append(id).append(", Item Name: ")
                                   .append(itemName).append(", Price: $").append(price)
                                   .append(", Quantity: ").append(quantity).toString();
    }

    /**
     * Returns instance data as a String in the format specified in your supplied 
     * format string.
     * 
     * @param formatString
     * @return
    */
    public String toString(String formatString) {
        if (formatString == null || formatString.equals("")) {
            return toString();
        }
        // String Format Specifiers must be separated 
        // with a single white-space.
        switch (formatString.split("\\s+").length) {
            case 1:
                return String.format(formatString, id);
            case 2:
                return String.format(formatString, id, itemName);
            case 3:
                return String.format(formatString, id, itemName, price);
            case 4:
                return String.format(formatString, id, itemName, price, quantity);
            default:
                return toString();
        }
    }
}

Now you need helper methods to: load your file data (inventory records), retrieve a specific record, retrieve specific item attributes, save specific item attributes, save records, etc, etc. These methods might look like this:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class InventoryDemo {

public enum ErrorMsg {
    LOAD_ERR("Inventory Items have not been loaded in yet!"),
    NOT_ADMIN("Invalid Operation! Admin Status Required!"),
    ID_EXISTS("Invalid ID! That ID Already Exists!"),
    NAME_EXISTS("Invalid Item Name! That Name Already Exists!");

    private String msg;

    ErrorMsg(String msg) {
        this.msg = msg;
    }

    public String msg() {
        return msg;
    }

}

private String encoding = "UTF-8"; // options: "US-ASCII", "UTF-8", "UTF-16"
private final List<InventoryItems> items = new ArrayList<>();
private boolean isInAdminMode = true;
private String dataFile = "InventoryItems.txt";
private String ls = System.lineSeparator();

public static void main(String[] args) {
    // App is started this way to avoid the need for statics.
    new InventoryDemo().startApp(args);
}

private void startApp(String[] args) {
    if (!loadDataFile()) {
        System.err.println("Error Loading: " + dataFile);
        System.exit(0);
    }

    Scanner input = new Scanner(System.in);

    System.out.println("This code is run from the startApp() method" + ls 
                     + "located within the InventoryDemo class. Its" + ls
                     + "intent is to demonstrate the how the methods" + ls
                     + "withint this class function." );
    pressAnyKey(input);

    System.out.println("Available Inventory Items");
    System.out.println("=========================");
    String[] allItems = getAllInventoryItems();
    if (allItems != null) {
        for (String strg : allItems) {
            System.out.println(strg);
        }
    }

    pressAnyKey(input);

    showFormattedTable();

    pressAnyKey(input);

    System.out.println();
    System.out.println("Available Inventory Items (with formated ID & Name)");
    String header = String.format("%-7s %-26s", "ID", "Item Name");
    String topLine = String.join("", Collections.nCopies(header.length(), "="));
    String underline = String.join("", Collections.nCopies(header.length(), "-"));
    System.out.println(topLine);
    System.out.println(header);
    System.out.println(underline);
    allItems = getAllInventoryItems("%-7s %-26s");
    if (allItems != null) {
        for (String strg : allItems) {
            System.out.println(strg);
        }
    }
    System.out.println(topLine);

    pressAnyKey(input);

    System.out.println();
    System.out.println("Retrieve A Specific Record (by ID: 00004)");
    System.out.println("=========================================");
    String itemString = getItemString("00004");
    System.out.println(itemString);

    pressAnyKey(input);

    System.out.println();
    System.out.println("Retrieve A Specific ID (by Name: Playstation 4 Pro)");
    System.out.println("===================================================");
    itemString = getItemID("Playstation 4 Pro");
    System.out.println(itemString);

    pressAnyKey(input);

    System.out.println();
    System.out.println("Retrieve A Specific Name (by ID: 00004)");
    System.out.println("=======================================");
    itemString = getItemName("00004");
    System.out.println(itemString);

    pressAnyKey(input);

    System.out.println();
    System.out.println("Retrieve A Specific Price (by ID: 00003)");
    System.out.println("========================================");
    double itemprice = getItemPrice("00003");
    System.out.println(itemprice);

    pressAnyKey(input);

    System.out.println();
    System.out.println("Retrieve A Specific Quantity (by ID: 00002)");
    System.out.println("===========================================");
    int itemQuant = getItemQuantity("00002");
    System.out.println(itemQuant);

    pressAnyKey(input);

    System.out.println();
    System.out.println("Change Name for: 'Playstation 4 Pro' at ID: "
            + "'00003' TO 'Some Kinda Game-Box'");
    System.out.println("============================================"
            + "================================");
    itemString = getItemString("00003");
    System.out.println("Current Name: -->  " + itemString);
    setItemName("00003", "Some Kinda Game-Box");
    itemString = getItemString("00003");
    System.out.println("Name is Now:  -->  " + itemString);

    pressAnyKey(input);

    System.out.println();
    String itemToAdd = "00006/LG LED 60\" Television/70555.0/08";
    System.out.println("Add a new Inventory Item (duplicate ID's are not allowed).");
    System.out.println("Adding (with 'NO Auto-ID-Increment'): "
            + itemToAdd);
    System.out.println("======================================"
            + "======================================");
    if (addInventoryItem(itemToAdd)) {
        showFormattedTable();
    }
    else {
        System.err.println("Could not add (" + itemToAdd + ") to Records List!");
    }

    pressAnyKey(input);

    System.out.println();
    itemToAdd = "DYSON 430 Vacume/65437.0/6";
    System.out.println("Add a new Inventory Item (duplicate ID's are not allowed).");
    System.out.println("Adding (with 'Auto-ID-Increment'): "
            + itemToAdd);
    System.out.println("======================================"
            + "=======================");
    if (addInventoryItem(itemToAdd)) {
        showFormattedTable();
    }
    else {
        System.err.println("Could not add (" + itemToAdd + ") to Records List!");
    }

    pressAnyKey(input);

    System.out.println();
    System.out.println("Delete an Inventory Item ID (based on Item ID: 00006).");
    String itemIDToDelete = "00006";
    if (deleteInventoryItemID(itemIDToDelete)) {
        showFormattedTable();
    }
    else {
        System.err.println("Could not delete record ID (" + itemIDToDelete + ") from Records List!");
    }

    pressAnyKey(input);

    System.out.println();
    System.out.println("Delete an Inventory Item Name (based on Item Name: DYSON 430 Vacume).");
    String itemNameToDelete = "DYSON 430 Vacume";
    if (deleteInventoryItemName(itemNameToDelete)) {
        showFormattedTable();
    }
    else {
        System.err.println("Could not delete record Item Name (" + itemNameToDelete + ") from Records List!");
    }
}

private void showFormattedTable() {
    System.out.println();
    System.out.println("   Available Inventory Items (with formatting)");
    String header = String.format("%-7s %-26s %-8s %-5s", "ID", "Item Name", "Price", "Stock");
    String topLine = String.join("", Collections.nCopies(header.length(), "="));
    String underline = String.join("", Collections.nCopies(header.length(), "-"));
    System.out.println(topLine);
    System.out.println(header);
    System.out.println(underline);
    String[] allItems = getAllInventoryItems("%-7s %-26s $%-9s %-5s");
    if (allItems != null) {
        for (String strg : allItems) {
            System.out.println(strg);
        }
    }
    System.out.println(topLine);
}

/**
 * Deletes the Inventory Item in Records List and data file that contains the 
 * supplied Item ID.<br><br>
 * 
 * @param idToDelete (String) The ID Number of the record to delete.<br>
 * 
 * @return (Boolean) True if successful and False if not.
 */
public boolean deleteInventoryItemID(String idToDelete) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return false;
    }
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return false;
    }
    for (int i = 0; i < items.size(); i++) {
        if (items.get(i).getId().equals(idToDelete)) {
            items.remove(i);
            break;
        }
    }
    refreshDataBase();
    return true;
}

/**
 * Deletes ALL Inventory Items in Records List and data file that contains the 
 * supplied Item Name.<br><br>
 * 
 * @param nameToDelete (String) The Item Name to delete (case insensitive).<br>
 * 
 * @return (Boolean) True if successful and False if not.
 */
public boolean deleteInventoryItemName(String nameToDelete) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return false;
    }
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return false;
    }
    for (int i = 0; i < items.size(); i++) {
        if (items.get(i).getItemName().equalsIgnoreCase(nameToDelete)) {
            items.remove(i);
            i--;
        }
    }
    refreshDataBase();
    return true;
}

/**
 * Admin Rights required!<br><BR>
 * <p>
 * Adds a Inventory Item to the Records List and database file.<br><br>
 * This method can produce a unique ID string automatically if (and only if)
 * an ID value is <b>not</b> supplied within the Inventory Item string. If
 * an ID <b>is supplied</b> within the Inventory Item string then that ID
 * will be used <b>UNLESS</b> that particular ID already exists in which
 * case this method will not apply the addition and a message is displayed
 * to Console indicating as such.
 *
 * @param itemToAdd (String) The string to be supplied must be a forward
 *                  slash (<b>/</b>) delimited <b>String</b> consisting
 *                  consisting of the 4 data parts that make up any
 *                  particular Inventory Item:<pre>
 *
 *     1) The unique Item ID String (optional);
 *     2) The Item Name String;
 *     3) The Item Price (as a String);
 *     4) The Item Quantity (as a String).</pre><br>
 *
 * A typical Inventory Item String supplied to this method should look like:
 * <pre>
 *     "00006/LG LED 60\" Television/70555.0/08"</pre><br>
 *
 * Or it can look like (if an Auto-ID is requested):
 * <pre>
 *     "LG LED 60\" Television/70555.0/08"</pre><br>
 *
 * Providing duplicate ID's (an ID that is already in the database file or
 * records List) is not allowed.
 *
 * @return (Boolean) True if the addition is successful and False if not.
 */
public boolean addInventoryItem(String itemToAdd) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return false;
    }
    try {
        String[] itemParts = itemToAdd.split("\\s{0,}/\\s{0,}");

        // Is Auto-ID-Increment required?
        if (!itemParts[0].matches("\\d+") && itemParts.length == 3) {
            //Yes...
            String newID = getUniqueID();
            String[] parts = new String[4];
            parts[0] = newID;
            parts[1] = itemParts[0];
            parts[2] = itemParts[1];
            parts[3] = itemParts[2];
            itemParts = parts;
        }
        String id = itemParts[0];
        if (doesItemIDAlreadyExist(id)) {
            System.err.println("The Item ID (" + id + ") is an " + ErrorMsg.ID_EXISTS.msg);
            return false;
        }
        String name = itemParts[1];
        if (doesItemNameAlreadyExist(name)) {
            System.err.println("The Item Name (" + name + ") is an " + ErrorMsg.NAME_EXISTS.msg);
            return false;
        }
        double price = Double.parseDouble(itemParts[2]);
        int quant = Integer.parseInt(itemParts[3]);
        items.add(new InventoryItems(id, name, price, quant));
        refreshDataBase();
    }
    catch (Exception ex) {
        System.err.println(ex);
        return false;
    }
    return true;
}

public boolean doesItemIDAlreadyExist(String id) {
    boolean res = false;
    if (items.isEmpty()) {
        res = false;
    }
    else {
        for (InventoryItems itms : items) {
            if (itms.getId().equals(id)) {
                res = true;
                break;
            }
        }
    }
    return res;
}

public boolean doesItemNameAlreadyExist(String name) {
    boolean res = false;
    if (items.isEmpty()) {
        res = false;
    }
    else {
        for (InventoryItems itms : items) {
            if (itms.getItemName().equalsIgnoreCase(name)) {
                res = true;
                break;
            }
        }
    }
    return res;
}

public String getUniqueID() {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return null;
    }
    int max = 0;
    for (InventoryItems itms : items) {
        String idVal = itms.getId();
        if (!idVal.matches("\\d+")) {
            continue;
        }
        int val = Integer.parseInt(idVal);
        if (val > max) {
            max = val;
        }
    }
    if (max == 0) {
        return String.format("%5s", 1).replace(' ', '0');
    }
    else {
        return String.format("%5s", (max + 1)).replace(' ', '0');
    }
}

public String[] getAllInventoryItems(String... formatString) {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return null;
    }
    String format = "";
    if (formatString.length > 0) {
        format = formatString[0];
    }
    String[] itms = new String[items.size()];
    for (int i = 0; i < items.size(); i++) {
        if (!format.equals("")) {
            itms[i] = items.get(i).toString(format);
        }
        else {
            itms[i] = items.get(i).toString();
        }
    }
    return itms;
}

public String getItemID(String itemName) {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return null;
    }
    String res = null;
    for (InventoryItems item : items) {
        if (item.getItemName().equalsIgnoreCase(itemName)) {
            res = item.getId();
            break;
        }
    }
    return res;
}

public void setItemID(String itemName, String newIDValue) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return;
    }

    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return;
    }
    for (InventoryItems item : items) {
        if (item.getItemName().equalsIgnoreCase(itemName)) {
            item.setId(newIDValue);
            break;
        }
    }
    refreshDataBase();
}

public String getItemString(String idNumber) {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return null;
    }
    String res = null;
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            res = item.toString();
            break;
        }
    }
    return res;
}

public String getItemName(String idNumber) {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return null;
    }
    String res = null;
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            res = item.getItemName();
            break;
        }
    }
    return res;
}

public void setItemName(String idNumber, String newName) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return;
    }
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return;
    }
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            item.setItemName(newName);
            break;
        }
    }
    refreshDataBase();
}

private double getItemPrice(String idNumber) {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return 0.0d;
    }
    double res = 0.0d;
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            res = item.getPrice();
            break;
        }
    }
    return res;
}

public void setItemPrice(String idNumber, double newPrice) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return;
    }
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return;
    }
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            item.setPrice(newPrice);
            break;
        }
    }
    refreshDataBase();
}

public int getItemQuantity(String idNumber) {
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return 0;
    }
    int res = 0;
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            res = item.getQuantity();
            break;
        }
    }
    return res;
}

public void setItemQuantity(String idNumber, int newQuantity) {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return;
    }
    if (items.isEmpty()) {
        System.err.println(ErrorMsg.LOAD_ERR.msg);
        return;
    }
    for (InventoryItems item : items) {
        if (item.getId().equals(idNumber)) {
            item.setQuantity(newQuantity);
        }
    }
    refreshDataBase();
}

public boolean loadDataFile() {
    boolean success;
    items.clear();  // Clear the Records Data List.

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile), encoding))) {
    // try (BufferedReader reader = new BufferedReader(new FileReader(dataFile))) {
        String line;
        while ((line = reader.readLine()) != null) {
            // Trim the data line and remove BOM's (if any).
            line = trimBOMs(line);
            // Skip the header line and blank lines (if any).
            if (line.equals("") || line.toLowerCase().startsWith("id")) {
                continue;
            }
            String[] lineParts = line.split("\\s{0,}/\\s{0,}");
            // Validate record data types...
            String id = "";
            String name = "Unknown";
            double price = 0.0d;
            int quant = 0;
            if (lineParts.length >= 1) {
                // Is it an actual integer value string
                if (lineParts[0].matches("\\d+")) {
                    id = lineParts[0];
                }
                if (lineParts.length >= 2) {
                    // Does the string contain a name or is it empty.
                    name = (lineParts[1].equals("") ? "Unknown" : lineParts[1]);
                    if (lineParts.length >= 3) {
                        // Is it an actual integer or double/float value string
                        if (lineParts[2].matches("-?\\d+(\\.\\d+)?")) {
                            price = Double.parseDouble(lineParts[2]);
                        }
                        if (lineParts.length >= 4) {
                            // Is it an actual integer value string
                            if (lineParts[0].matches("\\d+")) {
                                quant = Integer.parseInt(lineParts[3]);
                            }
                        }
                    }
                }
            }
            items.add(new InventoryItems(id, name, price, quant));
        }
        success = true;
    }
    catch (FileNotFoundException ex) {
        System.err.println(ex);
        success = false;
    }
    catch (IOException ex) {
        System.err.println(ex);
        success = false;
    }
    return success;
}

public boolean saveItemsToFile() {
    if (!isInAdminMode) {
        System.err.println(ErrorMsg.NOT_ADMIN.msg);
        return false;
    }
    String header = String.format("%-8s%-27s%-10s%-5s%n", "ID", "Item Name", "Price", "Stocks");

    try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(dataFile), encoding))) {
    // try (PrintWriter writer = new PrintWriter(new File(dataFile))) {
        writer.append(header);
        for (int i = 0; i < items.size(); i++) {
            String id = items.get(i).getId();
            String name = items.get(i).getItemName();
            double price = items.get(i).getPrice();
            int quant = items.get(i).getQuantity();
            String dataLine = String.format("%-6s/ %-25s/ %-8s/ %-4s%n", id, name, price, quant);
            writer.append(dataLine);
        }
        writer.flush();
    }
    catch (FileNotFoundException | UnsupportedEncodingException ex) {
        System.err.println(ex);
        return false;
    }
    return true;
}

public void refreshDataBase() {
    saveItemsToFile();
    loadDataFile();

    // Call and method to update your JTable(s) here!
}

public boolean isIsInAdminMode() {
    return isInAdminMode;
}

public void setIsInAdminMode(boolean isInAdminMode) {
    this.isInAdminMode = isInAdminMode;
}

public String getDataFilePath() {
    return dataFile;
}

public void setDataFilePath(String dataFile) {
    this.dataFile = dataFile;
}

public String getEncoding() {
    return encoding;
}

public void setEncoding(String encoding) {
    this.encoding = encoding;
}

/**
 * Removes (trims away) leading and trailing white-spaces and Removes 
 * any BOM's (Byte Order Marks) from all the different Character Encodings.<br><br>
 * 
 * @param inputString (String) The Unicode string to remove BOM's from.<br>
 * 
 * @return (String) 
 */
private static String trimBOMs(String inputString) {
    inputString = inputString.trim()
            .replaceAll("\uFEFF|\uEFBBBF|\uFFFE|\u0000FEFF|\uFFFE0000|\u2B2F7638","")
            .replaceAll("\u2B2F7639|\u2B2F762B|\u2B2F762F|\u2B2F76382D|\uF7644C", "")
            .replaceAll("\uDD736673|\u0EFEFF|\uFBEE28|\u84319533", "");
    return inputString;
}

private void pressAnyKey(Scanner input) {
    System.out.println(ls + "Press Enter key to continue (q to quit)...");
    String anyKey = input.nextLine();
    if (anyKey.toLowerCase().equals("q")) {
        System.exit(0);
    }
}
}

Create a new Java Application project named InventoryDemo and copy/paste the supplied classes into it. This should give you an idea of how to create the tools to do what you need.

Question:

I have 14 JFrame classes and one JFrame class holding a JTable which acts as a calendar. I want the date of a text field or any other component in any of the 14 JFrame classes to be changed when the user selects the date from the JTable calendar . Now, the frames appear one at a time and the jtable is instantiated from a JButton on any of the 14 JFrame classes.

My dilemma is how to identify the JFrame class that has instantiated the JFrame that contains the JTable so that it's text field value can be changed.

Code that instantiates the JFrame containing the table:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFrame frmMain = new Calender();
    frmMain.setVisible(true);
    frmMain.setSize(367, 342);
}

Code that changes the value of the textfield from one of the 14 JFrame classes :

@Override
public void mouseClicked(MouseEvent me) {
    if (me.getClickCount()==2){
        int selected1 =    jTable1.getSelectionModel().getLeadSelectionIndex();
        int selected2 =jTable1.getColumnModel().getSelectionModel().getLeadSelectionIndex();

        Object c = jTable1.getModel().getValueAt(selected1, selected2);
        SowInformation.jTextField4.setText(c.toString());
        this.setVisible(false);
    }
}

Where SowInformation is one of the 14 JFrame classes.


Answer:

As @AndrewThompson wisely mentioned, the use of multiple JFrame is a bad practice. In this case a (non-)modal dialog would be a better choice.

Now let's assume you choose non-modal dialogs, then you still having the same problem. There are several ways to solve it but essentially you need to keep a reference to the frame/dialog which opened the calendar's frame. Even better, you can use interfaces to define a contract to update a text field isolating the desired behavior from the actual implementation. For example:

public interface IUpdateText {

    public void updateText(String text);

}

Now your calendar's frame needs to keep a reference to a IUpdateText compliant object:

class Calender extends JFrame {
    ...
    private IUpdateText updateText;
    ...
    public void setIUpdateText(IUpdateText ut) {
        this.updateText = ut;
    }
    ...
}

Then in your mouse listener implementation just call updateText() method:

@Override
public void mouseClicked(MouseEvent me) {
    if (me.getClickCount()==2){
        int selectedRow = jTable1.getSelectedRow();
        int selectedColumn = jTable1.getSelectedColumn();
        if (selectedRow > -1 && selectedColumn > -1) {
            Object value = jTable1.getValueAt(selectedRow, selectedColumn); // ask the value to the view, not the model. Otherwise you need to convert both row and column indexes
            this.updateText.updateText(value.toString());
            this.setVisible(false); // maybe this.dispose() instead ?
        }
    }
}

Of course all your other frames/dialogs that can open this calendar frame would have to implement IUpdateText interface. For example SowInformation frame:

class SowInformation extends JFrame implements IUpdateText {
    ...
    private JTextField jTextField4;
    ...
    @Override
    public void updateText(String text) {
        this.jTextfield4.setText(text);
    }
    ...
}

And finally set the IUpdateText compliant object to the calendar frame when you instantiate it:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Calender frmMain = new Calender();
    calender.setIUpdateText(this);  // assuming 'this' is an IUpdateText interface compliant
    frmMain.pack();
    frmMain.setVisible(true);
}

Other comments

From an architectural point of view this problem might be better solved through an MVC pattern, having a Controller which acts as an intermediary for both frames/dialogs. There are multiple questions about MVC pattern and Swing here in SO but I really like the example/explanation shown in this answer by @MadProgrammer (a real Swing guru).

Question:

I have a few text fields, A JTable and a Button (ADD).

Now, every time the button it gets the JTextFields data and adds a row in JTable. And it adds the unlimited number of rows. I want to add only 10 rows.

public void setItemDetailInTable() {
    String free = freetext.getText();
    Object row[] = new Object[20];


    row[0] = itemcombo.getSelectedItem().toString();
    row[1] = mfgtext.getText();
    row[2] = sachsntext.getText();
    if (free.isEmpty()) {
        row[3] = qtytext.getText();
    } else {
        row[3] = qtytext.getText() + "+" + freetext.getText();
    }
    row[4] = pricetext.getText();
    row[5] = taxtext.getText() ;
    row[6] = discounttext.getText() ;
    row[7] = total.getText();

    model_table.addRow(row);
}

Answer:

Just add a counter ?

if (table.getRowCount < 10) {
     //doStuff...
} else {
    //doSomethingElse...
}