java 。如何绘制 JTable 中的特定单元格?

标签 java swing colors jtable tablecellrenderer

好吧,我处于这种情况...我的类(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 会将 rowcolumn 信息传递给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 值,使用此信息,您需要搜索您的用于指定日期的任何可能预订的数组。

这会严重扩展,这就是为什么我避免这样做并一直试图让您改变管理数据的方式,但事实就是这样

最后是一个非常粗略的例子......

此示例并不真正关心您将管理的所有信息,它只关心月份和日期信息

Example

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/

相关文章:

ios - 使用自定义颜色设置渐变

java - 当默认类中有公共(public)方法时,为什么编译器不会给出错误

java - Swing 应用程序不运行

java - 获取 JScrollPane 中包含的 JList

Java Swing 对话框问题

java - 如何使用 HTML 颜色通过 zxing 编写二维码

android:微调器的每个项目都有不同的颜色

java - ExecutorService 没有关闭

java - 理解Java中的this关键字

java - Gradle srcDirs 路径