- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的带有多个搜索和排序的简单 JTable 的代码。这可能是一个相对简单的问题,但是当我运行代码时,在“私有(private) DefaultTableModel 模型”中引用“aData”和“aHeaders”时出现错误。另外,在这个小问题解决之后,我还会遇到其他错误或异常吗? IE。空指针异常
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.event.*;
///////////
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.text.Document;
import javax.swing.table.TableRowSorter;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
///////////
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.table.*;
public class JtableIe
{
JFrame myMainWindow = new JFrame("Compare Tables");
JPanel firstPanel = new JPanel();
JScrollPane myScrollTable;
JTable myTable;
JTextField srchFld1;
JTextField srchFld2;
TableRowSorter sorter;
JLabel srchLbl1 = new JLabel();
JLabel srchLbl2 = new JLabel();
public void runGUI()
{
myMainWindow.setBounds(10, 10, 1296, 756);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel();
myMainWindow.getContentPane().add(firstPanel);
myMainWindow.setVisible(true);
}
public void createFirstPanel()
{
firstPanel.setLayout(null);
srchLbl1.setLocation(0,0);
srchLbl1.setSize(150,26);
srchLbl1.setText("Name Search:");
firstPanel.add(srchLbl1);
srchLbl2.setLocation(660,0);
srchLbl2.setSize(150,26);
srchLbl2.setText("ID Search:");
firstPanel.add(srchLbl2);
String[] aHeaders = {"Name","ID","Number 1","Number 2","Time","Date"};
Object[][] aData = new Object[15][6];
myTable = new JTable(model);
///////Data////////
aData[0][0] = "John";
aData[0][1] = "JS96";
aData[0][2] = "1";
aData[0][3] = "186";
aData[0][4] = "1h 23m";
aData[0][5] = getJavaDate("12-11-2015");
aData[1][0] = "David";
aData[1][1] = "DB36";
aData[1][2] = "2";
aData[1][3] = "111852";
aData[1][4] = "2h 55m";
aData[1][5] = getJavaDate("12-11-2020");
aData[2][0] = "Daniel";
aData[2][1] = "DK73";
aData[2][2] = "3";
aData[2][3] = "2921";
aData[2][4] = "1h 55m";
aData[2][5] = getJavaDate("12-11-2014");
aData[3][0] = "Janis";
aData[3][1] = "JW84";
aData[3][2] = "4";
aData[3][3] = "6512";
aData[3][4] = "12h 26m";
aData[3][5] = getJavaDate("13-11-2015");
aData[4][0] = "Adam";
aData[4][1] = "AF98";
aData[4][2] = "5";
aData[4][3] = "7524";
aData[4][4] = "5h 47m";
aData[4][5] = getJavaDate("11-11-2015");
aData[5][0] = "Matthew";
aData[5][1] = "MB55";
aData[5][2] = "6";
aData[5][3] = "4989";
aData[5][4] = "80h 30m";
aData[5][5] = getJavaDate("11-11-2015");
aData[6][0] = "Lewis";
aData[6][1] = "LS23";
aData[6][2] = "7";
aData[6][3] = "56321";
aData[6][4] = "880h 20m";
aData[6][5] = getJavaDate("11-11-2015");
aData[7][0] = "Mark";
aData[7][1] = "ML49";
aData[7][2] = "8";
aData[7][3] = "97456";
aData[7][4] = "8h 42m";
aData[7][5] = getJavaDate("11-11-2015");
aData[8][0] = "Josh";
aData[8][1] = "JT67";
aData[8][2] = "9";
aData[8][3] = "82135";
aData[8][4] = "56h 13m";
aData[8][5] = getJavaDate("11-11-2015");
aData[9][0] = "Oliver";
aData[9][1] = "OC98";
aData[9][2] = "10";
aData[9][3] = "152";
aData[9][4] = "9h 9m";
aData[9][5] = getJavaDate("11-11-2015");
aData[10][0] = "Tom";
aData[10][1] = "TS97";
aData[10][2] = "11";
aData[10][3] = "100";
aData[10][4] = "0h 0m";
aData[10][5] = getJavaDate("11-11-2015");
aData[11][0] = "Emma";
aData[11][1] = "EBS74";
aData[11][2] = "12";
aData[11][3] = "200";
aData[11][4] = "0h 30m";
aData[11][5] = getJavaDate("11-11-2015");
aData[12][0] = "Lucy";
aData[12][1] = "LK02";
aData[12][2] = "13";
aData[12][3] = "256";
aData[12][4] = "7h 00m";
aData[12][5] = getJavaDate("11-11-2015");
aData[13][0] = "Patrick";
aData[13][1] = "PM38";
aData[13][2] = "14";
aData[13][3] = "258";
aData[13][4] = "2h 00m";
aData[13][5] = getJavaDate("11-11-2015");
aData[14][0] = "Nicola";
aData[14][1] = "NB70";
aData[14][2] = "15";
aData[14][3] = "987";
aData[14][4] = "100h 56m";
aData[14][5] = getJavaDate("11-11-2015");
//////////////
myTable.setAutoCreateRowSorter(true);
sorter = new TableRowSorter(myTable.getModel());
List sortKeys = new ArrayList();
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);
sorter.setComparator(2, new AlphanumComparator());
sorter.setComparator(3, new AlphanumComparator());
sorter.setComparator(4, new AlphanumComparator());
setRenderers();
myTable.setRowSorter(sorter);
DefaultRowSorter sorter = (DefaultRowSorter) myTable.getRowSorter();
myScrollTable = new JScrollPane(myTable);
myScrollTable.setSize(1296,756);
myScrollTable.setLocation(0,25);
System.out.println("Creating compare table");
srchFld1 = new JTextField(10);
srchFld1.setSize(550,26);
srchFld1.setLocation(100,0);
srchFld1.setToolTipText("Enter Name");
firstPanel.add(srchFld1);
srchFld2 = new JTextField(10);
srchFld2.setSize(550,26);
srchFld2.setLocation(740,0);
srchFld2.setToolTipText("Enter ID");
firstPanel.add(srchFld2);
//////////////////////
Document doc = srchFld1.getDocument();
DocumentListener listener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
};
doc.addDocumentListener(listener);
///////////////
Document docb = srchFld2.getDocument();
DocumentListener listenerb = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
@Override
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
};
docb.addDocumentListener(listenerb);
///////////////
firstPanel.add(myScrollTable);
}
private DefaultTableModel model = new DefaultTableModel(aData, aHeaders) {
@Override
public Class<?> getColumnClass(int column)
{
return getValueAt(5, column).getClass();
}
};
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
private void setRenderers() {
myTable.setDefaultRenderer(Date.class, new DateRenderer());
}
private Date getJavaDate(String s) {
try
{
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date d = sdf.parse(s);
return d;
}
catch (ParseException ex)
{
Logger.getLogger(TableBasic.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
private void newFilter()
{
RowFilter rf = null;
try
{
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter(srchFld1.getText(), 0));
filters.add(RowFilter.regexFilter(srchFld2.getText(), 1));
rf = RowFilter.andFilter(filters);
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
public static void main(String[] args)
{
JtableIe ji = new JtableIe();
ji.runGUI();
}
private class DateRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable myTable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(myTable, value, isSelected, hasFocus, row, column);
if (!(value instanceof Date)) {
return this;
}
setText(DATE_FORMAT.format((Date) value));
return this;
}
}
}
AlphanumComparator 代码
/*
* The Alphanum Algorithm is an improved sorting algorithm for strings
* containing numbers. Instead of sorting numbers in ASCII order like
* a standard sort, this algorithm sorts numbers in numeric order.
*
* The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
import java.util.Comparator;
/**
* This is an updated version with enhancements made by Daniel Migowski,
* Andre Bogus, and David Koelle
*
* To convert to use Templates (Java 1.5+):
* - Change "implements Comparator" to "implements Comparator<String>"
* - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"
* - Remove the type checking and casting in compare().
*
* To use this class:
* Use the static "sort" method from the java.util.Collections class:
* Collections.sort(your list, new AlphanumComparator());
*/
public class AlphanumComparator implements Comparator
{
private final boolean isDigit(char ch)
{
return ch >= 48 && ch <= 57;
}
/** Length of string is passed in for improved efficiency (only need to calculate it once) **/
private final String getChunk(String s, int slength, int marker)
{
StringBuilder chunk = new StringBuilder();
char c = s.charAt(marker);
chunk.append(c);
marker++;
if (isDigit(c))
{
while (marker < slength)
{
c = s.charAt(marker);
if (!isDigit(c))
break;
chunk.append(c);
marker++;
}
} else
{
while (marker < slength)
{
c = s.charAt(marker);
if (isDigit(c))
break;
chunk.append(c);
marker++;
}
}
return chunk.toString();
}
public int compare(Object o1, Object o2)
{
if (!(o1 instanceof String) || !(o2 instanceof String))
{
return 0;
}
String s1 = (String)o1;
String s2 = (String)o2;
int thisMarker = 0;
int thatMarker = 0;
int s1Length = s1.length();
int s2Length = s2.length();
while (thisMarker < s1Length && thatMarker < s2Length)
{
String thisChunk = getChunk(s1, s1Length, thisMarker);
thisMarker += thisChunk.length();
String thatChunk = getChunk(s2, s2Length, thatMarker);
thatMarker += thatChunk.length();
// If both chunks contain numeric characters, sort them numerically
int result = 0;
if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
{
// Simple chunk comparison by length.
int thisChunkLength = thisChunk.length();
result = thisChunkLength - thatChunk.length();
// If equal, the first different number counts
if (result == 0)
{
for (int i = 0; i < thisChunkLength; i++)
{
result = thisChunk.charAt(i) - thatChunk.charAt(i);
if (result != 0)
{
return result;
}
}
}
} else
{
result = thisChunk.compareTo(thatChunk);
}
if (result != 0)
return result;
}
return s1Length - s2Length;
}
}
最佳答案
你不能像这样引用“模型”。
您的代码顺序错误。首先需要创建包含列名称的列数组,然后创建包含数据的数据数组。然后,您使用这些变量来创建表模型并将表模型添加到表中。
代码可能类似于:
String[] aHeaders = {"Name","ID","Number 1","Number 2","Time","Date"};
Object[][] aData = new Object[15][6];
///////Data////////
aData[0][0] = "John";
aData[0][1] = "JS96";
aData[0][2] = "1";
aData[0][3] = "186";
aData[0][4] = "1h 23m";
aData[0][5] = getJavaDate("12-11-2015");
...
DefaultTableModel model = new DefaultTableModel(aData, aHeaders)
{
@Override
public Class<?> getColumnClass(int column)
{
switch (column)
{
case 5: return Date.class;
default: return Object.class;
}
}
};
myTable = new JTable( model );
关于java - 在私有(private) DefaultTableModel 中引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29242197/
据我所知,在 C# 中,所有字段默认都是私有(private)的,如果没有另外标记的话。 class Foo { private string bar; } class Foo { strin
这个问题在这里已经有了答案: Why is it allowed to access Java private fields via reflection? (7 个答案) 关闭 6 年前。 使用反
在 C#(和许多其他语言)中,访问相同类型的其他实例的私有(private)字段是完全合法的。例如: public class Foo { private bool aBool; pu
使用私有(private)方法通过将一些决策点重构为单独的方法来降低 CC 会降低实际方法的 CC 并易于阅读,但不会减少在测试中获得完整分支覆盖的工作量。 这合理吗?你有什么现场经验? 最佳答案 好
在下面的例子中,模块outer有一个私有(private)类型Private和一个私有(private)内部模块inner。 inner 能够访问Private(因为子模块可以访问其父级的私有(pri
class Person def one @var = 99 self.two end private def two p @var end end p=P
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我正在尝试获得良好的 Ruby 编码风格。为防止意外调用具有相同名称的局部变量,我总是在适当的地方使用 self.。但是现在我偶然发现了这个: class MyClass "method" a
今天遇到一个案例类构造函数的奇怪问题。我想将构造函数设为私有(private),看来这不是问题。所以我已经在我的一个项目中尝试过它并且它有效。但在另一个项目中,我可以调用私有(private)构造函数
我想坚持使用记录,并且不想返回对象。所以我想知道是否可以将记录的字段设置为私有(private)?或者创建记录的私有(private)成员。其他具体类型(例如可区分联合)怎么样? 或者,这个要求是否违
我正在通过 Flickr API 进行经过身份验证的调用来访问照片。但我只得到我的公开照片,而没有任何私有(private)照片。 下面给出的是我正在使用的代码, Flickr f; Request
这两个类的行为不同;原因似乎与使用 private[this] 声明而不是 private 有关。有人可以解释一下为什么吗? 私有(private): class Person( private
在 private 中的 1000 秒 private 之后,我想到可能不需要它 public class Outer { private static class Inner { // yo
我有以下代码: class C { private enum E { // ... } } private extension C { func f(e: E)
OOP 语言中是否有object-private 的概念??我的意思是比经典的私有(private)访问限制更多? Private (or class-private) restricts the a
swift 3.0 我知道fileprivate访问级别修饰符将函数/属性的使用限制在声明它的源文件和 private - 仅限于声明的词法范围。但似乎这条规则不适用于扩展。例如。此代码有效: cla
即将推出的 Delphi 版本中启用该功能的功能怎么样? 也许它可能是一个编译器开关,促进所有 ** private **s to ** strict private **小号。 ... 或者它可能是
我可以通过将函数放入类的私有(private)扩展中来创建私有(private)函数,而不是通过不断调用 private func functionName(){} 来创建新的私有(private)函
部署专用端点并需要专用 IP 地址作为输出,但似乎无法正确获取值查询。下面的结果是“模板输出'主机名'无效:语言表达式属性|'privateIPAddress'具有无效的数组索引..(代码:Deplo
我是一名优秀的程序员,十分优秀!