gpt4 book ai didi

java - JTable 和 JTextField 用于输入。 JTable 是可更新的。用于 JTextField 的 DocumentListener。事件不会触发。如何获取事件?

转载 作者:行者123 更新时间:2023-11-29 06:13:18 25 4
gpt4 key购买 nike

import java.awt.*;
import java.awt.Component;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.event.*;
import javax.swing.text.Document;
import javax.swing.text.*;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.*;
import java.util.*;

public class Test1 extends JApplet
{

Object dummya [][] = new Object [9][9]; // Setup 2-Dimensional Arrays

int inputa [][] = new int [9][9];
int possiblea [][] = new int [81][9];
int solveda [][] = new int [9][9];

String ic = "B";
String nl = "\n";
String col_h [] = {"G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9"};

Font f = new Font ("Courier", Font.BOLD, 10);
Font h = new Font ("Times Roman", Font.BOLD, 14);

JTextField uc = new JTextField (1);
JTextField st = new JTextField (75);

int gs = 55;
int wl = 1;
int ts = col_h.length;

DefaultTableModel dm = new DefaultTableModel(dummya, col_h) { // Setup the Table Model
public String getColumnName (int col) {return col_h[col];}
public int getColumnCount() {return 9;} // Setup a 9x9 Table with headers
public int getRowCount() {return 9;}
public void setValueAt (Object foundValue, int row, int col) {
// st.setText ("At setValueAt.");
// inputa[row][col] = (Integer) foundValue;
fireTableCellUpdated (row, col);
}
};

JTable table = new JTable (dm); // Create the Table

public void init () {
Container c = getContentPane (); // Get a Container for User View
Color lc = new Color (240, 128, 128); // Set Light Coral Color
c.setBackground (lc);
c.setFont (f);

table.getTableHeader().setResizingAllowed (false); // Setup all the Table rules
table.getTableHeader().setReorderingAllowed (false);
table.setSelectionMode (javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

for (int i = 0; i < ts; i++) { // Resize Columns & Rows to 40 pixels
TableColumn column = table.getColumnModel().getColumn(i);
column.setMinWidth (40);
column.setMaxWidth (40);
column.setPreferredWidth (40);
table.setRowHeight (i, 40);
}
table.getModel().addTableModelListener (
new TableModelListener() {
public void tableChanged(TableModelEvent e) {
tableDataChanged (e);
}
});

c.setLayout (new FlowLayout (FlowLayout.CENTER, 340, 0)); // Setup a Layout & add Components
c.add (new JLabel (" "));
c.add (new JLabel ("Challenge", SwingConstants.CENTER));
c.add (new JLabel (" "));
c.add (new JLabel ("By Author X", SwingConstants.CENTER));
c.add (new JLabel (" ")); // Add a pad line
c.setFont (h);
table.setGridColor (Color.red); // Setup Table View
c.add (table.getTableHeader()); // Add Column Headers
c.add (table); // Show the Table
c.add (new JLabel (" "));
uc.setBackground (lc);
c.add (new JLabel (" Enter Command (C,F,H,L,I,P,S): ", SwingConstants.CENTER));
c.add (uc); // Add a User Command Field
uc.setEditable (true); // Allow Input in Command Field
uc.requestFocusInWindow();
c.add (new JLabel (" "));
st.setBackground (lc);
c.add (st); // Add a Status area
st.setEditable (false);

uc.getDocument().addDocumentListener (new DocumentListener() { // Listen for JTextField command
public void changedUpdate(DocumentEvent e) {
editInput();
}
public void insertUpdate(DocumentEvent e) {
editInput();
}
public void removeUpdate(DocumentEvent e) {}
});
}

// The following section is driven by a user command.
public void run () { // To keep the Applet running, just loop

int loopcnt = 0;
while (wl != -1) {
loopcnt++;
if (loopcnt == 100) {
try { Thread.sleep (10000); } // Sleep for 10 seconds to allow solve
catch (InterruptedException e) {} // thread to run.
loopcnt = 0;
}
}
}


private void editInput () { // Scan user command

try { ic = uc.getText (0, 1); } // Pick up user command
catch (BadLocationException be) { return; }
st.setText (" User Input is: " + ic);

if (ic == "C" || ic == "c") clearComponents ();
else if (ic == "E" || ic == "e") {
st.setText (" User Exit.");
wl = -1;
}
else if (ic == "H" || ic == "h") newFrame();
// else if (ic == "F" || ic == "f") getHintAtFirst(); // Get a hint where only 2 values possible
// else if (ic == "L" || ic == "l ") getHintAtLast();
// else if (ic == "I" || ic == "i") StartThread(); // Look into wait and Notify
// else if (ic == "P" || ic == "p") printGrid();
// else if (ic == "S" || ic == "s") solveAndShow();
// else st.setText (" Invalid Command, try again.");
}


public void clearComponents () { // Clear Arrarys and Table View

for (int i = 0; i < ts; i++) {
for (int j = 0; j < ts; j++) {
dummya[i][j] = null;
inputa[i][j] = 0;
possiblea[i][j] = 0;
solveda[i][j] = 0;
table.setValueAt (null, i, j);
}
}
st.setText (" Table Cleared.");
}

private void newFrame () { // Setup the Possibles frame
JFrame possibles = new JFrame ("Grid Possibilities");
possibles.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
possibles.setBounds (550, 0, 440, 580);
JTextArea ta = new JTextArea ();
JScrollPane sp = new JScrollPane ();
possibles.add (ta);
possibles.add (sp);
possibles.pack ();
possibles.validate ();
possibles.setVisible (true);
}

public void tableDataChanged (TableModelEvent e) { // Process & Edit user input
int row = e.getFirstRow();
int col = e.getColumn();
// TableModel model = (TableModel) e.getSource();
// String cn = model.getColumnName (col);

for (int i = row; i < ts; i++) { // Scan the input for values
for (int j = col; i < ts; j++) {
dummya [row][col] = table.getValueAt (i, j);
int newValue = (Integer) dummya [row][col];
int rc = integerEditor (1, 9, newValue); // Go check the user input
if (rc == 0) {
st.setText ("Input Value is " + newValue);
inputa [row][col] = (Integer) dummya [row][col]; // Store the grid value
}

else st.setText ("Input Value is invalid at " + row+1 + "," + col+1 + ".");
}
}
}

private int integerEditor (int va, int vb, int value) { // Edit user input
if (value < va || value > vb) return -1;
return 0;
}
}

最佳答案

DefaultCellEditor dce = (DefaultCellEditor)table.getDefaultEditor(Object.class);
JTextField editor = (JTextField)dce.getComponent();
editor.addDocumentListener(...);

关于java - JTable 和 JTextField 用于输入。 JTable 是可更新的。用于 JTextField 的 DocumentListener。事件不会触发。如何获取事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6002858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com