gpt4 book ai didi

java - 有没有一种方法可以根据文本字段中输入的数据突出显示或为各个表行着色?

转载 作者:行者123 更新时间:2023-12-02 09:45:17 26 4
gpt4 key购买 nike

我想做的是突出显示我从一盒元素中扫描的序列号。

所以当我扫描序列号时。我的想法是包含盒子中元素的表格改变它们的颜色。我的英语很糟糕,所以我认为图像可以更好地解释我的想法。

所以我的代码执行以下操作:

首先,我使用框 1(“Gaveta”)中的信息加载表格:

http://prntscr.com/o4ykdy

然后当我过滤框中项目的序列号时:

http://prntscr.com/o4yly4

我面临我的问题,搜索绘制了正确的序列号,但它损坏了我的表模型。

我的代码:

数据搜索:

公共(public)无效buscarNumeroSerie(字符串nserie) { 尝试{

             String [] campos={"NUMERO_SERIE","MARCA","GAVETA"};
String filtroNS = nserie;
String NSSQL = "SELECT NUMERO_SERIE,MARCA,GAVETA FROM"
+ "(SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_wd "
+ "UNION "
+ "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_toshiba "
+ "UNION "
+ "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_seagate "
+ "UNION "
+ "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_samsung "
+ "UNION "
+ "SELECT NUMERO_SERIE,MARCA,GAVETA FROM discos_hitachi )"
+ "AS TROUBLE WHERE NUMERO_SERIE LIKE '%"+filtroNS+"%'";

System.out.println(NSSQL);
nsconn = metodosPool.dataSource.getConnection();
//ModeloTablaLista = new DefaultTableModel(null, campos);
stmnt = nsconn.prepareStatement(NSSQL);
ResultSet nsrs = stmnt.executeQuery(NSSQL);
String [] nsfila = null;
//if(nsrs.next()== true){
// String [] nsfila = new String[3];



while (nsrs.next())
{
nsfila = new String[3];
nsfila[0]=nsrs.getString("Numero_Serie");
nsfila[1]=nsrs.getString("Marca");
nsfila[2]=nsrs.getString("Gaveta");
// ModeloTablaLista.addRow(nsfila);
}

if (nsfila == null)
{
Object[] opcionesPurga = {"Agregar Disco Hitachi",
"Agregar Disco Toshiba",
"Agregar Disco Seagate",
"Agregar Disco Samsung",
"Agregar Disco WD",
"Omitir"};

int sinDiscoEnTabla = JOptionPane.showOptionDialog
(rootPane,
"Disco no encontrado, ¿que desea hacer?:",
"Disco no encontrado",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
opcionesPurga,
opcionesPurga[1]);

System.out.println(sinDiscoEnTabla);

switch (sinDiscoEnTabla){

case 0:
sinDiscoAddHi.setVisible(true);
break;

case 1:
sinDiscoAddTo.setVisible(true);
break;

case 2:
sinDiscoAddSe.setVisible(true);
break;

case 3:
sinDiscoAddHi.setVisible(true);
break;

case 4:
sinDiscoAddWD.setVisible(true);
break;

case 5:
JOptionPane.showMessageDialog(null,"Por favor ingrese un nuevo disco:");
tfNumeroSeriePurga.setText("");
break;
}
}
nsrs.close();
stmnt.close();
// tablaDiscosGaveta.setModel(ModeloTablaLista);
ClaseColor colorear = new ClaseColor(0);
tablaDiscosGaveta.getColumnModel().getColumn(0).setCellRenderer(colorear);
}

//else
// {

// System.err.println("No existen datos asociados");
// JOptionPane.showMessageDialog(rootPane, "Disco no encontrado, quiere:");
// }


catch (SQLException nseerr)
{
System.err.println(""+nseerr.getSQLState());
JOptionPane.showMessageDialog(null, "Error al buscar \n"
+nseerr, "Error en la operacion ", JOptionPane.ERROR_MESSAGE);
}

}

