- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,我处于这种情况...我的类(class)中有渲染器,但不知道如何使用它使某些单元格的背景变成红色。这是一个房间租赁应用程序,我有 Jtable 作为日历,所以我想要将租赁的单元格涂成红色。所以它应该以某种方式采用特定的列和行并使该单元格变为红色。我的渲染器在下面,但正如我所说,不知道如何使用它,因为我是 java 新手。真正的问题是我如何传递该列和行,我对此有疑问。渲染的单元格可以与其他一些代码一起使用,但这不是我需要的。
ublic class TableColour extends javax.swing.table.DefaultTableCellRenderer {
@Override
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cellComponent.setBackground(java.awt.Color.RED);
return cellComponent;
}
}
最佳答案
Alright oh wow I might have some trouble figuring this out. But maybe somehow. You said you dont know how my code looks, well I have some basic renderer. One thing to have in mind I have 2 dimensional array ReservedOne which holds row index and column index of taken room also room number date, time until which its reserved. So now Im a bit confused when looking at your example how to use my array to set colors. I hope I wont have mental breakdown
您的 TableModel
应该对这些数据进行建模,这非常重要,因为它允许 API 的其余部分围绕它展开
Real question how do I pass that column and row, I have problem with that. Cell rendered worked with some other code but that wasnt what I need.
这就是为什么让 TableModel
包装数据很重要,因为表 API 会将 row
和 column
信息传递给TableCellRenderer
,但它也会传递单元格值!
public class RoomTableModel extends AbstractTableModel {
private Room[][] reservations;
public RoomTableModel(Room[][] reservations) {
this.reservations = reservations;
}
@Override
public int getRowCount() {
return reservations.length;
}
@Override
public int getColumnCount() {
return reservations[0].length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return reservations[rowIndex][columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return Room.class;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (aValue instanceof Room) {
Room room = (Room) aValue;
reservations[rowIndex][columnIndex] = room;
fireTableCellUpdated(rowIndex, columnIndex);
}
}
}
这意味着我们现在可以设置单元格渲染器来显示我们需要的信息
公共(public)静态类 RoomTableCellRenderer 扩展了 DefaultTableCellRenderer {
private static Color BOOKED_COLOR = Color.RED;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Room && value != null) {
if (isSelected) {
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
// Update the details based on the room properties
} else { //if (value == null) {
setBackground(BOOKED_COLOR);
setText(null);
}
return this;
}
}
不要忘记,如果您希望表格使用您的渲染器,您需要注册它...
table.setDefaultRenderer(Room.class, new RoomTableCellRenderer());
基于存储在二维 String
数组中的可用数据(你真的不喜欢我)。
这有点脏。实际上,数据应该以可以传递到 TableModel 并让它处理细节的方式设置。您还必须小心更新数组的方式,因为只有在您可以强制刷新表之前,表才会反射(reflect)更新......这不会很漂亮。
public class LocalDateTableCellRenderer extends DefaultTableCellRenderer {
protected static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd");
private String[][] bookings;
public LocalDateTableCellRenderer(String[][] bookings) {
this.bookings = bookings;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
if (value instanceof LocalDate) {
LocalDate date = (LocalDate) value;
if (hasBookingFor(date)) {
setForeground(Color.WHITE);
setBackground(Color.RED);
}
setText(formatter.format(date));
} else {
setText(null);
}
return this;
}
protected boolean hasBookingFor(LocalDate date) {
for (String[] data : bookings) {
int day = Integer.parseInt(data[2]);
int month = Integer.parseInt(data[3]);
int year = 2017; // Because :P
LocalDate booking = LocalDate.of(year, month, day);
if (booking.isEqual(date)) {
return true;
}
}
return false;
}
}
基本上,这允许您将预订信息传递给 TableCellRenderer
,正如我所说,这不是您真正应该这样做的方式,但它需要对您的代码使其正常工作。
现在,我创建一个 TableModel
,它基本上采用年份和月份值,并为每个单元格返回一个 LocalDate
(如果是,则返回 null
)单元格超出月份范围)
public class CalendarModel extends AbstractTableModel {
public static String[] COLUMN_NAMES = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private int rows = 0;
private LocalDate startOfCalendar;
private LocalDate firstDayOfMonth;
private LocalDate lastDayOfMonth;
public CalendarModel(int year, Month month) {
firstDayOfMonth = LocalDate.of(year, month, 1);
startOfCalendar = firstDayOfMonth.minusDays(firstDayOfMonth.getDayOfWeek().getValue());
lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(startOfCalendar.getDayOfWeek());
System.out.println(firstDayOfMonth);
System.out.println(lastDayOfMonth);
Duration between = Duration.between(startOfCalendar.atStartOfDay(), lastDayOfMonth.atStartOfDay());
long days = between.toDays();
rows = (int) Math.round(days / 7d) + 1;
}
@Override
public int getRowCount() {
return rows;
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return LocalDate.class;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
LocalDate date = null;
if (startOfCalendar != null) {
int day = (rowIndex * 7) + columnIndex;
date = startOfCalendar.plusDays(day);
if (date.isBefore(firstDayOfMonth) || date.isAfter(lastDayOfMonth)) {
date = null;
}
}
return date;
}
}
这意味着 TableCellRenderer
被传递了 null
值或 LocalDate
值,使用此信息,您需要搜索您的用于指定日期的任何可能预订的数组。
这会严重扩展,这就是为什么我避免这样做并一直试图让您改变管理数据的方式,但事实就是这样
最后是一个非常粗略的例子......
此示例并不真正关心您将管理的所有信息,它只关心月份和日期信息
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
String[][] bookings = new String[7][6];
bookings[0][2] = "5";
bookings[0][3] = "4";
bookings[1][2] = "10";
bookings[1][3] = "4";
bookings[2][2] = "15";
bookings[2][3] = "4";
bookings[3][2] = "20";
bookings[3][3] = "4";
bookings[4][2] = "25";
bookings[4][3] = "4";
bookings[5][2] = "30";
bookings[5][3] = "4";
bookings[6][2] = "5";
bookings[6][3] = "5";
TableModel model = new CalendarModel(2017, Month.APRIL);
JTable table = new JTable(model);
table.setDefaultRenderer(LocalDate.class, new LocalDateTableCellRenderer(bookings));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
关于 java 。如何绘制 JTable 中的特定单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43349420/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!