JLabel в JTable - Java
Формулировка задачи:
Мне нужно сделать вывод JLabel в ячейках JTable, но не смотря на рендерер и другое, всё равно выводится только название класса: javax.swing.JLabel. Вот код, буду благодарен за помощь.
package schedule; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; class tablepanel extends JPanel{ private table[][][] ttable; JTable Table; int i; class Renderer extends DefaultTableCellRenderer{ @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { return (JLabel)value; } } class Model extends DefaultTableModel{ @Override public Class getColumnClass(int columnIndex) { if(columnIndex == 0) return getValueAt(0, columnIndex).getClass(); else return super.getColumnClass(columnIndex); } } public tablepanel(table[][][] t,int stgrp,inputdata input) { DecimalFormatSymbols s = new DecimalFormatSymbols(); s.setDecimalSeparator('.'); DecimalFormat f = new DecimalFormat("#,##0.00", s); double[] time={8.20,10.00,12.05,13.50,15.35,17.30,18.00}; String[] day={"Mon","Tue","Wed","Thurs","Fri"}; ttable=t; int stno; Table = new JTable(); Model model = new Model(); Table.setModel(model); Table.setDefaultRenderer(JLabel.class, new Renderer()); Table.setPreferredScrollableViewportSize(new Dimension(600, 600)); Table.setRowHeight(30); Table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //JLabel label=new JLabel(input.stgrp[stno].code); //add(label,BorderLayout.NORTH); model.addColumn(" "); for(i=1;i<=5;i++) { model.addColumn(day[i-1]); } //JScrollBar s=new JScrollBar(HORIZONTAL); for(i=0;i<6;i++) { TableColumn column=Table.getColumnModel().getColumn(i); column.setMinWidth(100); column.setPreferredWidth(100); } for(stno=0;stno<stgrp;stno++) { for(i=1;i<=7;i++) { double x=time[i-1]+1.5; model.addRow(new Object[]{new JLabel(f.format(time[i-1])+"-"+f.format(x)), new JLabel(ttable[stno][i-1][0].course+" "+ttable[stno][i-1][0].ins+" "+ttable[stno][i-1][0].room), new JLabel(ttable[stno][i-1][1].course+" "+ttable[stno][i-1][1].ins+" "+ttable[stno][i-1][1].room), new JLabel(ttable[stno][i-1][2].course+" "+ttable[stno][i-1][2].ins+" "+ttable[stno][i-1][2].room), new JLabel(ttable[stno][i-1][3].course+" "+ttable[stno][i-1][3].ins+" "+ttable[stno][i-1][3].room), new JLabel(ttable[stno][i-1][4].course+" "+ttable[stno][i-1][4].ins+" "+ttable[stno][i-1][4].room)}); } } JScrollPane scroll = new JScrollPane(Table); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //scroll.getViewport().setExtentSize(new Dimension(100,100)); //scroll.setHorizontalScrollBar(s); add(scroll,BorderLayout.CENTER); } }
Решение задачи: «JLabel в JTable»
textual
Листинг программы
package schedule; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; class tablepanel extends JPanel{ private table[][][] ttable; private JTable Table; private inputdata input; private int stgrp; class Renderer extends DefaultTableCellRenderer{ @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(value instanceof JLabel){ //This time return only the JLabel without icon return (JLabel)value; }else{ return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } } class Model extends DefaultTableModel{ @Override public Class getColumnClass(int columnIndex) { /* if(columnIndex == 0) return getValueAt(0, columnIndex).getClass(); else return super.getColumnClass(columnIndex);*/ return JLabel.class; } } public tablepanel(table[][][] t,int stgrp,inputdata input) { this.ttable=t; this.input = input; this.stgrp = stgrp; DecimalFormatSymbols s = new DecimalFormatSymbols(); s.setDecimalSeparator('.'); DecimalFormat f = new DecimalFormat("#,##0.00", s); double[] time1={8.20,10.00,12.05,13.50,15.35,17.20,19.05}; double[] time2={9.50,11.35,13.40,15.25,17.10,18.55,20.40}; String[] day={"Mon","Tue","Wed","Thurs","Fri"}; int stno; int i; Table = new JTable(); Model model = new Model(); Table.setModel(model); Table.setDefaultRenderer(JLabel.class, new Renderer()); Table.setPreferredScrollableViewportSize(new Dimension(600, 600)); Table.setRowHeight(60); Table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); model.addColumn(" "); for(i=1;i<=5;i++) { model.addColumn(day[i-1]); } for(i=0;i<6;i++) { TableColumn column=Table.getColumnModel().getColumn(i); column.setMinWidth(100); column.setPreferredWidth(100); } for(stno=0;stno<stgrp;stno++) { model.addRow(new Object[]{new JLabel(input.stgrp[stno].code, SwingConstants.CENTER),"","","","",""}); for(i = 0;i <= 6;i++) { JLabel[] labels = new JLabel[6]; labels[0] = new JLabel(f.format(time1[i])+"-"+f.format(time2[i]), SwingConstants.CENTER); for (int j = 0; j <= 4; j++){ labels[j+1] = new JLabel("<html>"+ttable[stno][i][j].course+"<br>"+ttable[stno][i][j].ins+"<br>"+ttable[stno][i][j].room+"</html>", SwingConstants.CENTER); } /* model.addRow(new Object[]{new JLabel(f.format(time[i-1])+"-"+f.format(x)), new JLabel("<html>"+ttable[stno][i-1][0].course+"<br>"+ttable[stno][i-1][0].ins+" "+ttable[stno][i-1][0].room+"</html>", SwingConstants.CENTER), new JLabel(ttable[stno][i-1][1].course+" "+ttable[stno][i-1][1].ins+" "+ttable[stno][i-1][1].room, SwingConstants.CENTER), new JLabel(ttable[stno][i-1][2].course+" "+ttable[stno][i-1][2].ins+" "+ttable[stno][i-1][2].room, SwingConstants.CENTER), new JLabel(ttable[stno][i-1][3].course+" "+ttable[stno][i-1][3].ins+" "+ttable[stno][i-1][3].room, SwingConstants.CENTER), new JLabel(ttable[stno][i-1][4].course+" "+ttable[stno][i-1][4].ins+" "+ttable[stno][i-1][4].room, SwingConstants.CENTER)});*/ model.addRow(labels); } model.addRow(new Object[]{"","","","","",""}); model.addRow(new Object[]{"","","","","",""}); } JScrollPane scroll = new JScrollPane(Table); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); add(scroll,BorderLayout.CENTER); } public void createAndShowGUI(){ JFrame tableframe = new JFrame(); tablepanel panel = new tablepanel(ttable,stgrp,input); EventQueue.invokeLater(new Runnable() { @Override public void run() { tableframe.setTitle("CLASS SCHEDULING"); tableframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setOpaque(true); tableframe.setContentPane(panel); tableframe.pack(); tableframe.setVisible(true); } }); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д