Einfärben von Tabellenzellen

Biene2015

Mitglied
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:

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
 

Enceladus271

Bekanntes Mitglied
Für das Aussehen der Zellen werden TableCellRenderer verwendet. Beispiel:
Java:
    table.setDefaultRenderer( Object.class, new DefaultTableCellRenderer() {

       @Override
       public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
         final Component component = super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
         if ( "X".equals( value ) ) {
           component.setBackground( Color.BLUE );
         } else {
           component.setBackground( Color.WHITE );
         }
         return component;
       };
     } );
Hier gibts mehr dazu: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
T TableView Zeilen einfärben AWT, Swing, JavaFX & SWT 13
missy72 JavaFX TableRow / RowFactory Zeile einfärben AWT, Swing, JavaFX & SWT 9
B Tabellenzeilen einfärben AWT, Swing, JavaFX & SWT 4
kilopack15 JPanel im laufenden Zustand einfärben AWT, Swing, JavaFX & SWT 2
S Swing GANZE Row auf einen Schlag einfärben AWT, Swing, JavaFX & SWT 2
S Swing JTable - Einzelne Rows einfärben AWT, Swing, JavaFX & SWT 11
LexeB4F Zelle in JTable gezielt einfärben AWT, Swing, JavaFX & SWT 4
J Zellen in JavaFx einfärben funktioniert nicht AWT, Swing, JavaFX & SWT 1
D Swing Bestimmte Berechnung von Spalten durchführen und dann die Zeilen nach bestimmten Kriterien einfärben AWT, Swing, JavaFX & SWT 5
P DefaultStyledDocument eingegebenen String durchsuchen und bestimmte Sachen einfärben AWT, Swing, JavaFX & SWT 1
T Table-Zeilen mit Thread einfärben AWT, Swing, JavaFX & SWT 15
M JTable Zeilen und Zellen einfärben AWT, Swing, JavaFX & SWT 11
C Java2D Transparenz geht bei einfärben verloren AWT, Swing, JavaFX & SWT 7
M JTable Zeilen einfärben AWT, Swing, JavaFX & SWT 13
R Zeile in JTable anhand von Wert einfärben AWT, Swing, JavaFX & SWT 2
S Swing JTable - Zeilentext unabhänig von einander einfärben AWT, Swing, JavaFX & SWT 4
S Swing HTML-Text in JEditorPane einfärben AWT, Swing, JavaFX & SWT 5
U JTable: Zeilen abhängig vom Datensatz einfärben AWT, Swing, JavaFX & SWT 4
G JFreechart Balken einfärben AWT, Swing, JavaFX & SWT 6
P JTable zeile einfärben AWT, Swing, JavaFX & SWT 1
O Zeilen in JTable auf Grund eines Wertes einfärben AWT, Swing, JavaFX & SWT 6
A JXTable jede zweite Zeile einfärben AWT, Swing, JavaFX & SWT 4
M [Swing] Kalender mit JTable / Einfärben von Zellen AWT, Swing, JavaFX & SWT 10
B JTable-Zellen einfärben AWT, Swing, JavaFX & SWT 6
S SWT Tabellenzellen SelectionListener AWT, Swing, JavaFX & SWT 10

Ähnliche Java Themen


Oben