This week's book giveaway is in the
General Computing
forum.
We're giving away four copies of
Arduino in Action
and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
i cant set the string name of my table columns
jhon max
Ranch Hand
Joined: Jan 06, 2010
Posts: 45
posted
Jan 27, 2010 00:54:14
0
please I need some help how to make the column names appear on my table....
thanks!
public class InventoryMainDatabase extends JFrame { private JPanel inventoryPanel; private JTable inventoryTable; private ImageIcon background; public InventoryMainDatabase() { inventoryPanel = new JPanel(); background = new ImageIcon("c:/pics/popupbck4.jpg"); } public void setComponents(Container cont) { inventoryPanel.setLayout(new GridLayout(1, 0)); String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; Object[][] data = {{"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} }; inventoryTable = new JTable(data, columnNames) { public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer( renderer, row, column); // We want renderer component to be //transparent so background image is visible if( c instanceof JComponent ) { ((JComponent)c).setOpaque(false); } return c; } public void paint( Graphics g ) { // tile the background image Dimension d = getSize(); for( int x = 0; x < d.width; x += background.getIconWidth()) { for( int y = 0; y < d.height; y += background.getIconHeight()) { g.drawImage( background.getImage(), x, y, null, null ); } // Now let the paint do its usual work super.paint(g); } } }; inventoryTable.setGridColor(Color.BLACK); inventoryTable.setOpaque(false); inventoryTable.setPreferredScrollableViewportSize(new Dimension(500, 70)); inventoryTable.setFillsViewportHeight(true); inventoryPanel.add(inventoryTable); cont.add(inventoryPanel); } public void createTable() { this.setComponents(this.getContentPane()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(1000, 400); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { InventoryMainDatabase iD = new InventoryMainDatabase(); public void run() { iD.createTable(); } }); } }
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Jan 27, 2010 03:30:30
0
//inventoryPanel.add(inventoryTable);
inventoryPanel.add(new
JScrollPane
(inventoryTable));
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
4
posted
Jan 27, 2010 05:12:01
0
Moving to our GUIs forum.
I agree. Here's the link:
http://aspose.com/file-tools
subject: i cant set the string name of my table columns
Similar Threads
jtable: column header click
one click and the whole ROW will be selected
Jtable background image
Border around cells in JTable
JTable cell rendering
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter