我的要求是:
当用户保存JTable中的数据时,检查用户输入是不是正确,记录所有的错误信息并把错误信息以对话框形式显示给用户,当用户“确定”后,系统就将错误的地方标注红色并在错误的旁边显示错误图片,用户点击图片显示该单元格的错误信息
是不是很难,很麻烦?
劳驾各位高手了!
14 个解决方案
#1
严重关注!
#2
不难,不过没时间给你写程序,只提供思路:
1。每一行信息制作一个recordObj类(包括看得见的输入,看不见的错误信息等等属性)
2。当用户提交时将所有recordObj压入ArrayList。
3。检查用户信息不正确的话,就在相应的recordObj里面的errMsg里填写错误信息(errMsg可做成list存储多条错误信息)
4。所有检查完成后,察看ArrayList里面的recordObj,只要有一条信息有errMsg,就可以弹出错误对话框,在对话框里用循环把所有错误信息打出来
5。用户“确定”后,再次来到jtable,这个时候如果recordObj有errMsg,就打出图片(不明白你的图片是什么,是不是一个小icon按钮?)
6。点下图片的时候读出相应recordObj的错误信息显示
1。每一行信息制作一个recordObj类(包括看得见的输入,看不见的错误信息等等属性)
2。当用户提交时将所有recordObj压入ArrayList。
3。检查用户信息不正确的话,就在相应的recordObj里面的errMsg里填写错误信息(errMsg可做成list存储多条错误信息)
4。所有检查完成后,察看ArrayList里面的recordObj,只要有一条信息有errMsg,就可以弹出错误对话框,在对话框里用循环把所有错误信息打出来
5。用户“确定”后,再次来到jtable,这个时候如果recordObj有errMsg,就打出图片(不明白你的图片是什么,是不是一个小icon按钮?)
6。点下图片的时候读出相应recordObj的错误信息显示
#3
楼上的思路差不多
#4
多谢楼上的朋友!
思路很好!
图片是一个一个的icon,就是error icon.
我想知道如何显示icon, 谁能给一段代码?
谢谢!
思路很好!
图片是一个一个的icon,就是error icon.
我想知道如何显示icon, 谁能给一段代码?
谢谢!
#5
楼主是不是ysg,亳州一中的,^_^,老同学.
#6
西风?
太巧了吧?!
我的问题解决不了,你知道怎么搞吗?
帮我看看。有懂得人也帮我问问啊!
thanks!
太巧了吧?!
我的问题解决不了,你知道怎么搞吗?
帮我看看。有懂得人也帮我问问啊!
thanks!
#7
不知道这样写楼主满意不。
我的规则是,只要单元格中有数字就算输入不正确。
/*
* JTableError.java
*
* Created on 2004年7月13日, 下午1:34
*/
/**
*
* @author Administrator
*/
public class JTableError extends javax.swing.JFrame {
/** Creates new form JTableError */
public JTableError() {
errorLine = new int[ROWCOUNT][COLUMNCOUNT + 1];
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT + 1;j++){
errorLine[i][j] = 0;
}
}
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jTable1 = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jButtons = new ErrorButton[ROWCOUNT];
setTitle("Show Error Demo");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setPreferredSize(new java.awt.Dimension(460, 400));
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.setPreferredSize(new java.awt.Dimension(440, 800));
jTable1.setModel(new JTableErrorModel());
jTable1.setPreferredSize(new java.awt.Dimension(360, 800));
jPanel1.add(jTable1, java.awt.BorderLayout.EAST);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));
jPanel2.setPreferredSize(new java.awt.Dimension(80, 800));
for(int i = 0;i < ROWCOUNT;i++) {
jButtons[i] = new ErrorButton(i);
jPanel2.add(jButtons[i]);
}
jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);
jScrollPane1.setViewportView(jPanel1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
private void setButtonString(int index,boolean valid){
if(valid){
jButtons[index].setText("OK");
jButtons[index].setForeground(java.awt.Color.black);
}else{
jButtons[index].setText("ERROR");
jButtons[index].setForeground(java.awt.Color.red);
javax.swing.JOptionPane.showMessageDialog(this,"本行内容有误!","错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
}
private void showErrorStr(String str){
javax.swing.JOptionPane.showMessageDialog(this,str,"错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new JTableError().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private ErrorButton[] jButtons;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private int ROWCOUNT = 100;
private int COLUMNCOUNT = 4;
private int[][] errorLine;
// End of variables declaration//GEN-END:variables
class JTableErrorModel extends javax.swing.table.AbstractTableModel{
final String[] columnNames = {"Title 1", "Title 2", "Title 3", "Title 4"};
private Object[][] data = new Object[ROWCOUNT][COLUMNCOUNT];
public JTableErrorModel(){
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT;j++){
data[i][j] = new String("");
}
}
}
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
public Object getValueAt(int rowIndex,int columnIndex){
return data[rowIndex][columnIndex];
}
public void setValueAt(Object aValue,int rowIndex,int columnIndex){
data[rowIndex][columnIndex] = aValue;
checkValid(rowIndex);
}
public boolean isCellEditable(int rowIndex,int columnIndex){
return true;
}
public int getColumnCount(){
return COLUMNCOUNT;
}
public int getRowCount(){
return ROWCOUNT;
}
private void checkValid(int rowIndex){
int errorCount = 0,j;
String a;
for(int i = 0;i < COLUMNCOUNT;i++){
a = (String)data[rowIndex][i];
for(j = 0;j < a.length();j++){
if(a.charAt(j) > '0' && a.charAt(j) < '9'){
errorCount++;
errorLine[rowIndex][i + 1] = 1;
break;
}
}
if(j == a.length()) errorLine[rowIndex][i + 1] = 0;
}
if(errorCount == 0){
errorLine[rowIndex][0] = 0;
setButtonString(rowIndex,true);
}else{
errorLine[rowIndex][0] = 1;
setButtonString(rowIndex,false);
}
}
}
class ErrorButton extends javax.swing.JButton{
private int row;
public ErrorButton(int r){
this.row = r;
setFont(new java.awt.Font("Dialog", 1, 10));
setText("OK");
setPreferredSize(new java.awt.Dimension(80, 16));
addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
if(errorLine[row][0] == 0) return;
StringBuffer errorStr = new StringBuffer("以下列填写有误:\n");
for(int j = 1;j < COLUMNCOUNT + 1;j++){
if(errorLine[row][j] == 1){
errorStr.append("第"+j+"列\n");
}
}
showErrorStr(new String(errorStr));
}
});
}
}
}
我的规则是,只要单元格中有数字就算输入不正确。
/*
* JTableError.java
*
* Created on 2004年7月13日, 下午1:34
*/
/**
*
* @author Administrator
*/
public class JTableError extends javax.swing.JFrame {
/** Creates new form JTableError */
public JTableError() {
errorLine = new int[ROWCOUNT][COLUMNCOUNT + 1];
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT + 1;j++){
errorLine[i][j] = 0;
}
}
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jTable1 = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jButtons = new ErrorButton[ROWCOUNT];
setTitle("Show Error Demo");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setPreferredSize(new java.awt.Dimension(460, 400));
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.setPreferredSize(new java.awt.Dimension(440, 800));
jTable1.setModel(new JTableErrorModel());
jTable1.setPreferredSize(new java.awt.Dimension(360, 800));
jPanel1.add(jTable1, java.awt.BorderLayout.EAST);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));
jPanel2.setPreferredSize(new java.awt.Dimension(80, 800));
for(int i = 0;i < ROWCOUNT;i++) {
jButtons[i] = new ErrorButton(i);
jPanel2.add(jButtons[i]);
}
jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);
jScrollPane1.setViewportView(jPanel1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
private void setButtonString(int index,boolean valid){
if(valid){
jButtons[index].setText("OK");
jButtons[index].setForeground(java.awt.Color.black);
}else{
jButtons[index].setText("ERROR");
jButtons[index].setForeground(java.awt.Color.red);
javax.swing.JOptionPane.showMessageDialog(this,"本行内容有误!","错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
}
private void showErrorStr(String str){
javax.swing.JOptionPane.showMessageDialog(this,str,"错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new JTableError().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private ErrorButton[] jButtons;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private int ROWCOUNT = 100;
private int COLUMNCOUNT = 4;
private int[][] errorLine;
// End of variables declaration//GEN-END:variables
class JTableErrorModel extends javax.swing.table.AbstractTableModel{
final String[] columnNames = {"Title 1", "Title 2", "Title 3", "Title 4"};
private Object[][] data = new Object[ROWCOUNT][COLUMNCOUNT];
public JTableErrorModel(){
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT;j++){
data[i][j] = new String("");
}
}
}
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
public Object getValueAt(int rowIndex,int columnIndex){
return data[rowIndex][columnIndex];
}
public void setValueAt(Object aValue,int rowIndex,int columnIndex){
data[rowIndex][columnIndex] = aValue;
checkValid(rowIndex);
}
public boolean isCellEditable(int rowIndex,int columnIndex){
return true;
}
public int getColumnCount(){
return COLUMNCOUNT;
}
public int getRowCount(){
return ROWCOUNT;
}
private void checkValid(int rowIndex){
int errorCount = 0,j;
String a;
for(int i = 0;i < COLUMNCOUNT;i++){
a = (String)data[rowIndex][i];
for(j = 0;j < a.length();j++){
if(a.charAt(j) > '0' && a.charAt(j) < '9'){
errorCount++;
errorLine[rowIndex][i + 1] = 1;
break;
}
}
if(j == a.length()) errorLine[rowIndex][i + 1] = 0;
}
if(errorCount == 0){
errorLine[rowIndex][0] = 0;
setButtonString(rowIndex,true);
}else{
errorLine[rowIndex][0] = 1;
setButtonString(rowIndex,false);
}
}
}
class ErrorButton extends javax.swing.JButton{
private int row;
public ErrorButton(int r){
this.row = r;
setFont(new java.awt.Font("Dialog", 1, 10));
setText("OK");
setPreferredSize(new java.awt.Dimension(80, 16));
addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
if(errorLine[row][0] == 0) return;
StringBuffer errorStr = new StringBuffer("以下列填写有误:\n");
for(int j = 1;j < COLUMNCOUNT + 1;j++){
if(errorLine[row][j] == 1){
errorStr.append("第"+j+"列\n");
}
}
showErrorStr(new String(errorStr));
}
});
}
}
}
#8
多谢楼上的!
错误显示是我要的!但是我想让每一个错误的Cell旁都有一个error icon,而不是每一行就一个。
多谢啊!结贴时一定给分!!
错误显示是我要的!但是我想让每一个错误的Cell旁都有一个error icon,而不是每一行就一个。
多谢啊!结贴时一定给分!!
#9
老大,我看见你的帖子了,俺们来up下了,高手多多指教,我也省心学习了。
#10
哈哈!继续努力学习!也期待高手帮忙!
#11
学习学习
#12
给你个网址,你要说这个不行,那我实在就没办法了。
http://www.senun.com/Left/Programming/Java_old/Examples_swing/JTableExamples8.html
http://www.senun.com/Left/Programming/Java_old/Examples_swing/JTableExamples8.html
#13
客户要求原文如下:
On closing the error message window,the user is returned to the screen
in which problems are clearly indicated by means of text color red and an error icon.
when the user clicks one of the error icons adjacent to an entry field in which
an error has occured,the error message related to the specific field is displayed
in a pop-up window.
On closing the error message window,the user is returned to the screen
in which problems are clearly indicated by means of text color red and an error icon.
when the user clicks one of the error icons adjacent to an entry field in which
an error has occured,the error message related to the specific field is displayed
in a pop-up window.
#14
老大啊,怎么没有人来顶下了哦,高手快来帮忙了,学习的真苦啊!
#1
严重关注!
#2
不难,不过没时间给你写程序,只提供思路:
1。每一行信息制作一个recordObj类(包括看得见的输入,看不见的错误信息等等属性)
2。当用户提交时将所有recordObj压入ArrayList。
3。检查用户信息不正确的话,就在相应的recordObj里面的errMsg里填写错误信息(errMsg可做成list存储多条错误信息)
4。所有检查完成后,察看ArrayList里面的recordObj,只要有一条信息有errMsg,就可以弹出错误对话框,在对话框里用循环把所有错误信息打出来
5。用户“确定”后,再次来到jtable,这个时候如果recordObj有errMsg,就打出图片(不明白你的图片是什么,是不是一个小icon按钮?)
6。点下图片的时候读出相应recordObj的错误信息显示
1。每一行信息制作一个recordObj类(包括看得见的输入,看不见的错误信息等等属性)
2。当用户提交时将所有recordObj压入ArrayList。
3。检查用户信息不正确的话,就在相应的recordObj里面的errMsg里填写错误信息(errMsg可做成list存储多条错误信息)
4。所有检查完成后,察看ArrayList里面的recordObj,只要有一条信息有errMsg,就可以弹出错误对话框,在对话框里用循环把所有错误信息打出来
5。用户“确定”后,再次来到jtable,这个时候如果recordObj有errMsg,就打出图片(不明白你的图片是什么,是不是一个小icon按钮?)
6。点下图片的时候读出相应recordObj的错误信息显示
#3
楼上的思路差不多
#4
多谢楼上的朋友!
思路很好!
图片是一个一个的icon,就是error icon.
我想知道如何显示icon, 谁能给一段代码?
谢谢!
思路很好!
图片是一个一个的icon,就是error icon.
我想知道如何显示icon, 谁能给一段代码?
谢谢!
#5
楼主是不是ysg,亳州一中的,^_^,老同学.
#6
西风?
太巧了吧?!
我的问题解决不了,你知道怎么搞吗?
帮我看看。有懂得人也帮我问问啊!
thanks!
太巧了吧?!
我的问题解决不了,你知道怎么搞吗?
帮我看看。有懂得人也帮我问问啊!
thanks!
#7
不知道这样写楼主满意不。
我的规则是,只要单元格中有数字就算输入不正确。
/*
* JTableError.java
*
* Created on 2004年7月13日, 下午1:34
*/
/**
*
* @author Administrator
*/
public class JTableError extends javax.swing.JFrame {
/** Creates new form JTableError */
public JTableError() {
errorLine = new int[ROWCOUNT][COLUMNCOUNT + 1];
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT + 1;j++){
errorLine[i][j] = 0;
}
}
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jTable1 = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jButtons = new ErrorButton[ROWCOUNT];
setTitle("Show Error Demo");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setPreferredSize(new java.awt.Dimension(460, 400));
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.setPreferredSize(new java.awt.Dimension(440, 800));
jTable1.setModel(new JTableErrorModel());
jTable1.setPreferredSize(new java.awt.Dimension(360, 800));
jPanel1.add(jTable1, java.awt.BorderLayout.EAST);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));
jPanel2.setPreferredSize(new java.awt.Dimension(80, 800));
for(int i = 0;i < ROWCOUNT;i++) {
jButtons[i] = new ErrorButton(i);
jPanel2.add(jButtons[i]);
}
jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);
jScrollPane1.setViewportView(jPanel1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
private void setButtonString(int index,boolean valid){
if(valid){
jButtons[index].setText("OK");
jButtons[index].setForeground(java.awt.Color.black);
}else{
jButtons[index].setText("ERROR");
jButtons[index].setForeground(java.awt.Color.red);
javax.swing.JOptionPane.showMessageDialog(this,"本行内容有误!","错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
}
private void showErrorStr(String str){
javax.swing.JOptionPane.showMessageDialog(this,str,"错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new JTableError().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private ErrorButton[] jButtons;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private int ROWCOUNT = 100;
private int COLUMNCOUNT = 4;
private int[][] errorLine;
// End of variables declaration//GEN-END:variables
class JTableErrorModel extends javax.swing.table.AbstractTableModel{
final String[] columnNames = {"Title 1", "Title 2", "Title 3", "Title 4"};
private Object[][] data = new Object[ROWCOUNT][COLUMNCOUNT];
public JTableErrorModel(){
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT;j++){
data[i][j] = new String("");
}
}
}
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
public Object getValueAt(int rowIndex,int columnIndex){
return data[rowIndex][columnIndex];
}
public void setValueAt(Object aValue,int rowIndex,int columnIndex){
data[rowIndex][columnIndex] = aValue;
checkValid(rowIndex);
}
public boolean isCellEditable(int rowIndex,int columnIndex){
return true;
}
public int getColumnCount(){
return COLUMNCOUNT;
}
public int getRowCount(){
return ROWCOUNT;
}
private void checkValid(int rowIndex){
int errorCount = 0,j;
String a;
for(int i = 0;i < COLUMNCOUNT;i++){
a = (String)data[rowIndex][i];
for(j = 0;j < a.length();j++){
if(a.charAt(j) > '0' && a.charAt(j) < '9'){
errorCount++;
errorLine[rowIndex][i + 1] = 1;
break;
}
}
if(j == a.length()) errorLine[rowIndex][i + 1] = 0;
}
if(errorCount == 0){
errorLine[rowIndex][0] = 0;
setButtonString(rowIndex,true);
}else{
errorLine[rowIndex][0] = 1;
setButtonString(rowIndex,false);
}
}
}
class ErrorButton extends javax.swing.JButton{
private int row;
public ErrorButton(int r){
this.row = r;
setFont(new java.awt.Font("Dialog", 1, 10));
setText("OK");
setPreferredSize(new java.awt.Dimension(80, 16));
addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
if(errorLine[row][0] == 0) return;
StringBuffer errorStr = new StringBuffer("以下列填写有误:\n");
for(int j = 1;j < COLUMNCOUNT + 1;j++){
if(errorLine[row][j] == 1){
errorStr.append("第"+j+"列\n");
}
}
showErrorStr(new String(errorStr));
}
});
}
}
}
我的规则是,只要单元格中有数字就算输入不正确。
/*
* JTableError.java
*
* Created on 2004年7月13日, 下午1:34
*/
/**
*
* @author Administrator
*/
public class JTableError extends javax.swing.JFrame {
/** Creates new form JTableError */
public JTableError() {
errorLine = new int[ROWCOUNT][COLUMNCOUNT + 1];
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT + 1;j++){
errorLine[i][j] = 0;
}
}
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jScrollPane1 = new javax.swing.JScrollPane();
jPanel1 = new javax.swing.JPanel();
jTable1 = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jButtons = new ErrorButton[ROWCOUNT];
setTitle("Show Error Demo");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setPreferredSize(new java.awt.Dimension(460, 400));
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.setPreferredSize(new java.awt.Dimension(440, 800));
jTable1.setModel(new JTableErrorModel());
jTable1.setPreferredSize(new java.awt.Dimension(360, 800));
jPanel1.add(jTable1, java.awt.BorderLayout.EAST);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));
jPanel2.setPreferredSize(new java.awt.Dimension(80, 800));
for(int i = 0;i < ROWCOUNT;i++) {
jButtons[i] = new ErrorButton(i);
jPanel2.add(jButtons[i]);
}
jPanel1.add(jPanel2, java.awt.BorderLayout.WEST);
jScrollPane1.setViewportView(jPanel1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
private void setButtonString(int index,boolean valid){
if(valid){
jButtons[index].setText("OK");
jButtons[index].setForeground(java.awt.Color.black);
}else{
jButtons[index].setText("ERROR");
jButtons[index].setForeground(java.awt.Color.red);
javax.swing.JOptionPane.showMessageDialog(this,"本行内容有误!","错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
}
private void showErrorStr(String str){
javax.swing.JOptionPane.showMessageDialog(this,str,"错误信息",javax.swing.JOptionPane.WARNING_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new JTableError().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private ErrorButton[] jButtons;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private int ROWCOUNT = 100;
private int COLUMNCOUNT = 4;
private int[][] errorLine;
// End of variables declaration//GEN-END:variables
class JTableErrorModel extends javax.swing.table.AbstractTableModel{
final String[] columnNames = {"Title 1", "Title 2", "Title 3", "Title 4"};
private Object[][] data = new Object[ROWCOUNT][COLUMNCOUNT];
public JTableErrorModel(){
for(int i = 0;i < ROWCOUNT;i++){
for(int j = 0;j < COLUMNCOUNT;j++){
data[i][j] = new String("");
}
}
}
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
public Object getValueAt(int rowIndex,int columnIndex){
return data[rowIndex][columnIndex];
}
public void setValueAt(Object aValue,int rowIndex,int columnIndex){
data[rowIndex][columnIndex] = aValue;
checkValid(rowIndex);
}
public boolean isCellEditable(int rowIndex,int columnIndex){
return true;
}
public int getColumnCount(){
return COLUMNCOUNT;
}
public int getRowCount(){
return ROWCOUNT;
}
private void checkValid(int rowIndex){
int errorCount = 0,j;
String a;
for(int i = 0;i < COLUMNCOUNT;i++){
a = (String)data[rowIndex][i];
for(j = 0;j < a.length();j++){
if(a.charAt(j) > '0' && a.charAt(j) < '9'){
errorCount++;
errorLine[rowIndex][i + 1] = 1;
break;
}
}
if(j == a.length()) errorLine[rowIndex][i + 1] = 0;
}
if(errorCount == 0){
errorLine[rowIndex][0] = 0;
setButtonString(rowIndex,true);
}else{
errorLine[rowIndex][0] = 1;
setButtonString(rowIndex,false);
}
}
}
class ErrorButton extends javax.swing.JButton{
private int row;
public ErrorButton(int r){
this.row = r;
setFont(new java.awt.Font("Dialog", 1, 10));
setText("OK");
setPreferredSize(new java.awt.Dimension(80, 16));
addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
if(errorLine[row][0] == 0) return;
StringBuffer errorStr = new StringBuffer("以下列填写有误:\n");
for(int j = 1;j < COLUMNCOUNT + 1;j++){
if(errorLine[row][j] == 1){
errorStr.append("第"+j+"列\n");
}
}
showErrorStr(new String(errorStr));
}
});
}
}
}
#8
多谢楼上的!
错误显示是我要的!但是我想让每一个错误的Cell旁都有一个error icon,而不是每一行就一个。
多谢啊!结贴时一定给分!!
错误显示是我要的!但是我想让每一个错误的Cell旁都有一个error icon,而不是每一行就一个。
多谢啊!结贴时一定给分!!
#9
老大,我看见你的帖子了,俺们来up下了,高手多多指教,我也省心学习了。
#10
哈哈!继续努力学习!也期待高手帮忙!
#11
学习学习
#12
给你个网址,你要说这个不行,那我实在就没办法了。
http://www.senun.com/Left/Programming/Java_old/Examples_swing/JTableExamples8.html
http://www.senun.com/Left/Programming/Java_old/Examples_swing/JTableExamples8.html
#13
客户要求原文如下:
On closing the error message window,the user is returned to the screen
in which problems are clearly indicated by means of text color red and an error icon.
when the user clicks one of the error icons adjacent to an entry field in which
an error has occured,the error message related to the specific field is displayed
in a pop-up window.
On closing the error message window,the user is returned to the screen
in which problems are clearly indicated by means of text color red and an error icon.
when the user clicks one of the error icons adjacent to an entry field in which
an error has occured,the error message related to the specific field is displayed
in a pop-up window.
#14
老大啊,怎么没有人来顶下了哦,高手快来帮忙了,学习的真苦啊!