Results 1 to 4 of 4
Thread: Background under column header
- 08-22-2011, 01:39 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
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 ?
Or check this screenshot :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); } }

Thanks in advance.
- 08-22-2011, 06:43 PM #2
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:// This gives nullpointer //scrollPane.getColumnHeader().setBackground(Color.RED);
dbJava Code:....add(scrollPane); : : frame.pack(); scrollPane.getColumnHeader().setBackground(Color.RED);
- 08-22-2011, 06:47 PM #3
Alternatively, you can explicitly set the scroll pane's column header view.
dbJava Code:JScrollPane scrollPane = new JScrollPane(table); scrollPane.setColumnHeaderView(table.getTableHeader()); scrollPane.getColumnHeader().setBackground(Color.red);
- 08-23-2011, 08:47 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 06:10 PM -
How to write column by column to text file
By trkece in forum JDBCReplies: 9Last Post: 02-15-2011, 01:13 AM -
[SOLVED] How do you align/justify idividual table column header text?
By Angie in forum New To JavaReplies: 4Last Post: 02-05-2011, 06:47 PM -
JTable column header sorting direction arrows and row selection on focus loss
By r00tb33r in forum AWT / SwingReplies: 1Last Post: 07-28-2010, 03:46 PM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks