我正在设计一个客户机交互应用程序,其中的需求如下:-
package com.ajay.test;
/*
* SimpleTableDemo.java requires no other files.
*/
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
private JTable table = null;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)}};
table = new JTable(new javax.swing.table.DefaultTableModel(data, columnNames));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID() == KeyEvent.KEY_PRESSED)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
addRow();
}
}
return false;
}
});
//Add the scroll pane to this panel.
add(scrollPane);
}
private void addRow()
{
DefaultTableModel model = (DefaultTableModel)table.getModel();
if (model != null) {
Vector v = new Vector(1);
for (int j = 0; j < table.getColumnCount(); j++){
v.add("");
}
model.addRow(v);
}
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
如果单元格焦点不在,请建议添加行。并编辑单元格焦点是否存在。
我不知道你是否得到了你想要的,但只要在你的监听器中添加一个条件,你就可以做到(如果我理解你的话):
/*
* SimpleTableDemo.java requires no other files.
*/
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
private JTable table = null;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)}};
DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);
table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID() == KeyEvent.KEY_PRESSED)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER && table.isEditing()==false)
{//HERE IS THE CHANGE
addRow();
}
}
return false;
}
});
//Add the scroll pane to this panel.
add(scrollPane);
}
private void addRow()
{
DefaultTableModel model = (DefaultTableModel)table.getModel();
if (model != null) {
Vector v = new Vector(1);
for (int j = 0; j < table.getColumnCount(); j++){
v.add("");
}
model.addRow(v);
}
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
通过添加table.isediting()==false
条件,您将在按Enter
时添加新行,但如果您刚刚编辑了单元格并按Enter
则不会添加新行。
问题内容: 我有一个基于Swing的应用程序,其中包含一个。现在,我想使用唯一的行ID来更新或删除每一行。因此,我想向每行添加一个更新和删除按钮,它们具有支持ActionListener的功能。但是,我不知道如何使用NetBeans执行此操作。 问题答案: 要在列中显示按钮,您需要创建: 一个自定义渲染器以显示JButton 定制编辑器以响应鼠标单击 阅读Swing教程中有关如何使用表的部分。关于
问题内容: 您知道如何将新行添加到吗? 问题答案: 的后面处理表后面的所有数据。为了在表格中添加和删除行,您需要使用 要使用此模型创建表: 要添加一行: 您也可以使用此方法删除行。
问题内容: 我知道以前已经回答过,但是考虑到我仍然是一个新手,我想不出一种方法来执行操作并阻止单元格编辑,我已经尝试了几种方法,可以成功渲染按钮,但是当我单击它时,它编辑单元格而不是按下按钮,我知道为了避免单元格编辑,我应该创建一个抽象表并覆盖此方法: 但是我使用默认的JTable,所以这是我用于面板和单元格渲染器的代码: 这是我用来从sql检索数据到Jtable的代码(我自定义了DefaultJ
我编写了一个自定义的,这样当我键入示例的列时,除了数字之外什么都不能输入。 如果用户单击单元格一次,然后开始键入,同时仍然允许用户双击单元格开始编辑,我如何防止用户能够编辑单元格?
如何从数据库向jTable中添加新行,同时单击按钮而不清除jTable中的现有行? 我试过很多方法。但没有成功。帮助 编辑:-在所有答案的帮助下,我将代码改为下面的代码,但可以。但这里我在DefaultTableModel中得到一个错误 新代码 最新代码 **但这里出现了NullpointException错误!以及如何选择jTable1作为上面代码中的表?
下面是.java文件中的代码