java - How to make JLabel still appear after JTable column swap in the runtime? -


i have jtable:

enter image description here

after swap columns unfortunately flags not rendered anymore:

enter image description here

i assume fault of getcolumnclass method have fixed class every column, don't know how fix issue.

package zad1;  import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.image; import java.awt.image.bufferedimage; import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.util.arrays; import java.util.vector;  import javax.imageio.imageio; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtable; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecellrenderer; import javax.swing.table.tablemodel;  public class countrytable extends jtable {         public countrytable(string countriesfilename) {         vector<string> columnnames = new vector<string>();         vector<vector<object>> data = new vector<vector<object>>();         try {             bufferedreader br = new bufferedreader(new filereader(countriesfilename));             columnnames.addall(arrays.aslist(br.readline().split("\t")));             system.out.println(columnnames);             string line;             while ((line = br.readline()) != null) {                 string[] attributes = line.split("\t");                 vector<object> rowdata = new vector<object>();                 rowdata.add(attributes[0]);                 rowdata.add(attributes[1]);                 rowdata.add(long.valueof(attributes[2]));                 bufferedimage icon = imageio.read(new file("./data/" + attributes[3]));                 double ratio = (double) icon.getwidth() / icon.getheight();                 rowdata.add(new imageicon(icon.getscaledinstance(30, 20, image.scale_fast)));                 data.add(rowdata);             }             br.close();         } catch (exception e) {             e.printstacktrace();         }         setmodel(new mytablemodel(data, columnnames));         setrowheight(30);         getcolumnmodel().getcolumn(2).setcellrenderer(new populationcellrenderer());         (int = 0; < getcolumncount(); i++) {             getcolumnmodel().getcolumn(i).setwidth(400);         }     }      @override     public class getcolumnclass(int column) {         switch (column) {         case 0:             return string.class;// panstwo         case 1:             return string.class;// stolica         case 2:             return long.class;// ludnosc         case 3:             return icon.class; // flaga jakiej klasy jest dana komorka w danej kolumnie aby tabela ja poprawnie wyswietlila         default:             return string.class;         }     }       class mytablemodel extends defaulttablemodel {         public mytablemodel(vector<vector<object>> data, vector<string> columnnames) {             super(data, columnnames);//wywolanie konstruktora z nadklasy             system.out.println(columnnames);         }          @override         public boolean iscelleditable(int row, int column) {             return column == 3;         }     }      class populationcellrenderer extends jlabel implements tablecellrenderer {          @override         public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) {             long population = (long) value;             sethorizontalalignment(jlabel.right);             if (population > 20000000) {                 setforeground(color.red);             } else {                 setforeground(color.black);             }             settext(population + "");             return this;         }      } } 

there's no need extend jtable this. example overrides getcolumnclass() in view, when should in model. default renderer icon.class applied wherever column moved.

private final tablemodel model = new defaulttablemodel(data, columnnames) {      @override     public class<?> getcolumnclass(int column) {         switch (column) {              …         }     } }; 

this make possible apply populationcellrenderer on per-type basis:

table.setdefaultrenderer(long.class, new populationrenderer()); 

also consider extending defaulttablecellrenderer, shown here, using numberformat, shown below. based on example @mkorbel, self-contained code below shows corrected tablemodel , updated tablecellrenderer.

image

import java.awt.*; import java.text.numberformat; import javax.swing.*; import javax.swing.table.*;  public class tableexample {      private static final long cusp = 20000000;     private jframe frame = new jframe("table demo");     private icon erroricon = (icon) uimanager.geticon("optionpane.erroricon");     private icon infoicon = (icon) uimanager.geticon("optionpane.informationicon");     private icon warnicon = (icon) uimanager.geticon("optionpane.warningicon");     private string[] columnnames = {"string", "long", "float", "double", "boolean", "icon"};     private object[][] data = {         {"aaa", cusp - 1, 12.15f, 100.05, true, (erroricon)},         {"bbb", cusp, 7.154f, 6.1555, false, (infoicon)},         {"ccc", cusp + 1, 0.1135f, 3.1455, true, (warnicon)},         {"ddd", 42l, 31.15f, 10.05, true, (erroricon)},         {"eee", 12345l, 5.154f, 16.1555, false, (infoicon)},         {"fff", 54321l, 4.1135f, 31.1455, true, (warnicon)}};     private final tablemodel model = new defaulttablemodel(data, columnnames) {          @override         public class<?> getcolumnclass(int column) {             switch (column) {                 case 0:                     return string.class;                 case 1:                     return long.class;                 case 2:                     return float.class;                 case 3:                     return double.class;                 case 4:                     return boolean.class;                 case 5:                     return icon.class;                 default:                     return string.class;             }         }     };     private final jtable table = new jtable(model);      private static class populationrenderer extends defaulttablecellrenderer {          numberformat f = numberformat.getinstance();          @override         public component gettablecellrenderercomponent(jtable table, object value,             boolean isselected, boolean hasfocus, int row, int col) {             jlabel r = (jlabel) super.gettablecellrenderercomponent(                 table, value, isselected, hasfocus, row, col);             if (col == 1) {                 r.sethorizontalalignment(jlabel.right);                 long population = (long) value;                 if (population > cusp) {                     r.setforeground(color.red);                 } else {                     r.setforeground(color.black);                 }                 r.settext(f.format(population));             }             return r;         }     }      public tableexample() {         int h = infoicon.geticonheight();         table.setrowheight(h);         table.setpreferredscrollableviewportsize(             new dimension(table.getpreferredsize().width, 4 * h));         table.setautocreaterowsorter(true);         table.setdefaultrenderer(long.class, new populationrenderer());         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add(new jscrollpane(table));         frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     }      public static void main(string[] args) {         eventqueue.invokelater(() -> {             tableexample tableexample = new tableexample();         });     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -