2016-10-26 2 views
1

Wie ändert man die Farbe der einzelnen Zellen basierend auf dem Wert, den sie enthalten?Ändern der Hintergrundfarbe einer Zelle in einer JTable

Folgendes habe ich basierend auf dem, was ich gefunden habe.

import java.awt.EventQueue; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import java.awt.Dimension; 
import javax.swing.JPanel; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JMenuBar; 
import javax.swing.JMenu; 
import javax.swing.JMenuItem; 
import javax.swing.JButton; 
import javax.swing.JTable; 
import javax.swing.JLabel; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellRenderer; 


public class DriversSeason1 extends JLabel implements TableCellRenderer{ 

private JFrame frameSeason1Drivers; 
static JTable tableSeason1Drivers = new JTable(); 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       DriversSeason1 window = new DriversSeason1(); 
       window.frameSeason1Drivers.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    MyRenderer myRenderer = new MyRenderer(); 
    tableSeason1Drivers.setDefaultRenderer(Object.class, myRenderer); 
} 

/** 
* Create the application. 
*/ 
public DriversSeason1() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frameSeason1Drivers = new JFrame(); 
    frameSeason1Drivers.setBounds(100, 100, 780, 480); 
    frameSeason1Drivers.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panelSeason1DriversFull = new JPanel(); 
    panelSeason1DriversFull.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    frameSeason1Drivers.getContentPane().add(panelSeason1DriversFull, BorderLayout.CENTER); 
    panelSeason1DriversFull.setLayout(new BorderLayout(0, 0)); 

    JPanel panelSeason1Drivers = new JPanel(); 
    panelSeason1DriversFull.add(panelSeason1Drivers, BorderLayout.CENTER); 
    panelSeason1Drivers.setLayout(new BorderLayout(0, 0)); 

    tableSeason1Drivers = new JTable(); 
    tableSeason1Drivers.setBackground(Color.WHITE); 
    tableSeason1Drivers.setModel(new DefaultTableModel(
     new Object[][] { 
      {"Pos", null, null, null, null, null, null, null, null, null, null, "", null, null, null, null, null, null, null, null, null, null, null, null, null, null}, 
      {" 1", null, null, null, null, null, null, null, null, null, null, "", null, null, null, null, null, null, null, null, null, null, null, null, null, null}, 
      {" 2", 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}, 
      {" 3", 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}, 
      {" 4", 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}, 
      {" 5", 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}, 
      {" 6", 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}, 
      {" 7", 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}, 
      {" 8", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, "", null, null, null, null}, 
      {" 9", 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}, 
      {"10", 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}, 
      {"11", 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}, 
      {"12", 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}, 
      {"13", 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}, 
      {"14", 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}, 
      {"15", 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}, 
      {"16", 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}, 
      {"17", 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}, 
      {"18", 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}, 
      {"19", 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}, 
      {"20", 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}, 
      {"21", 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}, 
      {"22", 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}, 
      {"Pos", 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[] { 
      "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column" 
     } 
    )); 
    tableSeason1Drivers.getColumnModel().getColumn(0).setPreferredWidth(21); 
    tableSeason1Drivers.getColumnModel().getColumn(1).setPreferredWidth(91); 
    tableSeason1Drivers.getColumnModel().getColumn(2).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(3).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(4).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(5).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(6).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(7).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(8).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(9).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(10).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(11).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(12).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(13).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(14).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(15).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(16).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(17).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(18).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(19).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(20).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(21).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(22).setPreferredWidth(23); 
    tableSeason1Drivers.getColumnModel().getColumn(23).setPreferredWidth(35); 
    tableSeason1Drivers.getColumnModel().getColumn(24).setPreferredWidth(42); 
    tableSeason1Drivers.getColumnModel().getColumn(25).setPreferredWidth(34); 
    panelSeason1Drivers.add(tableSeason1Drivers, BorderLayout.CENTER); 

    JMenuBar menuBar = new JMenuBar(); 
    panelSeason1DriversFull.add(menuBar, BorderLayout.NORTH); 

    JMenu menuDrivers = new JMenu("Drivers Standings"); 
    menuBar.add(menuDrivers); 

    JMenuItem menuItemSeason1 = new JMenuItem("Season 1"); 
    menuDrivers.add(menuItemSeason1); 

    JMenuItem menuItemSeason2 = new JMenuItem("Season 2"); 
    menuDrivers.add(menuItemSeason2); 

    JMenuItem menuItemSeason3 = new JMenuItem("Season 3"); 
    menuDrivers.add(menuItemSeason3); 

    JMenuItem menuItemSeason4 = new JMenuItem("Season 4"); 
    menuDrivers.add(menuItemSeason4); 

    JMenuItem menuItemSeason5 = new JMenuItem("Season 5"); 
    menuDrivers.add(menuItemSeason5); 

    JMenuItem menuItemSeason6 = new JMenuItem("Season 6"); 
    menuDrivers.add(menuItemSeason6); 

    JMenuItem menuItemSeason7 = new JMenuItem("Season 7"); 
    menuDrivers.add(menuItemSeason7); 

    JMenuItem menuItemSeason8 = new JMenuItem("Season 8"); 
    menuDrivers.add(menuItemSeason8); 

    JMenuItem menuItemSeason9 = new JMenuItem("Season 9"); 
    menuDrivers.add(menuItemSeason9); 

    JMenuItem menuItemSeason10 = new JMenuItem("Season 10"); 
    menuDrivers.add(menuItemSeason10); 

    JMenu menuConstructors = new JMenu("Constructors Standings"); 
    menuBar.add(menuConstructors); 

    JMenuItem menuItemSeason1Con = new JMenuItem("Season1"); 
    menuConstructors.add(menuItemSeason1Con); 

    JMenuItem menuItemSeason2Con = new JMenuItem("Season 2"); 
    menuConstructors.add(menuItemSeason2Con); 

    JMenuItem menuItemSeason3Con = new JMenuItem("Season 3"); 
    menuConstructors.add(menuItemSeason3Con); 

    JMenuItem menuItemSeason4Con = new JMenuItem("Season 4"); 
    menuConstructors.add(menuItemSeason4Con); 

    JMenuItem menuItemSeason5Con = new JMenuItem("Season 5"); 
    menuConstructors.add(menuItemSeason5Con); 

    JMenuItem menuItemSeason6Con = new JMenuItem("Season 6"); 
    menuConstructors.add(menuItemSeason6Con); 

    JMenuItem menuItemSeason7Con = new JMenuItem("Season 7"); 
    menuConstructors.add(menuItemSeason7Con); 

    JMenuItem menuItemSeason8Con = new JMenuItem("Season 8"); 
    menuConstructors.add(menuItemSeason8Con); 

    JMenuItem menuItemSeason9Con = new JMenuItem("Season 9"); 
    menuConstructors.add(menuItemSeason9Con); 

    JMenuItem menuItemSeason10Con = new JMenuItem("Season 10"); 
    menuConstructors.add(menuItemSeason10Con); 

    JButton btnRacingRecord = new JButton("Racing Record"); 
    menuBar.add(btnRacingRecord); 

    JButton btnStats = new JButton("Stats"); 
    menuBar.add(btnStats); 

    JButton btnRaceInput = new JButton("Race Input"); 
    menuBar.add(btnRaceInput); 




} 

@Override 
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, 
     int row, int column) { 
    // TODO Auto-generated method stub 
    return null; 
} 
} 

