Skip to content

Instantly share code, notes, and snippets.

@miracleyoo
Last active December 12, 2019 04:51
Show Gist options
  • Save miracleyoo/8ea66dfd11b52d517e9223a408678f5c to your computer and use it in GitHub Desktop.
Save miracleyoo/8ea66dfd11b52d517e9223a408678f5c to your computer and use it in GitHub Desktop.
[Swing Table Utils] #Java #GUI #Swing
/**
* This table utils provide the function of setting the width of each column of a Java Swing table.
* It can Set one column size, or set the preferred or minimum column size.
* Also, it provide the function that allow us to set different color for a certain cell in table.
* */
package com.miracleyoo.utils;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
public class TableUtils {
// -- Color and theme controlling part --
// Whether using Dark Mode or not(Light Mode)
private static boolean DarkMode = false;
// Color theme used in cycle table and Tomasulo diagram
private static final String[] colorSchemeCycleLight = new String[]{"#F8C3CD", "#FFE2C9", "#A9D5D7", "#CEF1BE", "#BBB6E0"};
private static final String[] colorSchemeCycleDark = new String[]{"#FFAC5E", "#4ACFAC", "#7E8CE0", "#3DC7D0", "#FFA48E"};
// Current using UI/Diagram color theme
public static String[] colorSchemeCycleCur = DarkMode ? colorSchemeCycleDark : colorSchemeCycleLight;
public static void setOneColumnSize(JTable table, int i, int preferedWidth, int maxWidth, int minWidth) {
// The column model of the table
TableColumnModel cm = table.getColumnModel();
// Get the i-th column
TableColumn column = cm.getColumn(i);
column.setPreferredWidth(preferedWidth);
column.setMaxWidth(maxWidth);
column.setMinWidth(minWidth);
}
public static void setAllPreferredColumnSize(JTable table, int[] Widths) {
int i = 0;
for (int width:Widths){
TableColumnModel cm = table.getColumnModel();
TableColumn column = cm.getColumn(i);
column.setPreferredWidth(width);
i++;
}
}
public static void setAllMinColumnSize(JTable table, int[] Widths) {
int i = 0;
for (int width:Widths){
TableColumnModel cm = table.getColumnModel();
TableColumn column = cm.getColumn(i);
column.setMinWidth(width);
i++;
}
}
public static class StatusColumnCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
//To highlight table components for the cycle graph, see below
//Cells are by default rendered as a JLabel.
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
//Get the status for the current row.
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
Color background_color = Color.WHITE;
if(tableModel.getValueAt(row, col) != null) {
// background_color = Color.decode(colorSchemeCycleCur[Integer.parseInt((String)tableModel.
// getValueAt(row, 1))%colorSchemeCycleCur.length]);
switch ((String) tableModel.getValueAt(row, col)) {
case "IF":
background_color = Color.decode(colorSchemeCycleCur[0]);
break;
case "ID":
background_color = Color.decode(colorSchemeCycleCur[1]);
break;
case "EX":
background_color = Color.decode(colorSchemeCycleCur[2]);
break;
case "MEM":
background_color = Color.decode(colorSchemeCycleCur[3]);
break;
case "WB":
background_color = Color.decode(colorSchemeCycleCur[4]);
break;
}
}
else{
background_color = Color.decode(colorSchemeMainCur[0]);
}
l.setBackground(background_color);
//Return the JLabel which renders the cell.
return l;
}
}
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////// USAGE ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
private TableUtils.StatusColumnCellRenderer cycleTableRender = new TableUtils.StatusColumnCellRenderer();
// Update the data model when data are updated
private void cycleTableUpdate() {
constructCycleFullData();
cycleModel.setDataVector(cycleFullData, cycleColumnNames);
cycleModel.fireTableDataChanged();
for (String cycleColumnName : cycleColumnNames) {
CycleTable.getColumn(cycleColumnName).setCellRenderer(cycleTableRender);//new TableUtils.StatusColumnCellRenderer());
}
TableUtils.setAllMinColumnSize(CycleTable, cycleColumnWidths);
TableUtils.setAllPreferredColumnSize(CycleTable, cycleColumnWidths);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment