Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
JTable -Adding Column and Fix Columns Problems
Anand Karia
Ranch Hand
Posts: 158
I like...
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How do i add a Boolean Column in my existing table by clicking Edit Button.
How can i (fix/not editable) my column # 1 and column # 5. The rest must be editable.
ANAND.
Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
guy madura
Greenhorn
Posts: 6
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi ,
Your question is not clear.
Craig Wood
Ranch Hand
Posts: 1535
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; import javax.swing.table.*; public class AddingAColumn { int rows, cols; public AddingAColumn() { cols = 4; rows = 8; final TableSupport support = new TableSupport(); String[] headers = support.getHeaders(cols); String[][] data = support.getData(rows, cols); final CustomModel model = new CustomModel(data, headers); final JTable table = new JTable(model); table.setAutoCreateColumnsFromModel(false); JButton add = new JButton("add column"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s = "column " + cols++; Object[] data = support.getDataForNewColumn(rows); support.addColumn(table, s, data); } }); JPanel north = new JPanel(); north.add(add); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(north, "North"); f.getContentPane().add(new JScrollPane(table)); f.setSize(500,300); f.setLocation(200,200); f.setVisible(true); } public static void main(String[] args) { new AddingAColumn(); } } class TableSupport { Random seed; public TableSupport() { seed = new Random(); } public void addColumn(JTable table, Object header, Object[] data) { CustomModel model = (CustomModel)table.getModel(); TableColumn col = new TableColumn(model.getColumnCount()); col.setHeaderValue(header); table.addColumn(col); model.addColumn(header.toString(), data); } public Boolean[] getDataForNewColumn(int rows) { Boolean[] s = new Boolean[rows]; for(int j = 0; j < rows; j++) s[j] = Boolean.FALSE; return s; } public String[] getHeaders(int cols) { String[] headers = new String[cols]; for(int j = 0; j < cols; j++) headers[j] = "column " + (j + 1); return headers; } public String[][] getData(int rows, int cols) { String[][] data = new String[rows][cols]; for(int row = 0; row < rows; row++) for(int col = 0; col < cols; col++) data[row][col] = "item " + seed.nextInt(401); return data; } } class CustomModel extends DefaultTableModel { String[] headers; Object[][] data; int[] noEditColIndices; public CustomModel(String[][] data, String[] headers) { super(data, headers); this.data = data; this.headers = headers; // no edit in first and third columns noEditColIndices = new int[] { 0, 2 }; } public Class getColumnClass(int col) { return getValueAt(0, col).getClass(); } public boolean isCellEditable(int row, int col) { for(int j = 0; j < noEditColIndices.length; j++) if(col == noEditColIndices[j]) return false; return true; } }
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Handling Focussing of JTable cells
sorting an adf table
Input....in JTable
[JTable] Non editable cells on editable column
How to diable a entire column in JTable?
More...