Java 如何在一个jTable中获取所有具有一条共享信息的不同行?
我是新来的,也不熟悉编程,所以如果我不直呼事物的真名,我很抱歉 因此,我正在使用一个jTable,我试图让表格显示居住在同一个地方的所有人(在我的数据库中,我有两个人居住在“champa”地方),而不是只向我显示第一个在他们的“位置”atribute说地点的人。这是我正在使用的代码,它只向我显示第一个满足要求的人,而不是向我显示所有满足要求的人:Java 如何在一个jTable中获取所有具有一条共享信息的不同行?,java,swing,jtable,Java,Swing,Jtable,我是新来的,也不熟悉编程,所以如果我不直呼事物的真名,我很抱歉 因此,我正在使用一个jTable,我试图让表格显示居住在同一个地方的所有人(在我的数据库中,我有两个人居住在“champa”地方),而不是只向我显示第一个在他们的“位置”atribute说地点的人。这是我正在使用的代码,它只向我显示第一个满足要求的人,而不是向我显示所有满足要求的人: private void cargarLocalidad() { dtm.setRowCount(0); HabitanteADO h
private void cargarLocalidad() {
dtm.setRowCount(0);
HabitanteADO h_ado = new HabitanteADO();
Habitante habi = null;
String [] habitantes = new String[7];
String localidad = txtBuscar.getText().trim().toLowerCase();
if(localidad.length() == 0) {
ArrayList<Habitante> arregloHabitante = h_ado.readByAll();
for(Habitante h : arregloHabitante) {
habitantes[0] = h.getRut();
habitantes[1] = h.getNombre();
habitantes[2] = h.getGenero();
habitantes[3] = h.getFecha().toString();
habitantes[4] = h.getDireccion();
habitantes[5] = h.getLocalidad().toLowerCase();
habitantes[6] = String.valueOf(h.getVulnerabilidad());
dtm.addRow(habitantes);
txtBuscar.setText("");
txtBuscar.requestFocus();
}
} else {
habi = h_ado.readByLocalidad(localidad);
if(habi != null) {
habitantes[0] = habi.getRut();
habitantes[1] = habi.getNombre();
habitantes[2] = habi.getGenero();
habitantes[3] = habi.getFecha().toString();
habitantes[4] = habi.getDireccion();
habitantes[5] = habi.getLocalidad();
habitantes[6] = String.valueOf(habi.getVulnerabilidad());
dtm.addRow(habitantes);
txtBuscar.setText("");
txtBuscar.requestFocus();
} else
mensaje("La localidad que busca no existe!");
txtBuscar.setText("");
txtBuscar.requestFocus();
}
}
private void cargarLocalidad(){
dtm.setRowCount(0);
HabitanteADO h_ado=新的HabitanteADO();
Habitante habi=null;
字符串[]居住者=新字符串[7];
字符串localidad=txtBuscar.getText().trim().toLowerCase();
如果(localidad.length()==0){
ArrayList arregloHabitante=h_ado.readByAll();
适用于(居住区h:arregloHabitante){
居住者[0]=h.getRut();
居住者[1]=h.getNombre();
habitantes[2]=h.Get慷慨解囊();
居住者[3]=h.getFecha().toString();
居住者[4]=h.getDireccion();
habitantes[5]=h.getLocalidad().toLowerCase();
habitantes[6]=String.valueOf(h.getVulnerabilidad());
dtm.addRow(居住者);
txtBuscar.setText(“”);
txtBuscar.requestFocus();
}
}否则{
habi=h_ado.readByLocalidad(localidad);
如果(habi!=null){
habitantes[0]=habi.getRut();
habitantes[1]=habi.getNombre();
habitantes[2]=habi.get慷慨();
habitantes[3]=habi.getFecha().toString();
habitantes[4]=habi.getDireccion();
habitantes[5]=habi.getLocalidad();
habitantes[6]=String.valueOf(habi.getVulnerabilidad());
dtm.addRow(居住者);
txtBuscar.setText(“”);
txtBuscar.requestFocus();
}否则
mensaje(“没有存在的地方!”);
txtBuscar.setText(“”);
txtBuscar.requestFocus();
}
}
代码中位于else之后的部分仅向我显示在文本字段中输入位置的第一个人。我需要它向我显示共享上述位置的所有人,因此如果我输入“champa”,我希望jTable能够加载我数据库中居住在那里的所有人,而不仅仅是第一个这样做的人。提前谢谢,这是我在这里的第一篇文章,所以我为任何错误感到抱歉 通过正确查询数据库中的信息,您应该能够在JTable中显示此信息,例如:
String sql = "SELECT * FROM MyTableName WHERE city='champa';"
您还可以使用JTable的TableRowSorter类以及RowFilter.regexFilter()方法来过滤JTable的全部内容,以便只显示所需的记录,例如,这里有一个方法可以帮您做到这一点(阅读JavaDoc):
/**
*确保提供的JTable的DefaultTableModel已包含数据
*调用此方法之前。
*
*JTable始终保留当前在其内部填充的数据
*表格模型。知道了这一点,理论上我们可以用所有
*然后,数据库表记录使用过滤器仅显示特定的
*我们想要的表格记录。这样我们就不必为数据库设置极点
*对于不同的记录,所有时间和记录的获取都非常重要
*增加。
*
*此方法将通过提供的JTable传递提供的筛选器文本
*并将强制该JTable仅显示记录(包含在该JTable中)
*当时的JTable),其中包含该特定文本。它的反应非常好
*很像JTable的搜索引擎。
*
*如果要再次显示最初包含的所有记录
*在提供的JTable中,只需在
*此方法的filterText参数。
*
*@param table(JTable)要在其上运行筛选器的JTable。
*
*@param searchCriteria(字符串)用于筛选JTable的文本。路过
*空字符串(“”)将强制表显示所有记录。有规律的
*表达式(RegEx)也可以在条件字符串中提供。如果
*通配符?或*在过滤器中提供
*不带任何正则表达式元字符的条件字符串,然后用于函数目的
*这两个通配符中的任意一个在遇到时转换为正则表达式。如果
*实际的正则表达式将用于组成搜索条件
*字符串,然后确保将endorseWildcards可选参数设置为
*布尔值是假的吗?和*通配符具有
*正则表达式中的不同含义,必须以不同方式处理。
*
*@param选项(可选-整数/布尔):
*
*byColumnNumber-(可选-整数-默认值为-1)默认值
*此方法过滤所有表列,但
*如果传递列,则可以是特定于列的
*此可选参数的编号。此参数
*仅接受一个文本列编号,该列编号
*表示虽然
*JTable被认为是第0列,对于此方法,它是
*视为第1列。
*
*endorseWildcards-(布尔)默认值为true(允许通配符)。如果是真的
*然后,当在中遇到通配符时
*自动转换的搜索条件字符串
*与它的正则表达式等价。两个通配符
*几乎总是足够执行任何任务
*搜索是必需的,而且通常非常容易
/**
* Make sure the supplied JTable's DefaultTableModel already contains data
* before calling this method.<br><br>
* <p>
* The JTable always retains the data it is currently filled with within its
* Table Model. <b>Knowing this we can theoretically fill the Table with all
* database table records then use the Filter to display only the specific
* table records we want.</b> This way we don't have to pole the database
* for different records all the time and records acquisition is greatly
* increased.<br><br>
* <p>
* This method will pass the supplied Filter text across the supplied JTable
* and will force that JTable to only display records (contained within that
* JTable at the time) which contains that specific text. It reacts very
* much like a search engine for the JTable.<br><br>
* <p>
* If you want to display all records again which were originally contained
* within the supplied JTable then just pass a Null String ("") within the
* filterText parameter of this method.<br><br>
*
* @param table (JTable) The JTable to run filter on.<br>
*
* @param searchCriteria (String) The text to filter JTable with. Passing a
* Null String ("") will force the table to display all records. Regular
* Expressions (RegEx) can also be supplied within the criteria string. If
* the wildcard characters <b>?</b> or <b>*</b> are supplied within the filter
* criteria String without any RegEx meta characters then the functional purpose
* of these two wildcard characters are converted to RegEx when encountered. If
* actual Regular Expressions are going to be used to make up the search criteria
* string then be sure to set the <b>endorseWildcards</b> optional parameter to
* boolean false since the <b>?</b> and <b>*</b> wildcard characters have
* different meaning within a Regular Expression and must be handled differently.<br>
*
* @param options (optional - Integer/Boolean):<pre>
*
* byColumnNumber - (Optional - Integer - Default is -1) By default
* this method filters across all table columns but
* you can be column specific if you pass a column
* number to this optional parameter. This parameter
* accepts only a <b>Literal Column Number</b> which
* means that although the first column within a
* JTable is considered column 0, to this method it is
* considered as column 1.
*
* endorseWildcards - (boolean) Default is true (allow wildcards). If true
* then when a wildcard character is encountered within
* a search criteria string it is automatically converted
* to its RegEx equivalent. The two wildcard characters
* are almost always more than enough to carry out any
* search required and is usually much easier to use than
* some complex regular expressions. If endorseWildcards
* is true then upper or lower letter case is ignored as
* well.
*
* If you provide a true of false to this parameter then
* you must provide a value (or null) to the option
* <b>byColumnNumber</b> parameter.</pre>
*/
public static void filterTable(JTable table, String searchCriteria, Object... options) {
int column = -1;
boolean endorseWildcards = true;
if (options.length > 0) {
if (options[0] != null) {
column = (int)options[0] - 1;
}
if (options.length >= 2) {
if (options[1] != null) {
endorseWildcards = (boolean)options[1];
}
}
}
String criteria = searchCriteria;
if (endorseWildcards) {
criteria = "(?i)" + searchCriteria.replace("?", ".?").replace("*", ".*?");
}
try {
TableRowSorter<TableModel> sorter = new TableRowSorter<>(((DefaultTableModel) table.getModel()));
sorter.setRowFilter(column < 0 ? RowFilter.regexFilter(criteria) :
RowFilter.regexFilter(criteria, column));
table.setRowSorter(sorter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}