Set JTable to be uneditable?
I create JTable using DefaultTableModel. I want user can only view the data but cannot edit value in each cell. How can I do that ?
One way to accomplish this is to create your own table model, extending the AbstractTableModel. One of the methods you override is "isCellEditable".
MyTableModel mtm = new MyTableModel();
JTable myTable = new JTable(mtm);
private class MyTableModel extends AbstractTableModel {
public boolean isCellEditable(int rowPos, int columnPos) {
return false;
}
Similar Messages
-
How to set JTable cells to uneditable
I was trying to set the cells in my Jtable to uneditable.
I overrided the isCellEditable method. Here is what I did. But the
cells remain editable. Any ideas?
private class MyTableModel extends DefaultTableModel
public MyTableModel(Vector vectRow,Vector vecColumn)
super(vecVector,vecColumn);
public boolean isCellEditable(int row, int col)
if (col >= 0) {
return false;
}Just Use:
public boolean isCellEditable(int r, int c) { return false; } -
How to make a cell in a JTable to be uneditable and handle events
I tried many things but failed,How do you make a cell in a JTable to be uneditable and also be able to handle events>Anyone who knows this please help.Thanx
Hello Klaas2001
You can add KeyListener ,MouseListener
Suppose you have set the value of cell using setValueAt()
table.addKeyListener(this);
public void keyTyped(KeyEvent src)
String val="";
int r= table.getEditingRow();
int c= table.getEditingColumn();
val=table.getValueAt(r,c).toString();
if (r!=-1 && c!=-1)
TableCellEditor tableCellEditor = table.getCellEditor(r, c);
tableCellEditor.stopCellEditing();
table.clearSelection();
table.setValueAt(val,r,c);
public void keyReleased(KeyEvent src)
public void keyPressed(KeyEvent src)
table.addMouseListener(this);
public void mouseClicked(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
if(e.getClickCount()>=2)//Double Click
table.clearSelection();
int r= table.getEditingRow();
int c= table.getEditingColumn();
if (r!=-1 && c!=-1)
TableCellEditor tableCellEditor = table.getCellEditor (
r,c);
tableCellEditor.stopCellEditing();
table.clearSelection();
table.setValueAt("",r,c);
}//Mouse Released
You can remove keyListener and Mouse Listener whenever You want to edit
then add it later.
Regarding handling events implement javax.swing.event.TableModelListener
table.getModel().addTableModelListener(this);
public void tableChanged(javax.swing.event.TableModelEvent source)
TableModel tabMod = (TableModel)source.getSource();
switch (source.getType())
case TableModelEvent.UPDATE:
break;
}//Table Changed Method
//This method gets fired after table cell value is changed. -
Set JTable in another JTable Column
Hai Java Experts
Can we Set JTable in another JTable Column
In jsp we add one table in a column of another table. Like the previous way can we do it in swing?
Pls Help me with example..
Thanks[http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender]
db
edit And adding a table in a column of another table doesn't make any sense at all. Perhaps you meant showing a table in a cell of another table.
Edited by: DarrylBurke -
How to set JTable column's color?
How can I set JTable Columns' color? I only found this class DefaultTableCellRenderer
which can set cell's color.rmalina wrote:
You are going to need to derive a renderer class for your Column from DefaultTableCellRenderer and override the following function with something like this:
@Override
public Component getTableCellRendererComponent(JTable jTable, Object oValue, boolean isSelected, boolean hasFocus, int nRow, int nColumn) {
super.setForeground(Color.GREEN);
super.setBackground(Color.GREEN);
}That would set your column to green.
Edited by: rmalina on Jul 28, 2008 8:47 AMHow can I know I only change the columns' color instead of other cells? -
Hallo,
I have troubles setting jtable cell edtior. when i run code below, editor stays unchnaged.
Do you know where's the problem, please (except in programmer^_^) ???
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Test extends JFrame {
JTable table;
Test() {
// Create table model
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{ Boolean.TRUE, Integer.valueOf(10) },
{ "Hallo!", Boolean.FALSE },
}, new String[] { "Col1", "Col2" }));
// Setup frame a little
setLayout(new BorderLayout());
setBounds(new Rectangle(300,300,200,100));
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(table,BorderLayout.CENTER);
* Two calls below have no effect:-(
* What did i wrong?
// set cell editor for all cells
table.setCellEditor(new DefaultCellEditor(
new JComboBox(new String[] { "0", "1" }) ));
// Use checkbox for booleans
table.setDefaultEditor(Boolean.class, new DefaultCellEditor(
new JCheckBox() ));
// Use combobox for strings
table.setDefaultEditor(String.class, new DefaultCellEditor(
new JComboBox( new String[] { "Hallo!", "Bye!" } ) ));
public static void main(String[] arg) {
Test test = new Test();
test.setVisible(true);
}Hi again,
yes it works when i set it for single column, but i'd like to use default editor, because column count is not fixed.
According to documentation: "Sets a default cell editor to be used if no editor has been set in a TableColumn. If no editing is required in a table, or a particular column in a table, uses the isCellEditable method in the TableModel interface to ensure that this JTable will not start an editor in these columns. If editor is null, removes the default editor for this column class."
So when i call
column.setCellEditor(null); in TableColumnModelListener columnAdded event. But still it's not working. See sample below (i've changed it to cell renderer coz result is clear on single view, but problem stays same)
More over problem seems to be with algoritm that decide which class to use. Object takes preference any time. W/o it npe arries.
Regards
Adam
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class Test extends JFrame {
JTable table;
Test() {
// Create table model
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{ Boolean.TRUE, Integer.valueOf(10) },
{ "Hallo!", Boolean.FALSE },
}, new String[] { "Col1", "Col2" }));
// Setup frame a little
setLayout(new BorderLayout());
setBounds(new Rectangle(300,300,200,100));
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(table,BorderLayout.CENTER);
table.setDefaultRenderer(Object.class,new Renderer(Color.YELLOW));
table.setDefaultRenderer(String.class,new Renderer(Color.BLUE));
table.setDefaultRenderer(Boolean.class,new Renderer(Color.RED));
table.setDefaultRenderer(Integer.class,new Renderer(Color.GREEN));
// use default renderer in all columns
table.getColumn(table.getColumnName(0)).setCellRenderer(null);
table.getColumn(table.getColumnName(0)).setCellRenderer(null);
public static void main(String[] arg) {
Test test = new Test();
test.setVisible(true);
class Renderer extends DefaultTableCellRenderer {
Color color;
Renderer(Color color) { this.color = color; }
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBackground(color);
setToolTipText("Class is "+value.getClass().getName());
return this;
}Message was edited by:
a3cchan -
How to set jtable editable?
how to set jtable editable?
Hi,
Doubleclick on a particular cell to get the cellEditor for editing.
Cheers :)
Nagaraj -
How to set JTable cell editable/non-editable dynamically using NetBean?
I am using NetBean 6.5, create a database desktop application, I using the persistence entity manager to bind my JTable with database.
Now, I want to set practicular cells which are enable and disable, I tried to create my own table model and override the isEditable() method, which it seems didn't work. I guessed the problem is the persistence connection between my Table and database.
How can I solve it? Any example? I just googled the web and can't find example which use netbean combined with database. I know that a single JTable would work when creating your own model.
thx.I know what coding i did, Yes u r right but im new to GUI programming only,
That's why i posted it to forums, I gone through the Control Flow Statements link what u have given.
U didnt get my question at all.
Again the problem im facing is the table is already displayed with 2nd column uneditable.
When some Action done on table, i need to make 2nd column editable at run time.
Thanks if u provide me the solution instead of deciding what type of programmer im. -
How to set JTable row background color
Hello,
I have seen JTable with rows having alternate background color (mostly white and light blue). How can i set the same.
Thanks,
DeepakWell, i am new user to forum and so have no idea if this question has been asked previously "countless" times.Which is why you "search first" and "ask later". How to you expect to find out what valuable information is in the forum without learning how to do a simple search. This goes for any forum on the web you might use, so don't use the "I'm a new user" excuse.
-
Hi,
I would like to set the column width of JTable.
========================================
JTable table;
DefaultTableModel tableModel;
tableModel = new DefaultTableModel();
table = new JTable( tableModel);
JScrollPane scrollPane = new JScrollPane(table);
for(int i = 0; i < heading.length; i++)
tableModel.addColumn( heading[ i ]);
========================================Did you run the tutorial code? Did it work?
Now compare the tutorial code with your code to determine what is different.
We can't tell you what is different based on the one line of code you posted:
colModel.getColumn(i).setPreferredWidth(100); -
How to set jtable column width
Hello..
I 'm using a simple jtable with the default model. I just want to set each column size to make table smaller Is this possible?
Thanks
Ferasnewdeveloper wrote:
Hello..
I 'm using a simple jtable with the default model. I just want to set each column size to make table smaller Is this possible?
Thanks
FerasI think that your problem probably has more to do with the Layout you're using (or not using) than it does with individual column sizes. What layout are you using?
What happens when you set the size (or preferredSize, minimumSize, and maximumSize) on the whole JTable?
You might also be looking for JTable.[getColumnModel()|http://download-llnw.oracle.com/javase/6/docs/api/javax/swing/JTable.html#getColumnModel%28%29].[getColumn()|http://download-llnw.oracle.com/javase/6/docs/api/javax/swing/table/TableColumnModel.html#getColumn%28int%29].[setPreferredWidth()|http://download-llnw.oracle.com/javase/6/docs/api/javax/swing/table/TableColumn.html#setPreferredWidth%28int%29].
If none of those approaches work, post an SSCCE that demonstrates the problem.
PS- Swing questions belong in the Swing forum. -
Hi,
I would like to know if anyone know how to set a specific cell colour in a JTable, or if they know any links to when i can find help...thanks in advance
RudyTake a look at TableCellRenderer and its default implementation
HTH
Mike -
Hello,
I have a one column JTable that I would like not to be editable. How do I set the table not be editable? I am not using a table model. Would that be a problem?
I am using the folllwing code:
Vector rows = new Vector(0); //create a vector for the rows
//add a vector within a vector
for(int i = 0; i < 10; i++)
//create a new vector and add the values to the vector
Vector oneRow = new Vector(0);
//fill one row
oneRow.add("Row Input"));
//add the new vector to the outer vector
rows.add(oneRow);
} //add the header to the byte column
Vector cols = new Vector(0);
cols.add("Values");
//create a JTable with one col and rows the size of the byte array
JTable myTable = new JTable(rows, cols);look at DefaultTableModel it has a isCellEditable(int rowl, int col)
method which you can override for your own needs. -
Set JTable resize manual behaviour
I've been trying for the better part of the day to implement a manual resize behavior for table column widths but without success.
The JTable.setAutoResizeMode method that does this automatically has a couple of modes but not a weight mode (closest would be the one allocating new space equally to all columns, i.e. same weights).
What I want to do is to change the columns width according to preset weights when the table changes size. The reason behind this is that I want to set the columns to a specific width percentage (e.g. 80% 10% 10% 0%). But when the application is resized and subsequently the table the percentages gets skewed since the AutoResizeMode (if set to ALL_COLUMNS) will add surplus space equally to all columns.
My first approach was to implement a component listener that does just this:
public void componentResized(ComponentEvent e) {
JTable table = (JTable) e.getComponent();
TableColumnModel model = table.getColumnModel();
double before = 0.0d;
for(int i = 0; i < model.getColumnCount(); ++i)
before += model.getColumn(i).getWidth();
double delta = table.getSize().getWidth() - before;
for(int i = 0; i < model.getColumnCount(); ++i) {
TableColumn column = model.getColumn(i);
column.setWidth((int) (column.getWidth() + columnWeights*delta));
... but the AutoResizeMode overrides this but if I turn off the AutoResizeMode then the table will not resize at all which means the component listener wont do a thing.
Any ideas as how to approach this problem would be appreciated!
/MaigoCheck out camickr's [Table Column Adjuster|http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/]
db -
How can I set JTable's columns width?
Please, can anyone give me a hint.
It seems I miss something important.
I can't set the table columns to an appropriate width.
Please, help !!!In JTable you can get a TableColumn object by supplying the name of the column to the method getColumn in JTable.
JTable myTable;
TableColumn tableColumn;
// Initialise table
tableColumn = myTable.getColumn("COLUMN NAME");There are methods for the TableColumn such as
tableColumn.setPreferredWidth(int);
tableColumn.setMaxWidth(int);
tableColumn.setMinWidth(int);
which all deal with setting up the width of the column in terms of pixel width.
You can look up more info about them in the API
Maybe you are looking for
-
R/3 data Not matching 0PS_C04 cube
hello guys, we had an oracle upgarde last week and "apprently after the upgarde the data in this cube isn't matching the data in r/3". first i thought may be the data in the cube was inaccurate, so 1) i cleaned the cube reloaded all the data accordin
-
My iPhone 4S went into lockup with a screen displaying an image of the USB connector pointing to the iTunes logo. I was traveling and took the phone to the closest Verizon store. The store informed me that I had a brick lock and that only an Apple re
-
How do I update my iTunes version manually without notification?
I have updated my iphone to use IOS.7.4 and iTunes is now asking me to download 11.1 as 11.0 is not compatible. It does not give me the option to upgrade to this version only to go to apple.com and download. I can only find the option to download as
-
Revision: 10898 Author: [email protected] Date: 2009-10-06 13:54:04 -0700 (Tue, 06 Oct 2009) Log Message: Fix for a bug: After playing a hide effect, a window wouldn't be visible again. The issue here is that all effects that occur because of H
-
Does anyone know how to make the iPhone default photo size 2 x 3 so you don't have to crop every photo before printing as 10cm x 15cm (normal print size on snapchat)? I find it very odd that the iPhone default sizing is different to the crop format i