Dann her die Renderer-Klasse, die die Hintergrundfarbe

import java.awt.Color; 
import java.awt.Component; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableCellRenderer; 

public class MyRenderer extends DefaultTableCellRenderer 
{ 
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
{ 
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

if (((int)table.getValueAt(row, column)) == 1) 
{ 
    System.out.println("hi"); 
} 
else 
{ 
    c.setBackground(table.getBackground()); 
} 
return c; 
} 

} 
+0

Die Rendering-Komponente ist standardmäßig ein 'JLabel'. Eine Beschriftung zeigt keine Hintergrundfarbe, es sei denn, sie ist als undurchsichtig definiert. –

+0

Für bessere Hilfe, früher, ein [MCVE] oder [Kurze, selbständige, korrekte Beispiel] (http://www.sscce.org/) . 1. Beispiel von craft to trim: 'tableSeason1Drivers.getColumnModel(). GetColumn (0) .setPreferredWidth (21);' Das hat nichts mit Hintergrundfarben zu tun. 2. Beispiel - es scheint, dass die Tabelle nur zwei Zeilen und zwei Spalten benötigt. –

Antwort

3
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       DriversSeason1 window = new DriversSeason1(); 
       window.frameSeason1Drivers.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    MyRenderer myRenderer = new MyRenderer(); 
    tableSeason1Drivers.setDefaultRenderer(Object.class, myRenderer); 
} 

Die wichtigste Methode auf einem separaten Thread läuft ändern sollte, so dass Sie versuchen, den Renderer auf den Tisch vor dem DriversSeason1 hinzufügen Klasse ist erstellt.

Wenn der Code im Event Dispatch Thread ausgeführt wird, wird die DriverSeason1-Klasse erstellt und Sie erstellen eine neue JTable, sodass alle benutzerdefinierten Renderer verloren gehen.

Die Lösung ist einfach. Fügen Sie den Renderer der Tabelle hinzu, wenn Sie die Tabelle erstellen.

