JavaでJTableの個々の列の背景/前景色を設定するにはどうすればよいですか?
JTable JComponentのサブクラスです 複雑なデータ構造を表示するためのクラス。 JTableコンポーネントは、 Model View Controller(MVC)デザインパターンに従うことができます 行と列にデータを表示するため。 JTableは、 TableModelListener、TableColumnModelListener、ListSelectionListener、CellEditorListener、RowSorterListenerを生成できます。 インターフェイス。 DefaultTableCellRenderer をカスタマイズすることで、JTableの各列の背景色と前景色を変更できます。 クラスであり、メソッドは1つだけです getTableCellRendererComponent() 実装します。
例
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableColumnColorTest extends JFrame {
private JTable table;
private TableColumn tColumn;
public JTableColumnColorTest() {
setTitle("JTableColumnColor Test");
table = new JTable(10, 5);
tColumn = table.getColumnModel().getColumn(2);
tColumn.setCellRenderer(new ColumnColorRenderer(Color.lightGray, Color.red));
add(new JScrollPane(table), BorderLayout.CENTER);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String [] args) {
new JTableColumnColorTest();
}
}
// Customize the code to set the background and foreground color for each column of a JTable
class ColumnColorRenderer extends DefaultTableCellRenderer {
Color backgroundColor, foregroundColor;
public ColumnColorRenderer(Color backgroundColor, Color foregroundColor) {
super();
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cell.setBackground(backgroundColor);
cell.setForeground(foregroundColor);
return cell;
}
} 出力
-
Javaで背景色をJPanelに設定するにはどうすればよいですか?
JPanel はコンテナであり、見えない コンポーネント Javaで。 FlowLayout JPanelのデフォルトのレイアウトです 。 ボタンなどのほとんどのコンポーネントを追加できます 、テキストフィールド、ラベル、テーブル、リスト、ツリー などをJPanelに 。 setBackground()を使用して、背景色をJPanelに設定できます。 メソッド。 例 import java.awt.* import javax.swing.*; public class JPanelBackgroundColorTest extends JFrame { p
-
matplotlibテーブルの列の背景色を設定するにはどうすればよいですか?
matplotlibテーブルの列の背景色を設定するには、次の手順を実行できます- 図のサイズを設定し、サブプロット間およびサブプロットの周囲のパディングを調整します。 列のタプルを作成します 属性。 リストのリストを作成します つまり、レコードのリストです。 リストのリストを作成します つまり、各セルの色です。 図とサブプロットのセットを作成します。 軸にテーブルを追加します。ax 。 軸をオフにします。 図を表示するには、 show()を使用します メソッド。 例 import matplotlib.pyplot as plt plt.r