logo
down
shadow

How to set the background color of target cells in a jtable?


How to set the background color of target cells in a jtable?

By : user3850992
Date : October 18 2020, 11:12 AM
Hope that helps you need to use a custom renderer
code :


Share : facebook icon twitter icon
How to color cells from JTable?

How to color cells from JTable?


By : ზურაბ გულმაგარაშვილი
Date : March 29 2020, 07:55 AM
it fixes the issue You can attach a cellrenderer to your JTable and adjust the color with it: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer
Set different background color for all cells in a JTable Column, based on the String returned by the database Query

Set different background color for all cells in a JTable Column, based on the String returned by the database Query


By : PivotPoint
Date : March 29 2020, 07:55 AM
it fixes the issue I'm working on translating a project from gambas code language to Java. , You can implement something like this
code :
public void changeTable(JTable table,int column_index) { 
table.getColumnModel().getColumn(column_index).setCellRenderer(new DefaultTableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                final Component c = super.getTableCellRendererComponent(table, value,
                        isSelected, hasFocus, row, column);

                if (!isSelected) {
                    int age = Integer.parseInt(table.getValueAt(row, 1).toString());
     // you can set your own implementation here. As an example I 
     // have checked if the age is over 20 or not. According to the 
     // age return from the table column you get different background colors. 
                    int req_age = 20;
     // in your case req_age can be rs.getString("column_name"); or something
     // you need to check with..
                    if (age < req_age) {
                        c.setBackground(new Color(255, 101, 18));
                    } else {
                        c.setBackground(new Color(0, 204, 0));
                    }

                }
                return c;
            }
        });
    }
Color only specific cells in JTable

Color only specific cells in JTable


By : Dimitar Ruskov
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Make it so that when your cell should not be yellow, that you set the background to white (or the table's background color).
A renderer that extends DefaultTableCellRenderer uses the same component (a JLabel) as a template for all cells (cf DefaultTableCellRenderer implementation notes - they call it rubber-stamping). Once you set its background to yellow, it will remain yellow for rendering consecutive cells until you change its background color again.
code :
boolean isYellow = false;
for (int i = 0; i < 3; i++) {
            if ((row == coordinatesYellow[i][0]) && (column == coordinatesYellow[i][1])) {
                c.setBackground(Color.yellow);
                isYellow = true;
            }
        }
if( !isYellow )
  c.setBackground(Color.white);
change cells color in jTable when the value of cells is true java

change cells color in jTable when the value of cells is true java


By : Charles Mendes
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further when i insert data into jtable i test if the value of cells is true, how i can change the background color of this cell , Try to add component to focus it like :
code :
    String[] header = { "ID", "Name", "Department" };

    String[][] datas = { { "0001", "Onur", "Java Developer" }, { "0002", "Umut", "Musical Ens" },
            { "0003", "Eymen", "Just Reader" }, { "0004", "Mustafa", "Worker" } };

    boolean isSelected = false;
    boolean hasFocus = false;

    JTable table = new JTable(datas, header);
    int rows = table.getRowCount();
    int cols = table.getColumnCount();
    Object color = (Color) Color.CYAN;
    table.setOpaque(false);
    table.setFillsViewportHeight(true);
    table.setCellSelectionEnabled(true);
    table.setBounds(30, 40, 200, 300);

    if (table.isCellSelected(rows, cols)) {
        isSelected = true;
    }
    Component comp = getTableCellRendererComponent(table, color, isSelected, hasFocus, rows, cols);
    if (rows == 1 && cols == 1)
        comp.setBackground(new java.awt.Color(0, 0, 255));
    JScrollPane pane = new JScrollPane(table);
    getContentPane().add(pane);

}

@Override
public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Color newColor = (Color) color;
    table.setBackground(newColor);

    if (isSelected) {

        table.setBackground(Color.CYAN);
    } else {

        table.getSelectionBackground();
    }
    // Discussed in the following section
    return null;
}
iOS UITableView: changing background color of table overrides background color of cells

iOS UITableView: changing background color of table overrides background color of cells


By : Shinanghun Febrie
Date : March 29 2020, 07:55 AM
Does that help
setting the background color of my tableView overrides the background colors I've set for my individual cells
Related Posts Related Posts :
  • My "Enter the secret code" method is printing out the wrong output
  • Package maven app into a single jar - with an exception
  • Return List from forEach using Stream in java
  • compareTo comparison method violates its general contract
  • Slow message consumption using AmazonSQSClient
  • NullPointerException problem for paint and paintComponent
  • Using Callable instead of Supplier or vice versa
  • how to get the title of the listview in androidstudio
  • CRD creation with java-client for Kubernetes
  • How do I draw shape on the JPanel, also linking the shape with an event control?
  • Execute and implement java file from command prompt
  • Accessing JAX-WS Published Endpoint is Not Working
  • Springboot - injection from application.yml depending method name
  • How to connect words from two lists in Java
  • Cannot resolve symbol QueryParser Lucene 7.5
  • null String when retrieving data from Firebase
  • Convert Item to Map<String, AttributeValue> for DynamoDB in Java
  • grouping on list of objects using lambda in java
  • Spring HATEOAS does not respect default inclusion property for resource link
  • How to use gradle 'api' dependency
  • How to change my statement to print frequencies of object member type in hashmap?
  • How Chrome added text to tab switcher in a drawable?
  • Calendar give wrong day
  • DataBinding not Updating TextView when notifyPropertyChanged(BR.xx) is used
  • How to give parameters to hashmap getMethod
  • JAXB Binding Exception
  • How to fix java.net.SocketException: Broken pipe?
  • Manual object serialization in Java
  • JAXB: Unmarshalling does not always populate certain classes?
  • How do you save your Activity's state when exiting? Android
  • reload java applet
  • How can tokenize this string in java?
  • GWT DatePicker Locale
  • How would you design a twitter like message system, how to design the messaging system?
  • Is there a robust java.util.logging handler implementation of syslog?
  • Aligning messageformat on printing a JTable
  • SXXP0003: Error reported by XML parser: Content is not allowed in prolog
  • ehcache LRU evicting recently used entries
  • java cosine similarity problem
  • Efficient way to implement a client multiple file upload service
  • Regarding Object Comparison
  • How to embed IE in Swing application?
  • Using javax.tools.ToolProvider from a custom classloader?
  • What is the difference between i++ & ++i in a for loop?
  • Match a java main class with regexp
  • Confluence Persistence
  • Is there a way for a SecurityManager in java to selectively grant ReflectPermission("suppressAccessChecks")?
  • Using the camera activity in Android
  • Is communication in java rmi secure?
  • Java - how to make "runnable" JTable?
  • How can I write a Java program to draw a box, an oval, and an arrow?
  • What is the difference between ServletRequest's getRemoteHost() and getServerName()
  • java sparse matrix problem
  • ClassCastException when using HQL
  • how to generate a csv file through java code
  • Netbeans + tomcat on Ubuntu
  • How to connect to a remote MySQL database with Java?
  • System Event Handling using Java
  • How to change Ant script with EMMA code-coverage so it can find runtime coverage data?
  • Java bytecode iconst_0 iadd sequence
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org