Hallo zusammen,
ich habe ein Problem und zwar möchte ich in einer Tabelle einzelne Zellen einfärben. Allerdings funktioniert es nicht so wie ich es mir vorgestellt habe. Ich schaffe es, dass ich in die betreffenden Bereiche Text schreiben kann, aber nicht diese einzufärben. Ich hoffe, dass ihr mir helfen könnt.
Hier mein Code:
LG Biene
ich habe ein Problem und zwar möchte ich in einer Tabelle einzelne Zellen einfärben. Allerdings funktioniert es nicht so wie ich es mir vorgestellt habe. Ich schaffe es, dass ich in die betreffenden Bereiche Text schreiben kann, aber nicht diese einzufärben. Ich hoffe, dass ihr mir helfen könnt.
Hier mein Code:
Code:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import javafx.scene.paint.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
public class TestTabelle extends JFrame {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestTabelle frame = new TestTabelle();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestTabelle() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 969, 274);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
table = new JTable();
table.setEnabled(false);
table.setModel(new DefaultTableModel(
new Object[][] {
{"Januar", null, null, null, null, "", "", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"Februar", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"M\u00E4rz", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"April", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"Mai", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"Juni", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"Juli", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"August", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"September", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"Okotober", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"November", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
{"Dezember", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
},
new String[] {
"Monat", "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"
}
));
table.getColumnModel().getColumn(0).setPreferredWidth(130);
table.getColumnModel().getColumn(0).setMinWidth(100);
table.getColumnModel().getColumn(31).setMinWidth(10);
//TEST
int spalte;
int i;
int beginn;
int ende;
beginn = 1;
ende = 21;
for(i = beginn; i < ende; i++){
spalte = i;
if(spalte == i){
table.setValueAt("X" , 0, spalte);
//table.setValueAt(Color.BLUE , 0, spalte); // funktioniert leider nicht
}
}
beginn = 5;
ende = 11;
for(i = beginn; i < ende; i++){
spalte = i;
if(spalte == i){
table.setValueAt("X" , 1, spalte);
//table.setValueAt(Color.BLUE , 1, spalte);
}
}
beginn = 10;
ende = 31;
for(i = beginn; i < ende; i++){
spalte = i;
if(spalte == i){
table.setValueAt("X" , 2, spalte);
//table.setValueAt(Color.BLUE , 2, spalte);
}
}
scrollPane.setViewportView(table);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(5)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 928, Short.MAX_VALUE)
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(6)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
.addContainerGap(21, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
}
LG Biene