Results 1 to 4 of 4
  1. #1
    viral is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Background under column header

    I have small problem. I would like to change color under column header when I am draging column to change its order.

    Try this simple code and drag columns. You will notice GRAY rectangle under header. How can I change it ?

    Java Code:
    import java.awt.Color;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.JTableHeader;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    
    public class MainWindow extends JFrame {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JPanel contentPane;
    	private JTable table;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MainWindow frame = new MainWindow();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public MainWindow() {
    		setBackground(Color.GREEN);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.CYAN);
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		
    		JScrollPane scrollPane = new JScrollPane();
    		GroupLayout gl_contentPane = new GroupLayout(contentPane);
    		gl_contentPane.setHorizontalGroup(
    			gl_contentPane.createParallelGroup(Alignment.LEADING)
    				.addGroup(gl_contentPane.createSequentialGroup()
    					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
    					.addGap(5))
    		);
    		gl_contentPane.setVerticalGroup(
    			gl_contentPane.createParallelGroup(Alignment.LEADING)
    				.addGroup(gl_contentPane.createSequentialGroup()
    					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
    					.addGap(3))
    		);
    		
    		String[] columnNames = {"Column 1",
                    "Column 2",
                    "Column 3"};
    		
    		Object [] [] data = new Object [15][3];
    		for (int i = 0; i < data.length; i++){
    			data [i][0] = 322 + (i * 3) + (1213*i)/3;
    			data [i][1] = "Some string";
    			data [i][2] = "Another string in column";					
    		}
    		table = new JTable(data , columnNames);		
    		table.setForeground(Color.YELLOW);
    		table.setFillsViewportHeight(true);
    		table.setBackground(Color.MAGENTA);
    		scrollPane.setViewportView(table);
    				
    		scrollPane.getViewport().setBackground(Color.RED);
    		
    		contentPane.setLayout(gl_contentPane);
    		JTableHeader anHeader = table.getTableHeader();
    	    anHeader.setForeground(Color.WHITE);
    	    anHeader.setBackground(Color.BLUE);
    	    
    	    // This gives nullpointer
    	    //scrollPane.getColumnHeader().setBackground(Color.RED);
    		
    	    
    	    
    	}
    }
    Or check this screenshot :

    Background under column header-columnheader2.jpg
    Thanks in advance.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Java Code:
    // This gives nullpointer
    //scrollPane.getColumnHeader().setBackground(Color.RED);
    Put that code after the JScrollPane has been realized by being added to a top level window that is subsequently pack()ed or setVisible(true)
    Java Code:
    ....add(scrollPane);
    :
    :
    frame.pack();
    scrollPane.getColumnHeader().setBackground(Color.RED);
    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Alternatively, you can explicitly set the scroll pane's column header view.
    Java Code:
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setColumnHeaderView(table.getTableHeader());
    scrollPane.getColumnHeader().setBackground(Color.red);
    db

  4. #4
    viral is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    I do not understand first example - I am not experienced enough but second works.
    Thanks DarrylBurke !

    Problem solved.

Similar Threads

  1. Replies: 11
    Last Post: 10-21-2014, 05:46 AM
  2. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 07:10 PM
  3. Replies: 4
    Last Post: 02-05-2011, 07:47 PM
  4. Replies: 1
    Last Post: 07-28-2010, 04:46 PM
  5. Replies: 1
    Last Post: 07-06-2010, 05:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •