Resize Row in a JTable??
Hi,
Is it possible to allow the 'user' to resize the height of a row in JTable??
I know that we can programatically set the height of a row through setRowHeight method.
But my requirement is to allow the 'user' to resize the height of a row. If resized, how can I get the new height of the row??
Regards,
Satish
Very interesting and difficult question.
I think the best way is to create a row header (like in excel) which allows to resize the rows individually.
I will work on this problem.
Denis
Similar Messages
-
hi,
Is there any way to resize row header column of a jtable just as other columns of jtable. i searched in forums and google but couldnt find any article with resizing row header.
thanxIf you want help in the future I suggest you remain a little more patient. It's the weekend. People don't hang around waiting for you to post a question. We answer questions if and when we have time and we know the answer.
The general answer is yes, of course, it can be done. But we can't give you a specifice answer since you question is so general.
For example, take a look at my code in this posting:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=639189
I made the following changes:
// addColumn( new TableColumn() );
TableColumn column = new TableColumn();
column.setHeaderValue(" ");
addColumn( column );And then I overrode the following method of the table:
public void columnMarginChanged(ChangeEvent e)
super.columnMarginChanged(e);
TableColumn resizingColumn = getTableHeader().getResizingColumn();
if (resizingColumn != null)
Dimension d = getPreferredSize();
d.width = resizingColumn.getWidth();
setPreferredScrollableViewportSize(d);
}and finally
scrollPane.setRowHeaderView( lineTable );
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, lineTable.getTableHeader()); // newOf course we have no idea what component you are using for your row header so we can't give a detailed solution, but I suspect you would need to do something like the following:
a) add a mouseListener to your component
b) calculate the change in size of the component using the mouse location changes
c) set the preferred size of your component
d) notify the scroll pane of the change in size. I would look at the setPreferredScrollableViewportSize method of JTable to see what it does. -
JTable - Resize rows the same way as the columns
Hi!
I have run into a very frustrating problem and have tried to solve this for many hours but can't come up with a good solution. If there is anyone that can help me on the right track I will be very grateful!
When I'm using a JTable it provides good "graphical" functionality to handle resize of colums, but when it comes to resize rows it is not available.
I have found a solution for the row height adjustment in the following post: [http://forums.sun.com/thread.jspa?forumID=257&threadID=453665]
That solution only partially work in my case because of the following:
The JTable exists inside a JInternalFrame and the JTable should not be able to get bigger then the actual size of the InternalFrame it is put in (so no scrollbar).
The problem with the solution above is that the JTable total height will also expand when a row is resized (contrary to what happens when the columns are resized), ending up with a JTable that is bigger then the InternalFrame.
So my question is:
Is it possible to provide the same ("graphical") resize functionality for rows as for columns in a JTable? Or any row resize functionality that prevent the JTable from getting bigger then the container it is placed in?
Thanks for your time!
Best regards,
MarcusThanks for the fast answer!
Yes, that would solve the height expanding problem. Thanks!
It would be even better though if the row resize functionality would behave the same as the column resize (for consistency). For example: If a row is resized the other rows will get smaller so they all fit into the InternalFrame.
It has to be some easy way to do this. I have started on a solution with component listeners etc. (to track resize) but it is quite hard to get it right and I keep getting the feeling I reinventing the wheel all over again:(
Any ideas out there? -
JTable: Resizable Row Header
Hi,
Did anyone implement a row header class for JTable that allows to resize rows (the height of rows) like JTableHeader does for columns?Take a look at the link shown below:
http://forum.java.sun.com/thread.jsp?forum=57&thread=252175
;o)
V.V. -
How to refresh an existing row of a JTable
Hi all,
I have to refresh the data of an existing row of a JTable after some time in thread. The code snippet is
DefaultTableModel model=new DefaultTableModel(data,columnNames);
table = new JTable(model)
public boolean isCellEditable(int row, int col)
return false;
Now I also add rows to this table within the run() of the thread as
model.addRow(new Object []{sub1,sub6,sub12,sub3,sub18});
My problem is that I want to refresh the data of this added row within the thread.
Any help is highly appreciable. Thanks in advance.
Regards,
Har KrishanHmmm. His qhestion does not seem to be with how to change the value of a field, but how to get the table to recognize the change. I thought such things were automatic. The model fires a value changed event and the JTable picks up the event and refreshes. I'm not sure why your table is not refreshing under these circumstances. Perhaps a more complete code snippet. Please use the open and close code tags to format your code if you include it.
-
To get the unique id of the selected row in a JTable as in Database
Hi,
After fetching the recodrs from the Database, I have displayed them in a JTable. Though I fetched all the columns from db, I am displying only only 2 columns, say Name and Number neither of which is unique, however I have a unique ID for each record in the db. Now when I select some row in the JTable, I need to know its unique ID (the one which is in the db) which i have not displayed in the JTable.
Is there any API method that can store the unique ids of a table? Or how can this be done?
Thanks in Advance.Although, if you don't want the Id visible in the table, then you need to remove its TableColumn from the TableColumnModel.
Then when you want to reference the id you need to use:
table.getModel.getValueAt(...); -
How to delete the selected rows in a JTable on pressing a button?
How to delete the selected rows in a JTable on pressing a button?
You are right. I did the same.
Following is the code where some of them might find it useful in future.
jTable1.selectAll();
int[] array = jTable1.getSelectedRows();
for(int i=array.length-1;i>=0;i--)
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
model.removeRow(i);
} -
How to clear the rows in a jTable
Can anyone tell me how to clear the contents of all the rows in a jTable?
This is how I did. Posting the same.
jTable1.selectAll();
int[] array = jTable1.getSelectedRows();
for(int i=array.length-1;i>=0;i--)
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
model.removeRow(i);
Regards. -
How can i add rows to a JTable at run time ??????
hi there
how can i add a row to a JTable at run time? and display the table after the change? thank you.For adding or removing the rows from the JTable, you have to use the methods on the table model. I would show you a simple implementation of table model.
public class MyTableModel extends AbstractTableModel {
private ArrayList rowsList = null;
private String [] columns = { "Column 1" , "Column 2", "Column 3"};
public MyTableModel() {
rowsList = new ArrayList();
public int getRowCount() {
return rowsList.size();
public int getColumnCount() {
return columns.length;
public void addRow(MyRow myRow) {
//MyRow is any of your object.
rowsList.add(myRow);
fireTableDataChanged();
public void removeRow(int rowIndex) {
rowsList.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
public Object getValueAt(int row, in col) {
MyRow currentRow = (MyRow)rowsList.get(row);
switch (col) {
case 0:
//return the value of first cell
break;
case 1 :
//return the value of second cell
break;
case 2 :
//return the value of third cell
break;
}Then create the table using the TableModel using the constructor new JTable(TableModel) and then when you want to add/remove a row from the table, call myTableModel.addRow(MyRow) or myTableModel.removeRow(rowIndex)....I hope that this solves your problem. -
How to increase the number of rows in a jTable dynamically?
Can anyone help me in increasing the number of rows in a jTable dynamically without using DefaultTableModel?
I don't want to use DefaultTableModel because my program is about to finish except this problem.
Thanks in advance.Presumably you're using your own custom table model class. Modify that so that it returns the number of rows you want. Though presumably it would do this anyway so I've no idea what the problem is.
Do you mean you've added rows to the table model but they're not being reflected in the table itself? You need to fire events - AbstractTableModel provides simple methods to fire them, but if you're not using that then you'll have to fire them manually. -
To change the font of a selected row in a Jtable
Hello,
Is it possible to change the font of a selected row in a jtable?
i.e. if all the table is set to a bold font, how would you change the font of the row selected to a normal (not bold) font?
thank you.String will be left justified
Integer will be right justified
Date will be a simple date without the time.
As it will with this renderer.Only if your custom renderer duplicates the code
found in each of the above renderers. This is a waste
of time to duplicate code. The idea is to reuse code
not duplicate and debug again.
No, no, no there will be NO duplicated code.
A single renderer class can handle all types ofdata.
Sure you can fit a square peg into a round hole if
you work hard enough. Why does the JDK come with
separate renderers for Date, Integer, Double, Icon,
Boolean? So that, by default the rendering for common classes is done correctly.
Because its a better design then having code
with a bunch of "instanceof" checks and nested
if...else code.This is only required for customization BEYOND what the default renderers provide
>
And you would only have to use instanceof checkswhen you required custom
rendering for a particular classAgreed, but as soon as you do require custom
renderering you need to customize your renderer.
which you would also have to do with theprepareRenderer calls too
Not true. The code is the same whether you treat
every cell as a String or whether you use a custom
renderer for every cell. Here is the code to make the
text of the selected line(s) bold:
public Component prepareRenderer(TableCellRenderer
renderer, int row, int column)
Component c = super.prepareRenderer(renderer, row,
, column);
if (isRowSelected(row))
c.setFont( c.getFont().deriveFont(Font.BOLD) );
return c;
}It will work for any renderer used by the table since
the prepareRenderer(...) method returns a Component.
There is no need to do any kind of "instanceof"
checking. It doesn't matter whether the cell is
renderered with the "Object" renderer or the
"Integer" renderer.
If the user wants to treat all columns as Strings or
treat individual columns as String, Integer, Data...,
then they only need to override the getColumnClass()
method. There is no change to the prepareRenderer()
code.
Have you actually tried the code to see how simple it
is?
I've posted my code. Why don't you post your solution
that will allow the user to bold the text of a Date,
Integer, and String data in separate column and then
let the poster decide.Well, I don't see a compilable, runnable demo anywhere in this thread. So here's one
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Vector;
public class TableRendererDemo extends JFrame{
String[] headers = {"String","Integer","Float","Boolean","Date"};
private JTable table;
public TableRendererDemo() {
buildGUI();
private void buildGUI() {
JPanel mainPanel = (JPanel) getContentPane();
mainPanel.setLayout(new BorderLayout());
Vector headerVector = new Vector(Arrays.asList(headers));
Vector data = createDataVector();
DefaultTableModel tableModel = new DefaultTableModel(data, headerVector){
public Class getColumnClass(int columnIndex) {
return getValueAt(0,columnIndex).getClass();
table = new JTable(tableModel);
// table.setDefaultRenderer(Object.class, new MyTableCellRenderer());
table.setDefaultRenderer(String.class, new MyTableCellRenderer());
table.setDefaultRenderer(Integer.class, new MyTableCellRenderer());
table.setDefaultRenderer(Float.class, new MyTableCellRenderer());
table.setDefaultRenderer(Date.class, new MyTableCellRenderer());
JScrollPane jsp = new JScrollPane(table);
mainPanel.add(jsp, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
private Vector createDataVector(){
Vector dataVector = new Vector();
for ( int i = 0 ; i < 10; i++){
Vector rowVector = new Vector();
rowVector.add(new String("String "+i));
rowVector.add(new Integer(i));
rowVector.add(new Float(1.23));
rowVector.add( (i % 2 == 0 ? Boolean.TRUE : Boolean.FALSE));
rowVector.add(new Date());
dataVector.add(rowVector);
return dataVector;
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
TableRendererDemo tableRendererDemo = new TableRendererDemo();
tableRendererDemo.setVisible(true);
SwingUtilities.invokeLater(runnable);
class MyTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if ( isSelected){
setFont(getFont().deriveFont(Font.BOLD));
else{
setFont(getFont().deriveFont(Font.PLAIN));
if ( value instanceof Date){
SimpleDateFormat formatter =(SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.MEDIUM);
setText(formatter.format((Date)value));
if(value instanceof Number){
setText(((Number)value).toString());
return this;
}Hardly a "bunch of instanceof or nested loops. I only used the Date instanceof to allow date format to be specified/ modified. If it was left out the Date column would be "18 Apr 2005" ( DateFormat.MEDIUM, which is default).
Cheers
DB -
The columns of a selected row in a JTable
Hello guys,
I am trying to loop through the columns of a selected row in a JTable. Any ideas how i can do that?
Thanks in advance for your replies.
Antana.there is getValueAt(int row, int column) method in JTable.
will this help you?
and please post swing related queries to swing forum.
--Azodious_ -
Deleting a row from a JTable using AbstractTableModel
Hi,
Can someone please help me on how should i go about deleting a row in a jtable using the AbstractTableModel. Here i know how to delete it by using vector as one of the elements in the table. But i want to know how to delete it using an Object[][] as the row field.
Thanks for the helpHi,
I'm in desperate position for this please help -
How can I add a new row in a JTable dynamically?
Dear Sir(s)
I want to add a new row in a Jtable as I press enter key the time focus is on the last cell of the row? pls helpTomDooley wrote:
Hello,
...I write directly to a cvs file ...my loop slows down to 0.2 Hz
Writing a series of 5 values to a file should not take so long. There is probably something wrong in the way you are saving your data. May be you should post a simplified version of your vi, so we could see how to improve your code.
CC
Chilly Charly (aka CC)
E-List Master - Kudos glutton - Press the yellow button on the left... -
How do you set the font color for a specific entire row inside a JTable?
How do you set the font color for a specific entire row inside a JTable?
I want to change the font color for only a couple of rows inside a JTable.
I've seen some ways to possibly do this with an individual cell.
Clarification on changing the font color in an individual cell would be helpful too if
there is no easy way to do this for a row.hai,
Try out with this piece of code.Create your table and assign the renderer to each column in the table.
CellColorRenderer m_CellColorRenderer = new CellColorRenderer();
for(int i=0;i<your_JTable.getColumnCount();i++)
your_JTable.getColumnModel().getColumn(i).setCellRenderer(m_CellColorRenderer);
class CellColorRenderer extends JLabel implements TableCellRenderer
CellColorRenderer()
setOpaque(true);
setHorizontalAlignment(LEFT);
setVerticalAlignment(CENTER);
setBackground(Color.white);
setForeground(Color.black);
protected void setValue(Object value)
setText((value == null) ? "" : value.toString());
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row,int column)
if(isSelected == true)
setForeground(Color.red);
else
setForeground(Color.black);
setValue(value);
return this;
regards,
bala
Maybe you are looking for
-
How do I get rid of the floating black prompt over my "screen saver" about airplay
I have 5 apple tvs and when the screen saver comes on after a minute or 2 an annoying prompt comes up covering part of the pictures on the screen saver prompting me on the use of airplay. I want it "THE PROMPT" to go away and not come back any other
-
I'm using my iPad 1 and wanting to print a page from a book that I purchased from iBooks. I can't figure out how to print. Any suqqetions? Thanks.
-
Insertfrom.jsf problem on submit
Hi Everyone, I appreciate if anyone can give me a clue on what could be the problem. I wasted few hours on this without any sign on resolving the problem. I have a form with data fields that are filled and submitted. There is validation check on each
-
Blurry Scroll Bars on Cover Flow
I know this has been posted last month, but no one answered it. Is there any fix to the blurry scroll bars in iTunes? It's a little embarrassing to have my cool Apple look gross in iTunes... Thanks!
-
Is imp logging/nologging aware ?
Hi, I'm looking a way to reduce redo generation during import via imp utility in oracle 10g (imp because exp was done in 9i so no impdp allowed). And I wonder if setting table to NOLOGGING after importing with rows=n can reduce redo generation rate w