could not set the column width to zero i.e. not made column invisible
not, never to remove
TableColumn
fromTableModel
, this is wrong suggestion, instead of to use built-in method JTable#removeColumn(TableColumn aColumn),notice, this method remove column only from View, in the model is removed column still presents, and you can revert that, visible the removed column by using JTable#addColumn(TableColumn aColumn)
EDIT
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TableRowHeight {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame("p*s*s*s*s*t*t");
private String[] columnNames = {"one", "two", "Playing with", "four", "five",};
private String[][] data = {
{"aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeeee",},
{"bbbbbb", "cccccc", "dddddd", "eeeeeee", "aaaaaa",},
{"cccccc", "dddddd", "eeeeeee", "aaaaaa", "bbbbbb",},
{"dddddd", "eeeeeee", "aaaaaa", "bbbbbb", "cccccc",},
{"eeeeeee", "aaaaaa", "bbbbbb", "cccccc", "dddddd",}};
private JTable table = new JTable(new DefaultTableModel(data, columnNames));
private TableColumnModel tcm = table.getColumnModel();
private Stack<TableColumn> colDeleted = new Stack<TableColumn>();
private JButton restoreButton = new JButton("Restore Column Size");
private JButton hideButton = new JButton("Set Column Size to Zero");
private JButton deleteButton = new JButton("Delete Column");
private JButton addButton = new JButton("Restore Column");
public TableRowHeight() {
table.setRowMargin(4);
table.setRowHeight(30);
table.setFont(new Font("SansSerif", Font.BOLD + Font.PLAIN, 20));
JScrollPane scrollPane = new JScrollPane(table);
for (int i = 0; i < (tcm.getColumnCount()); i++) {
tcm.getColumn(i).setPreferredWidth(100);
}
table.setPreferredScrollableViewportSize(table.getPreferredSize());
restoreButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tcm.getColumn(2).setPreferredWidth(100);
}
});
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tcm.getColumn(2).setPreferredWidth(000);
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (table.getColumnCount() > 0) {
TableColumn colToDelete = table.getColumnModel().getColumn(table.getColumnCount() - 1);
table.removeColumn(colToDelete);
table.validate();
colDeleted.push(colToDelete);
addButton.setEnabled(true);
} else {
deleteButton.setEnabled(false);
}
}
});
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (colDeleted.size() > 0) {
table.addColumn(colDeleted.pop());
table.validate();
deleteButton.setEnabled(true);
} else {
addButton.setEnabled(false);
}
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(hideButton);
btnPanel.add(restoreButton);
btnPanel.add(deleteButton);
btnPanel.add(addButton);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(btnPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
TableRowHeight frame = new TableRowHeight();
}
});
}
}
EDIT2
you have to check too
JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JTable.getTableHeader().setReorderingAllowed(false);
unable to set column width after jtable becomes visible
as i have pointed out, removing the table column not only adds complexity
I agree, and I gave you working code that manages this complexity. I see you didn't try the code even though it can be added to your program with a single line of code.
try adding and removing data from the table
That is not an issue. All a TableColumn does is map the data from the TableModel to the TableColumn. When you create a TableColumn, it is created with a default column value of 0. So yes, unless you manage this properly you will have a problem, which is why I gave you a link to working code.
my question as to why column sizes are not adjusted when invoking setMin, setPreferred and setMax.
The answer is that order of code execution is important. That is the preferred size must be in range of the min/max sizes, so you need to set the preferred size last:
tableColumn.setMinWidth(minWidth);
tableColumn.setMaxWidth(maxWidth);
tableColumn.setPreferredWidth(preferredWidth);
Edit:
Simple example. You can call it an MCVE
or a SSCCE.
import java.awt.*;
import javax.swing.table.*;
public class Main
{
public static void main(String[] args)
{
TableColumn tc = new TableColumn();
tc.setMinWidth(1);
tc.setMaxWidth(10);
tc.setPreferredWidth(15);
System.out.println( tc.getPreferredWidth() );
}
}
If this does not verify that the API is working correctly, then I don't know what will.
How to hide a particlar column in DefaultTableModel from displaying it in table?
by default minimum size is
10 pixels
widht,you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)
How to hide the Jtable column data?
You need to remove the TableColumn
from the TableColumnModel
of the JTable
. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel
. to access the data you use:
table.getModel().getValueAt(...);
Hide a column in JTable
Set the Column Minimum and Maximum width as zero.
table.getColumnModel().getColumn(columnIndex).setMinWidth(0);
table.getColumnModel().getColumn(columnIndex).setMaxWidth(0);
As suggested by Andrew Thomson in the comment section you can also use removeColumn.
From javaDoc;
removeColumn
public void removeColumn(TableColumn aColumn)
Removes aColumn from this JTable's array of columns. Note: this method does
not remove the column of data from the model; it just removes the
TableColumn that was responsible for displaying it. Parameters:
aColumn - the TableColumn to be removed
P.S: But I have personally used the first approach to hide a column in the JTable
. Thanks for removeColumn
method I will try to use it from now on.
Can not set ASP.Net Gridview Template Column width to 0
I found TemplateColumn
HeaderText
is automatically set to four spaces (automatically generated), setting the HeaderText
to one space from UI fixed the problem.
Related Topics
How to Create Variables at Runtime in Java
Compiler Error: "Class, Interface, or Enum Expected"
How to Get a Index Value from Foreach Loop in Jstl
Javafx Entirely Customized Windows
Multiple Inheritance for an Anonymous Class
Oracle Jdbc Intermittent Connection Issue
Java Implementation of JSON to Xml Conversion
Read Rsa Private Key of Format Pkcs1 in Java
Java: Get Month Integer from Date
Difference Between Using Throwable and Exception in a Try Catch
Apache Poi Error Loading Xssfworkbook Class
Eclipse/Maven Error: "No Compiler Is Provided in This Environment"
How to Call Methods in Constructor in Java