表格渲染器:

公共(public)类 ClaseColor 扩展了 DefaultTableCellRenderer {

private final int patron;  

public ClaseColor(int patron) {
this.patron = patron;
}

@Override
public Component getTableCellRendererComponent
(JTable tablaDiscosGaveta,
Object value,
boolean Selected,
boolean hasFocus,
int row,
int col)
{

if (!tfNumeroSeriePurga.getText().equals(tablaDiscosGaveta.getValueAt(row, patron).toString()))

{
System.out.println("FramePurgar.PurgarDiscos1.ClaseColor.methodName()"+"no pille nada !");
}
else
{
setForeground(Color.RED);
//setFont(font);
//
super.getTableCellRendererComponent(tablaDiscosGaveta, value, Selected, hasFocus, row, col);
return this;
}

返回此;

} }

我想做的是绘制与执行搜索的文本字段“序列号”匹配的每个序列号。

类似这样的事情:

http://prntscr.com/o4ynui

所以任何新的S/N我搜索,在我继续扫描S/N的同时得到一个标记,然后没有标记的S/N从表中删除。

最佳答案

因为您没有给我们 minimal-reproducible example我创建了自己的示例来向您展示如何做到这一点。对 JTable 使用下划线会很棘手,因为(我认为)需要 JTextPane。但是,通过突出显示表格单元格中的搜索文本来实现类似的效果会更容易。当然,为了做到这一点,我们需要创建自己的自定义 TableCellRenderer .

代码:

public class TableExample extends JFrame {
private static final Color HIGHLIGHT_COLOR = Color.GREEN;
private JTextField textField;

TableExample() {
String data[][] = { { "101", "Mike", "17" }, { "102", "Thomas", "21" }, { "103", "Brian", "42" }, { "104", "George", "24" } };
String column[] = { "ID", "NAME", "AGE" };

JPanel inputpanel = new JPanel(new BorderLayout(10, 10));

JTable table = new JTable();
JScrollPane sp = new JScrollPane(table); // You were not adding the sp into your panel, take care that next time.
DefaultTableModel dtm = new DefaultTableModel(data, column); // Create the model with our data.
table.setModel(dtm);
table.setDefaultRenderer(Object.class, new CellHighlightRenderer());

textField = new JTextField();
textField.getDocument().addDocumentListener(new DocumentListener() {

@Override
public void removeUpdate(DocumentEvent e) {
table.repaint();

}

@Override
public void insertUpdate(DocumentEvent e) {
table.repaint();
}

@Override
public void changedUpdate(DocumentEvent e) {
table.repaint();
}
});

inputpanel.add(textField, BorderLayout.PAGE_START);
inputpanel.add(sp);

add(inputpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
setLocationRelativeTo(null);
pack();
}

class CellHighlightRenderer extends JTextField implements TableCellRenderer {
public DefaultHighlighter high = new DefaultHighlighter();
public DefaultHighlighter.DefaultHighlightPainter highlight_painter = new DefaultHighlighter.DefaultHighlightPainter(HIGHLIGHT_COLOR);

public CellHighlightRenderer() {
setBorder(BorderFactory.createEmptyBorder());
setHighlighter(high);
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());

setFont(table.getFont());
setValue(value);
int pos = 0;
String word = textField.getText();
word = word.toLowerCase();// In order to improve search?
String stringValue = value.toString();
stringValue = stringValue.toLowerCase(); // In order to improve search?
if (!word.isEmpty()) {
if ((pos = stringValue.indexOf(word, pos)) >= 0) {
try {
high.addHighlight(pos, pos + word.length(), highlight_painter);
pos += word.length();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return this;
}

protected void setValue(Object value) {
setText((value == null) ? "" : value.toString());
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TableExample te = new TableExample();
te.setVisible(true);
});

}
}
<小时/>

预览:

enter image description here

关于java - 有没有一种方法可以根据文本字段中输入的数据突出显示或为各个表行着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56707880/

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