- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基于this我这样做的问题:
public class ShowTicketPlanning extends javax.swing.JFrame {
private static String prevTicket="";
private static int color=1;
public ShowTicketPlanning() {
initComponents();
for (int i = 0; i < C.GameList.size(); i++) {
gameSelectGameCB.addItem(C.GameList.get(i).printForList());
}
}
private void gameSelectGameCBActionPerformed(java.awt.event.ActionEvent evt) {
int ind = gameSelectGameCB.getSelectedIndex();
if (C.GameList.size() > 0) {
DefaultTableModel tmodel = (DefaultTableModel) dbgPlanTBL.getModel();
while (tmodel.getRowCount() > 0) {
tmodel.removeRow(0);
}
String columnNames[] = new String[C.DBGList.get(ind).getDrawDayList().size()];
int gameDrawSize = C.DBGList.get(ind).getDrawDayList().size();
for (int z = 0; z < gameDrawSize; z++) {
columnNames[z] = C.DBGList.get(ind).getDrawDayList().get(z).getDrawNumber() + ":" + C.DBGList.get(ind).getDrawDayList().get(z).getCdc() + ":" + ES.dayToShortDay(C.DBGList.get(ind).getDrawDayList().get(z).getDay());
}
String dataValues[][] = {};
DefaultTableModel model = new DefaultTableModel(dataValues, columnNames);
dbgPlanTBL.setModel(model);
for (int t = 0; t < C.TicketList.size(); t++) {//for all tickets
for (int b = 0; b < C.TicketList.get(t).getBoardList().size(); b++) {
Object[] ob = new Object[gameDrawSize];
for (int brpd = 0; brpd < C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().size(); brpd++) {
int d = C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getDrawIndex();
if (C.DBGList.get(ind).isHasRaffle()) {
ob[d] = C.TicketList.get(t).getTicketRefName() + ":" + b
+ ":" + C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getNumberResult()
+ ":" + C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getRaffleResult();
} else {
ob[d] = C.TicketList.get(t).getTicketRefName() + ":" + b
+ ":" + C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getNumberResult();
}
}
model.addRow(ob);
}
}
DefaultTableModel amodel = (DefaultTableModel) dbgPlanTBL.getModel();
int tableSize = dbgPlanTBL.getRowCount();
//System.out.println("rows:"+tableSize);
int tableCol = dbgPlanTBL.getColumnCount();
//System.out.println("cols:"+tableCol);
for (int r = 0; r < tableSize; r++) {
for (int col = 0; col < tableCol; col++) {
if (amodel.getValueAt(r, col) == null) {
} else {
dbgPlanTBL.setDefaultRenderer(Object.class, new ShowTicketPlanning.CustomRenderer());
dbgPlanTBL.getCellEditor(r, col).getTableCellEditorComponent(dbgPlanTBL, dbgPlanTBL.getValueAt(r, col), true, r, col).setForeground(Color.red);
}
}
}
//define column width..............
TableColumnModel tcm = dbgPlanTBL.getColumnModel();
for (int i = 0; i < (tcm.getColumnCount()); i++) {
tcm.getColumn(i).setPreferredWidth(120);
}
dbgPlanTBL.setDefaultRenderer(Object.class, new ShowTicketPlanning.CustomRenderer());
}
}
class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value == null) {
setBackground(new java.awt.Color(255, 255, 255));
} else {
StringTokenizer st2 = new StringTokenizer(value.toString(), ":");
String ref = st2.nextElement().toString();
if (prevTicket.equals(ref)) {
if (color == 1) {
setBackground(new java.awt.Color(204, 204, 204));
} else {
setBackground(new java.awt.Color(255, 255, 255));
}
} else {
if (color == 1) {
color = 2;
} else {
color = 1;
}
if (color == 1) {
setBackground(new java.awt.Color(204, 204, 204));
} else {
setBackground(new java.awt.Color(255, 255, 255));
}
}
prevTicket=ref;
}
return c;
}
}
所以我有一张票,每张票都有多个板。我想改变每张票的颜色。
结果:
颜色是正确的,但是滚动后颜色变得不规则,如下图所示。
注意:每个“ticket0、ticket1、ticket2”等应具有交替颜色。
最佳答案
请随意查看此代码片段如何解决此问题并将其应用到您自己的程序中。
我必须否认这一点,因为我不确定这是最好的方法,但它的工作原理是它没有您所描述的奇怪行为(行着色滚动时变得困惑)。我还添加了一些代码来对事件进行排序和插入/删除/更新行。
我的代码片段跟踪数组中的颜色,因此一旦为一行建立了颜色,就不会再次建立它。渲染器只是从颜色存储中获取颜色。
有关该代码段的作用的说明,请参阅里面的注释。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableWithAlternatingColorsExample {
@SuppressWarnings("serial")
private static class AlternatingColorOnKeyRenderer extends DefaultTableCellRenderer {
/* The alternating colors */
public static final Color color1 = new Color(204,204,204);
public static final Color color2 = new Color(255,255,255);
/** The model index of the key column, ie the model column from which the key is derived. */
public static final int modelKeyColId = 0;
/** The color store that stores the colors for each row in the view.
* Indexed with view index to retrieve color for a row in the view. */
private ArrayList<Color> colorStore = new ArrayList<>( );
/** Reset all colors in the color store. */
public void resetAllColors() {
colorStore = new ArrayList<>(Math.max(10,colorStore.size()));
}
/** Reset all colors in the color store from a certain view index on. */
public void resetColorsFrom(int row) {
if(row==0)
resetAllColors();
else {
ArrayList<Color> retainedColors = new ArrayList<>(Math.max(10,colorStore.size()));
retainedColors.addAll(colorStore.subList(0,Math.min(row-1,colorStore.size()-1))); // copy retained colors
colorStore = retainedColors;
}
}
/** Determines the key value from a cell value. */
private Object getCellKeyVal(Object cellValue) {
return new StringTokenizer(cellValue.toString(),":").nextElement();
}
/** Retrieves the key value for a certain row. */
private Object getRowKeyVal(JTable t,int viewRowId) {
int modelRowId = t.convertRowIndexToModel(viewRowId); // convert row view index to model index to index the model
Object cellValue = t.getModel().getValueAt(modelRowId,modelKeyColId); // get cell value from model, because the column might not be present in the view
return getCellKeyVal(cellValue);
}
/** Prepares the color store for a certain row in the view.
* After calling this method colors for the range [0,row] will be present in the color store.
* Call this method to ensure that <i>colorStore.get(row)</i> will return a color. */
private void prepareColorStore(JTable t,int row) {
if(colorStore.size()>row) return; // color already present for row
if(colorStore.isEmpty()) colorStore.add(color1); // if color store empty, start with the first color
if(row==0) return;
Object curKeyVal = getRowKeyVal(t,colorStore.size()-1);
while(colorStore.size()<=row) { // add colors until color present for row
Object prevKeyVal = curKeyVal;
curKeyVal = getRowKeyVal(t,colorStore.size());
if(prevKeyVal.equals(curKeyVal)) // compare key values between current and previous row
colorStore.add(colorStore.get(colorStore.size()-1)); // add previous color in store when key values are equal
else if(colorStore.get(colorStore.size()-1)==color1) // reference compare because color instances are fixed
colorStore.add(color2); // add alternating color when key values differ
else
colorStore.add(color1);
}
}
@Override
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {
Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if(!table.isRowSelected(row)) {
prepareColorStore(table,row); // ensure color is present for row
c.setBackground(colorStore.get(row)); // set color from color store
}
return c;
}
};
private static JTable createTable() {
Vector<Vector<Object>> rowData = new Vector<>( Arrays.asList(
new Vector<>(Arrays.asList(new Object[]{"ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::"}))
));
Vector<Object> colData = new Vector<>(Arrays.asList(new Object[]{"1:103:Wed","2:106:Sat","3:110:Wed","4:110:Sat","5:117:Wed","6:120:Sat","7:124:Wed","8:127:Sat"}));
final JTable t = new JTable(rowData,colData);
DefaultTableCellRenderer tcr = new AlternatingColorOnKeyRenderer();
Enumeration<TableColumn> e = t.getColumnModel().getColumns();
while(e.hasMoreElements())
e.nextElement().setCellRenderer(tcr); // all table columns share the same AlternatingColorOnKeyRenderer instance
t.setAutoCreateRowSorter(true);
/* When the row sorter changes, reset all colors in the color store.
* When row sorting changes, the row view indices will point to different rows in the model, so the color store needs to be reset. */
t.getRowSorter().addRowSorterListener(new RowSorterListener() {
@Override
public void sorterChanged(RowSorterEvent e) {
((AlternatingColorOnKeyRenderer) t.getColumnModel().getColumn(0).getCellRenderer()).resetAllColors(); // all rows share the same AlternatingColorOnKeyRenderer instance, so pick the renderer from the first column and reset color store
}
});
/* Add a model listener to listen for inserted/deleted/updated rows. When rows are inserted/deleted/updated, the row colors could change for subsequent rows.
* Determines the minimum view index for the inserted/deleted/updated rows, then resets the color store from that view index on. */
t.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
int minViewId;
switch(e.getType()) {
case TableModelEvent.INSERT:
case TableModelEvent.DELETE:
minViewId = Integer.MAX_VALUE;
for(int r=e.getFirstRow();r<=e.getLastRow();++r) { // determine minimum view index for inserted/deleted rows
int viewId=t.convertRowIndexToView(r); // convert model index to view index
if(viewId>=0 && viewId<minViewId) // due to filtering, it is possible that inserted/deleted rows are not visible in the view (convert will return -1 in that case)
minViewId=viewId;
}
if(minViewId!=Integer.MAX_VALUE) // if any of the inserted/deleted rows were visible, reset the color store from the minimum view index on
((AlternatingColorOnKeyRenderer) t.getColumnModel().getColumn(0).getCellRenderer()).resetColorsFrom(minViewId);
break;
case TableModelEvent.UPDATE:
int r = e.getFirstRow();
if(r==TableModelEvent.HEADER_ROW)
return; // outside scope for this example
if(e.getColumn()!=TableModelEvent.ALL_COLUMNS && e.getColumn()!=AlternatingColorOnKeyRenderer.modelKeyColId)
return; // only listen for updates in the key column
minViewId = Integer.MAX_VALUE;
for( ;r<=e.getLastRow();++r) { // determine minimum view index for updated rows
int viewId=t.convertRowIndexToView(r); // convert model index to view index
if(viewId>=0 && viewId<minViewId) // due to filtering, it is possible that updated rows are not visible in the view (convert will return -1 in that case)
minViewId=viewId;
}
if(minViewId!=Integer.MAX_VALUE) { // if any of the updated rows were visible, reset the color store from the minimum view index on & repaint
((AlternatingColorOnKeyRenderer) t.getColumnModel().getColumn(0).getCellRenderer()).resetColorsFrom(minViewId);
t.repaint(); // repaint necessary for UPDATE events to update colors in the view
}
break;
}
}
});
return t;
}
protected static JButton createAddButton(final JTable t) {
JButton button = new JButton("Add row randomly");
button.addActionListener(new ActionListener() {
@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel dtm = (DefaultTableModel) t.getModel();
Vector<Object> newRow = new Vector<>(Arrays.asList(new Object[]{"ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::"}));
int index = new Random().nextInt(t.getRowCount()+1); // random index to insert
if(index<t.getRowCount())
dtm.getDataVector().add(index,newRow); // use the dataVector from DefaultTableModel to insert
else
dtm.getDataVector().add(newRow);
dtm.fireTableRowsInserted(index,index); // notify the view that a row was inserted
}
});
return button;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JTable t = createTable();
f.add(new JScrollPane(t),BorderLayout.CENTER);
f.add(createAddButton(t),BorderLayout.SOUTH);
f.setSize(500, 500);
f.setVisible(true);
}
});
}
}
关于java - 根据列值问题更改JTable行的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35228957/
我正在使用 CSS background: url(stuffhere.jpeg)对于我的背景,但是当你点击其他视频时,“元素”不是页面,背景不会改变。 我试过了 和 ,并尝试为#home 和#pr
这两个 CSS 属性有区别吗: background: none; background: transparent; 它们都有效吗? 应该使用哪一个,为什么? 最佳答案 它们之间没有区别。 如果您没有
csslint 警告回退背景(十六进制或 RGB)应该在 RGBA 背景之前。"evidence="background: rgba(0, 0, 0, 0.8);/* FF3+,Saf3+,Opera
我在我正在制作的新网站上使用 Flip 插件: http://www.concept-it.be/padre (点击联系人,然后点击电子邮件地址)。 正如你所看到的,当翻转开始后,div 的背景变成灰
有没有办法使用“前后”图像作为全尺寸背景?我想会很棒!我正在尝试将此类示例用作整页大小的图像; http://www.catchmyfame.com/2009/06/25/jquery-beforea
我认为答案是否定的,但是... 有没有办法说: background-size: contain 90% 所以它的作用正是 contain 会做的,但是然后将它调整得更小一些? 最佳答案 理想的解决方
将鼠标悬停在给定文本的每个字母上将更改文本的整个字体 + 正文背景颜色。我试过了,但我的尝试失败了。相反,字体只在被悬停的字母之后发生变化,我什至不知道如何从 div 选择器中影响正文背景颜色。 .h
我想给我的 UITableView 提供背景图片,所以我尝试了这个方法: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional
我正在尝试使用 Python 3.6 使用 PIL/Numpy(每个屏幕截图~0.01s)快速截取准备处理的屏幕截图。理想情况下,窗口不需要位于前台,即即使另一个窗口覆盖它,屏幕截图仍然成功。 到目前
我正在尝试做一些可能不可能的事情,但让我们看看你怎么想。这是我的代码: html { background: url(../img/pattern.png) repeat, url(../im
一位设计师想出了这种类型的背景,如下图所示。我想避免使用图像背景。因此,如果可以使用 CSS background 属性复制它,我会努力思考。 最底层只是一个线性渐变,没有问题。但是在其之上分层的圆形
当 TreeView(或应用程序)失去焦点时,如何更改所选 TreeViewItem 的背景。在这种情况下,默认情况下选定的项目具有浅灰色背景。 编辑:第一个答案后的尝试:但是找不到带有 Target
一位设计师想出了这种类型的背景,如下图所示。我想避免使用图像背景。因此,如果可以使用 CSS background 属性复制它,我会努力思考。 最底层只是一个线性渐变,没有问题。但是在其之上分层的圆形
我需要有一个带有 CSS 的背景作为附加的图像我不能让它与线性渐变一起工作。 我正在尝试以下操作,但我无法仅创建 1 个白色条纹。 div { background: #5cbcb0; bac
我有一个ListView,它有一个页眉和页脚。它们在 CardView 中的布局。以及其中必须为背景的内容列表。这是一张可以清楚看到的图片:我现在是这样的: 以及如何做: 我这样做了,ScrollVi
我目前有一个 DIV,其背景图像设置如下: background: url(../images/site/common/body-bannar-bkground.png) repeat 0 0; 如何
我有一个 slider ,需要在不使用 .style.backgroundImage 的情况下更改背景。那么我该如何通过向 slider 或其他东西添加一些类来做到这一点呢? 'use strict'
好的,所以在 photoshop 中,我创建了一个具有透明背景和一些文本的 8 位彩色图像。然后我创建了一个具有透明背景和一些文本的 16 位颜色的图像。 当我右键单击两个图像并转到属性时,它显示两个
我有一个问题困扰着我,我似乎在 Google 上找不到答案。我用一段代码创建了一个小型测试应用程序,它执行如下操作: 在 MainActivity 中,我创建了一个 SomeClass 的实例,它有一
我想做这个, 在 Android Studio 的预览中看起来不错,但在运行时我得到这个 正如您在屏幕开头看到的那样,颜色是白色,我想添加我自己的颜色,在本例中为绿色。 最初它使用的是 Cordina
我是一名优秀的程序员,十分优秀!