Java 更改单击的JTable单元格的背景颜色
我有两个问题 1.我想在鼠标单击时更改JTable单元格的颜色。我添加了一个监听器,比如在两种颜色之间切换,如果当前它是绿色的,它应该在其他单击时变为吹Java 更改单击的JTable单元格的背景颜色,java,swing,colors,jtable,Java,Swing,Colors,Jtable,我有两个问题 1.我想在鼠标单击时更改JTable单元格的颜色。我添加了一个监听器,比如在两种颜色之间切换,如果当前它是绿色的,它应该在其他单击时变为吹 String[][] data = new String[rows][columns]; DefaultTableModel model = new DefaultTableModel(data, header); JTable table = new JTable(model); table.addMouseListener(new Cell
String[][] data = new String[rows][columns];
DefaultTableModel model = new DefaultTableModel(data, header);
JTable table = new JTable(model);
table.addMouseListener(new CellClickListener());
// Using following MouseListener
public class CellClickListener extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
// How to getCell object here and change its background color to clicked cell
}
}
2.如何设置不同的列宽,如
firstColumn.setWidth(20)
,secondColumn.setWidth(40)
。我已经禁用了自动调整大小table.setAutoResizeMode(JTable.auto_resize_OFF)代码>您必须创建自己的CellRenderer,并使用MouseAdapter
这里有一个SSCCE来解释我的意思:
希望有帮助()
我已经为同一问题工作了几天。
我开发了一个通用函数
setColorAtCell(Jtable table,Color colr,int row,int column)
这样可以做得更好
下面是解决上述问题的代码
您还可以从我的
导入java.awt.Color;
导入java.awt.Component;
导入javax.swing.JOptionPane;
导入javax.swing.JTable;
导入javax.swing.table.DefaultTableCellRenderer;
公共类ChangeCellColorJTable扩展了javax.swing.JFrame{
公共静态颜色[]表_颜色=新颜色[40][40];
//此数组用于更改表格颜色
公共静态对象[][]表_值=新对象[40][40];
//此数组用于更改表值
公共ChangeCellColorJTable(){
初始化组件();
initTable();//初始化表
刷新表(mytable,1,1);
}
公共静态void initTable(){
int i,j;
对象[][]数据=新对象[][]{
{10,10},
{11,11},
{12,12},
{13,13}
};
对于(i=0;i<4;i++){
对于(j=0;j1.您必须重置SetBackdLond(table.getBackground())2.取决于您真正想做什么(对于列模型中的自动调整大小循环和设置首选大小),抱歉,我不明白。setBackground()在哪个对象上?看一看>我已经看到了,但在“渲染只在创建时起作用”,但我想使用侦听器更改单击事件的背景。对于1,我仍然不知道(我正在寻找答案),但是对于2:看一看-看问题的代码(SetPreferredWidth)。可能不是最好的选择,但它很有效。请在回答中描述您的方法,并包括相关的代码段。一旦您将代码放在此处,我将投反对票(堆栈溢出)加入你的答案。拥有外部链接不是一个好主意。正如Maciej Lach所说,描述你的代码的作用。请从答案中去掉你的名字,它已经出现在你的个人资料中。顺便说一句,你为什么要感谢我们?这并不能真正回答问题。如果你有不同的问题,你可以通过单击来提问。你也可以一旦你有足够的时间,请注意这个问题。这太粗鲁了。无论如何,我已经根据你的评论改变了。@Frakcool我已经删除了“谢谢”和“我的名字”的链接。对不起,我是stackoverflow的新手。我几乎不知道这里有任何书面答案。如果现在答案中有任何错误,请纠正我。
setColorAtCell(Jtable table,Color colr,int row,int column)
import java.awt.Color;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class ChangeCellColorJTable extends javax.swing.JFrame {
public static Color[][] table_color = new Color[40][40];
//this array is used to change the table color
public static Object[][] table_value = new Object[40][40];
//this array is used to change the table values
public ChangeCellColorJTable() {
initComponents();
initTable(); //Initializing the table
refreshtable(mytable,1,1);
}
public static void initTable(){
int i,j;
Object[][] data = new Object[][]{
{10,10},
{11,11},
{12,12},
{13,13}
};
for(i = 0;i < 4;i++ ){
for(j = 0;j<2;j++){
mytable.setValueAt(data[i][j], i, j); //Setting values in Jtable
table_value[i][j] = data[i][j]; //Setting same values table array
}
}
for(i = 0;i<4;i++){
for(j = 0;j<2;j++){
table_color[i][j] = Color.getColor("#f9f4bd"); //Setting initial color of the table
//Not Mandatory
}
}
}
private void initComponents() { //These code is to initialize the Components to be Displayed
jScrollPane1 = new javax.swing.JScrollPane();
mytable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mytable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
mytable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mytableMouseClicked(evt);
}
});
jScrollPane1.setViewportView(mytable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(13, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}
//MouseClicked on Table Event
private void mytableMouseClicked(java.awt.event.MouseEvent evt) { //MouseClickEvent
int row,column;
Color colr = Color.CYAN;
JTable target = (JTable)evt.getSource();
row = target.getSelectedRow();
column = target.getSelectedColumn();
setColorAtCell(mytable,colr,row,column);
}
//referesh table function by call custom_renderer
public static void refreshtable(JTable table,int row,int column){
int i;
try{
mytable.getColumnModel().getColumn(column).setCellRenderer(new Custom_Renderer());
}catch(Exception e){
JOptionPane.showMessageDialog(null, "error there");
}
}
//this function could be used for changing the color at specific cell
//Provided we use a Table Color Array (table_color)
public static void setColorAtCell(JTable table,Color colr,int row,int column){
table_color[row][column] = colr;
refreshtable(table,row,column);
refreshtable(table,row,column);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChangeCellColorJTable().setVisible(true);
}
});
}
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTable mytable;
}
class Custom_Renderer extends DefaultTableCellRenderer
{
Custom_Renderer(){
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Object value_present = table.getValueAt(row, column);
Object value_in_table = ChangeCellColorJTable.table_value[row][column];
if(value_present.equals(value_in_table)){ //This condition I used so that the Specific cell come into the condition
Color color = ChangeCellColorJTable.table_color[row][column];
cellComponent.setBackground(color);
}else{
cellComponent.setBackground(Color.blue);
}
return cellComponent;
}
}