
تغییر رنگ سلول های جی تیبل جاوا به سادگی هر چه تمام تر
تغییر رنگ سلول:
برای تغییر رنگ سلول های jtable توی جاوا راه های مختلفی هست ولی ساده ترین روشی که من میدونم اینه اول از همه این 2 تا رو فراخوانی می کنید
|
1 2 |
import java.awt.Component; import javax.swing.table.TableCellRenderer; |
بعد روی تیبل راست کلیک میکنید و گزینه customize code رو انخاب میکنید

توی خط اول گزینه زیر رو میبینید ( فرض بر اینه نام jtable رو تغییر ندادیدن اگه عوضش کرده باشین اسمش میشه اسمی که شما براش انتخاب کردین )
|
1 |
jTable1 = new javax.swing.JTable(); |
کنارش یه کومبو باکس میبینید که مقدار پیش فرض default code هستش اون رو به custom creation تغییر بدین.
قبل از سیم کالن ; آخر خط اول چند تا اینتر بزنید

کدهای زیر رو اونجا بنویسید:( کد های زیر اگه روی سطری کلیک کنید سبز میشه و سلولی خالی باشه یا کلمه null توش نوشته شده باشه رنگ اون سلول های خاص رو عوض میکنه شما نسبت به شرایطی که دارید میتونید شرط ها رو تغییر بدین )و اگر مقداری در یک سلول متن خاصی بود کل رنگ آن ردیف سبز شود ( در اینجا مقدار ستون دوم چک شده )
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
{ public Component prepareRenderer ( TableCellRenderer renderer, int row, int column ){ // در اینجا میگوییم مقدار ستون دوم را در متغیر بریز String dat = (String)jtable.getModel().getValueAt(row, 1); Component component = super.prepareRenderer(renderer,row,column); // در اینجا میگوییم اگه مقدار ستون دوم برابر با مقدار تعیین شده بود رنگ آن ردیف سبز شود if ("programsfuture.com".equals(dat)){ component.setBackground(Color.GREEN); } //System.out.println("getModel= " + getModel().getValueAt(row,column)); else if (getModel().getValueAt(row,column) == null){ component.setBackground(Color.ORANGE); } else if (getModel().getValueAt(row,column) != null){ Object value = getModel().getValueAt(row,column).toString(); if (isRowSelected(row)) { component.setBackground(new Color(56,105,138)); component.setForeground(Color.WHITE); if(value.equals("false")){ component.setForeground(Color.red); } } else if(value.equals("false")){ component.setBackground(Color.ORANGE); component.setForeground(Color.BLACK); } else if(value.equals("")){ component.setBackground(Color.ORANGE); component.setForeground(Color.BLACK); } else{ component.setBackground(Color.WHITE); component.setForeground(Color.BLACK); } } return component; } } |
نهایتا کدتان مثل این باید باشد:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
jTable1 = new javax.swing.JTable() { public Component prepareRenderer ( TableCellRenderer renderer, int row, int column ){ // در اینجا میگوییم مقدار ستون دوم را در متغیر بریز String dat = (String)jtable.getModel().getValueAt(row, 1); Component component = super.prepareRenderer(renderer,row,column); // در اینجا میگوییم اگه مقدار ستون دوم برابر با مقدار تعیین شده بود رنگ آن ردیف سبز شود if ("programsfuture.com".equals(dat)){ component.setBackground(Color.GREEN); } //System.out.println("getModel= " + getModel().getValueAt(row,column)); elseif (getModel().getValueAt(row,column) == null){ component.setBackground(Color.ORANGE); } else if (getModel().getValueAt(row,column) != null){ Object value = getModel().getValueAt(row,column).toString(); if (isRowSelected(row)) { component.setBackground(new Color(56,105,138)); component.setForeground(Color.WHITE); if(value.equals("false")){ component.setForeground(Color.red); } } else if(value.equals("false")){ component.setBackground(Color.ORANGE); component.setForeground(Color.BLACK); } else if(value.equals("")){ component.setBackground(Color.ORANGE); component.setForeground(Color.BLACK); } else{ component.setBackground(Color.WHITE); component.setForeground(Color.BLACK); } } return component; } } ; |
در ضمن اگر خواستید یک در میان سطر هایتان رنگی باشه قبل از انتخاب کردن یک سطر کافی است به جای
|
1 2 3 4 |
else { component.setBackground(Color.WHITE); component.setForeground(Color.BLACK); } |
|
1 2 3 4 5 6 7 8 9 10 |
else{ if (row%2 == 0){ component.setBackground(new Color(255, 255, 253)); component.setForeground(Color.BLACK); } else { component.setBackground(new Color(208,205,205)); component.setForeground(Color.BLACK); } } |
|
1 2 3 4 |
String id = (String)jTable1.getModel().getValueAt(row, 0); if ("1".equals(id)){ component.setBackground(Color.GREEN); } |
و در نهایت اگر خواستین کل یک ستون رو رنگش رو تغییر بدین کافیه از دستور زیر بعد از اولین if استفاده کنید بجای عدد صفر شماره ستون رو مینویسید صفر یعنی ستون اول
|
1 2 3 4 |
else if(column == 0){ component.setBackground(new Color(100,100,100)); component.setForeground(Color.WHITE); } |