Außerdem sollten Sie die DriversSeason1-Klasse JLabel nicht erweitern und TableCellRenderer implementieren. All diesen Code loswerden.

Schließlich, wenn Sie diese Probleme beheben, haben Sie immer noch einen Fehler im Renderer, der offensichtlich sein sollte. Sie laden die Daten als String-Objekte, so dass Sie nicht erwarten können, das Objekt und eine Ganzzahl zu erstellen. Sie müssen einen String-Vergleich in Ihrem Code durchführen, keinen Integer-Vergleich.

-1

Ihr Code hat viel schlechten Geruch, wenn Sie nicht wissen, wie man es besser macht, empfehle ich Ihnen, einige Bücher wie Refactoring zu lesen: Verbesserung des Designs von bestehendem Code und Effektivem Java.
Wie auch immer, ich habe es funktioniert, bitte überprüfen Sie, ob es das ist, was Sie wollen.

package self.examples; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.EventQueue; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JTable; 
import javax.swing.border.EmptyBorder; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellRenderer; 

public class DriversSeason1 extends JLabel implements TableCellRenderer { 

    private JFrame frameSeason1Drivers; 
    static JTable tableSeason1Drivers = new JTable(); 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        DriversSeason1 window = new DriversSeason1(); 
        window.frameSeason1Drivers.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 

    /** 
    * Create the application. 
    */ 
    public DriversSeason1() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frameSeason1Drivers = new JFrame(); 
     frameSeason1Drivers.setBounds(100, 100, 780, 480); 
     frameSeason1Drivers.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel panelSeason1DriversFull = new JPanel(); 
     panelSeason1DriversFull.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     frameSeason1Drivers.getContentPane().add(panelSeason1DriversFull, BorderLayout.CENTER); 
     panelSeason1DriversFull.setLayout(new BorderLayout(0, 0)); 

     JPanel panelSeason1Drivers = new JPanel(); 
     panelSeason1DriversFull.add(panelSeason1Drivers, BorderLayout.CENTER); 
     panelSeason1Drivers.setLayout(new BorderLayout(0, 0)); 

     tableSeason1Drivers = new JTable(); 
     tableSeason1Drivers.setBackground(Color.WHITE); 

     MyRenderer myRenderer = new MyRenderer(); 
     tableSeason1Drivers.setDefaultRenderer(Object.class, myRenderer); 

