| | |
adding columns
![]() |
•
•
Join Date: Jul 2007
Posts: 66
Reputation:
Solved Threads: 14
I'm also assuming you're talking about adding columns to a JTable.
Adding a new column to a table is quite simple. it goes something like this:
Unfortunately, the hard part is getting meaningful data to the column.
A JTable (usually) works in the following way:
You have a list of data.
You have a TableModel that transform an object from the list of data into columns.
You have the JTable that converts the information from the TableModel into TableColumns.
This way each column is mapped to specific data from the objects in your datalist. By dynamically adding columns in the way I described above, you also have to either write a tablemodel that handles variable number of columns, or you somehow have to fetch the data you want to display in a custom cellRenderer.
Edit:
To answer the rest of the question - how to add it with a button:
Hope that helps
Adding a new column to a table is quite simple. it goes something like this:
java Syntax (Toggle Plain Text)
JTable tabell = new JTable(); TableColumn column = new TableColumn(); column.setHeaderValue("New column"); tabell.getColumnModel().addColumn(column);
Unfortunately, the hard part is getting meaningful data to the column.
A JTable (usually) works in the following way:
You have a list of data.
You have a TableModel that transform an object from the list of data into columns.
You have the JTable that converts the information from the TableModel into TableColumns.
This way each column is mapped to specific data from the objects in your datalist. By dynamically adding columns in the way I described above, you also have to either write a tablemodel that handles variable number of columns, or you somehow have to fetch the data you want to display in a custom cellRenderer.
Edit:
To answer the rest of the question - how to add it with a button:
java Syntax (Toggle Plain Text)
class ButtonListener implements ActionListener { private JTable table; public ButtonListener(JTable table) { this.table = table; } public void actionPerformed(ActionEvent e) { TableColumn column = new TableColumn(); column.setHeaderValue("column header"); this.table.getColumnModel().addColumn(column); } } JTable table = new JTable(); JButton button = new JButton(); button.addActionListener(new ButtonListener());
Hope that helps
Last edited by vidaj; Jul 18th, 2008 at 5:31 am. Reason: added button code
![]() |
Similar Threads
- extract columns from a text file in vb (Visual Basic 4 / 5 / 6)
- Syntax for adding two columns and sorting the results (MySQL)
- C Program for Adding two numbers without using + sign (C)
- Dynamically adding/removing columns in datagrid (VB.NET)
- Adding and deleting a column in JTable (Java)
- need help with tables (VB.NET)
- Listbox with Columns (Visual Basic 4 / 5 / 6)
- Adding a checkbox column dynamically (C#)
- Adding controls into a ListView (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Help with part 2 of Inventory Program
- Next Thread: Login authentication
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