     tableSeason1Drivers.setModel(new DefaultTableModel(
       new Object[][] { 
         { "Pos", null, 1, null, null, null, null, null, null, null, null, "", null, null, null, null, 
           null, null, null, null, null, null, null, null, null, null }, 
         { " 1", null, null, null, null, null, null, null, null, null, null, "", null, null, null, null, 
           null, null, null, null, null, null, null, null, null, null }, 
         { " 2", 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 }, 
         { " 3", null, null, null, null, null, 1, null, null, null, null, null, null, null, null, null, 
           null, null, null, null, null, null, null, null, null, null }, 
         { " 4", null, null, 1, null, null, null, null, null, null, null, null, null, null, null, null, 
           null, null, null, null, null, null, null, null, null, null }, 
         { " 5", 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 }, 
         { " 6", 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 }, 
         { " 7", 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 }, 
         { " 8", null, null, null, null, null, null, null, null, null, null, null, null, null, null, 
           null, null, null, null, null, null, "", null, null, null, null }, 
         { " 9", 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 }, 
         { "10", 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 }, 
         { "11", 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 }, 
         { "12", 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 }, 
         { "13", 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 }, 
         { "14", 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 }, 
         { "15", 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 }, 
         { "16", 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 }, 
         { "17", 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 }, 
         { "18", 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 }, 
         { "19", 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 }, 
         { "20", 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 }, 
         { "21", 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 }, 
         { "22", 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 }, 
         { "Pos", 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[] { "New column", "New column", "New column", "New column", "New column", "New column", 
         "New column", "New column", "New column", "New column", "New column", "New column", 
         "New column", "New column", "New column", "New column", "New column", "New column", 
         "New column", "New column", "New column", "New column", "New column", "New column", 
         "New column", "New column" })); 
     tableSeason1Drivers.getColumnModel().getColumn(0).setPreferredWidth(21); 
     tableSeason1Drivers.getColumnModel().getColumn(1).setPreferredWidth(91); 
     tableSeason1Drivers.getColumnModel().getColumn(2).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(3).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(4).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(5).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(6).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(7).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(8).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(9).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(10).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(11).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(12).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(13).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(14).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(15).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(16).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(17).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(18).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(19).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(20).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(21).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(22).setPreferredWidth(23); 
     tableSeason1Drivers.getColumnModel().getColumn(23).setPreferredWidth(35); 
     tableSeason1Drivers.getColumnModel().getColumn(24).setPreferredWidth(42); 
     tableSeason1Drivers.getColumnModel().getColumn(25).setPreferredWidth(34); 
     panelSeason1Drivers.add(tableSeason1Drivers, BorderLayout.CENTER); 

     JMenuBar menuBar = new JMenuBar(); 
     panelSeason1DriversFull.add(menuBar, BorderLayout.NORTH); 

     JMenu menuDrivers = new JMenu("Drivers Standings"); 
     menuBar.add(menuDrivers); 

     JMenuItem menuItemSeason1 = new JMenuItem("Season 1"); 
     menuDrivers.add(menuItemSeason1); 

     JMenuItem menuItemSeason2 = new JMenuItem("Season 2"); 
     menuDrivers.add(menuItemSeason2); 

     JMenuItem menuItemSeason3 = new JMenuItem("Season 3"); 
     menuDrivers.add(menuItemSeason3); 

     JMenuItem menuItemSeason4 = new JMenuItem("Season 4"); 
     menuDrivers.add(menuItemSeason4); 

     JMenuItem menuItemSeason5 = new JMenuItem("Season 5"); 
     menuDrivers.add(menuItemSeason5); 

     JMenuItem menuItemSeason6 = new JMenuItem("Season 6"); 
     menuDrivers.add(menuItemSeason6); 

     JMenuItem menuItemSeason7 = new JMenuItem("Season 7"); 
     menuDrivers.add(menuItemSeason7); 

     JMenuItem menuItemSeason8 = new JMenuItem("Season 8"); 
     menuDrivers.add(menuItemSeason8); 

     JMenuItem menuItemSeason9 = new JMenuItem("Season 9"); 
     menuDrivers.add(menuItemSeason9); 

     JMenuItem menuItemSeason10 = new JMenuItem("Season 10"); 
     menuDrivers.add(menuItemSeason10); 

     JMenu menuConstructors = new JMenu("Constructors Standings"); 
     menuBar.add(menuConstructors); 

     JMenuItem menuItemSeason1Con = new JMenuItem("Season1"); 
     menuConstructors.add(menuItemSeason1Con); 

     JMenuItem menuItemSeason2Con = new JMenuItem("Season 2"); 
     menuConstructors.add(menuItemSeason2Con); 

     JMenuItem menuItemSeason3Con = new JMenuItem("Season 3"); 
     menuConstructors.add(menuItemSeason3Con); 

     JMenuItem menuItemSeason4Con = new JMenuItem("Season 4"); 
     menuConstructors.add(menuItemSeason4Con); 

     JMenuItem menuItemSeason5Con = new JMenuItem("Season 5"); 
     menuConstructors.add(menuItemSeason5Con); 

     JMenuItem menuItemSeason6Con = new JMenuItem("Season 6"); 
     menuConstructors.add(menuItemSeason6Con); 

     JMenuItem menuItemSeason7Con = new JMenuItem("Season 7"); 
     menuConstructors.add(menuItemSeason7Con); 

     JMenuItem menuItemSeason8Con = new JMenuItem("Season 8"); 
     menuConstructors.add(menuItemSeason8Con); 

     JMenuItem menuItemSeason9Con = new JMenuItem("Season 9"); 
     menuConstructors.add(menuItemSeason9Con); 

     JMenuItem menuItemSeason10Con = new JMenuItem("Season 10"); 
     menuConstructors.add(menuItemSeason10Con); 

     JButton btnRacingRecord = new JButton("Racing Record"); 
     menuBar.add(btnRacingRecord); 

     JButton btnStats = new JButton("Stats"); 
     menuBar.add(btnStats); 

     JButton btnRaceInput = new JButton("Race Input"); 
     menuBar.add(btnRaceInput); 

    } 

    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, 
      int row, int column) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    class MyRenderer extends DefaultTableCellRenderer { 
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, 
       int row, int column) { 
      Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 

      System.out.println(table.getValueAt(row, column)); 

      if ("1".equals(String.valueOf(table.getValueAt(row, column)))) { 
       System.out.println("hi"); 
       c.setBackground(Color.YELLOW); 
      } else { 
       c.setBackground(Color.RED); 
      } 
      return c; 
     } 

    } 
} 
+1

(1-), eine Antwort ist nicht zum Posten von Code, damit es funktioniert. Es geht darum zu erklären, was falsch ist, damit das OP es beheben kann. Bei 200 Codezeilen ist nicht ersichtlich, was geändert wurde und warum. – camickr

+1

Zustimmen! Lektion gelernt. Vielen Dank. – Jason

Verwandte Themen