Columns in JTable, Help!
Hello There,
I have two question about the JTable in Java:
1. How can i set a column width? I want to have one column widder than the other and not editable at runtime.
2. How can i have multiple lines (block of data/small paragraph) in one cell? because whenver i write a 3 new lines of data in a cell the JTable converts it cont. line.
Please Help!!
Here there is a JTable where 3 colums are being resized
TableColumn column1 = null;
for (int i = 0; i < 3; i++) {
column1 = table1.getColumnModel().getColumn(i);
if (i == 0) {
column1.setPreferredWidth(20);
} else {
if (i == 1){
column1.setPreferredWidth(160);
else
column1.setPreferredWidth(80);
}
Similar Messages
-
HELP Friends!!! How to set the width of columns of JTABLE?
Hi!
How to set the width of a particular column in JTable?
I tried using headerRenderers and though I'm able to set the color and font of the header (using getTableCellRendererComponent method)I can't change the size.
Pls some one help me..it's very urgent.
Thanks in advance.
Regards,
NavalJTable.getColumnModel().getColumn(i).setWidth(width). There are also setPreferredWidth, setMaxWidth and setMinWidth methods.
-
How to set different renderers to different cells in same column of JTable?
Hello Friends,
I need your help again...
Does any body knows, how to set different renderer's for different cells of same column in JTable..??
For ex.
Col1 Col2 Col3 Col4
A A A A
A A A B
A A A C
A A A D
Where A B C D would be different Renderers. I want set exactly same ,,, ie. one column with different renderer at different cell positions..
Right now i m setting renderer using statement bellow :
table.getColumnModel().getColumn(int).setCellRenderer(rederer_Instance);But with this, effect in the last renderer is applicable whole column....
Can any body help me out ?????????
please refer this thread for similar kind of discussion...
http://forums.sun.com/thread.jspa?forumID=57&threadID=571445Thanks
SuyogPlease refer to the first reply of [this thread|http://forums.sun.com/thread.jspa?forumID=57&threadID=571445] for the answer. If you have a specific problem implementing it, post you code with a specific question.
-
How to set color of a row depending on the value of column in JTable?
Hi All,
I have a JTable that add rows when the user clicks on the button. In this way there can be any no. of rows in my table. My table contains five columns. When a new row is added , it is added with new data each time. Also the data of the rows keep on changing time to time.
My problem is that when the data value for the third column comes out to be -ve then color of the row should be red and if its value is +ve then the color of the row should be green.
I have tried for this in the way but it is not working properly.
public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int vColIndex)
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
if(rowIndex<table.getRowCount() && change<0 )
c.setForeground(Color.red);
c.setFont(new Font("TimesRoman",Font.PLAIN,11));
else if(rowIndex<table.getRowCount() && change>0)
c.setForeground(new Color(20,220,20));
c.setFont(new Font("TimesRoman",Font.PLAIN,11));
return c;
where change is the value of the third column.
Any help is highly appreciated.
Thanx in advance.
Regards,
Har KrishanPerhaps you'll find this link useful. It gives a general idea of how you can create and adjust your custom renderer:
http://www.senun.com/Left/Programming/Java_old/Examples_swing/JTableExamples4.html
Hope it helps.
Eugene -
Add and remove columns from JTable
Help me please!
A try to remove column from JTable. It's removed, but when I try to add column in table, then I get all old (removed early) columns + new column....
I completely confused with it.....
Here is my code for remove column:
class DelC implements ActionListener
public void actionPerformed (ActionEvent e )
int [] HowManyColDelete = table.getSelectedColumns();
if (HowManyColDelete.length !=0)
TableColumnModel tableCModel = table.getColumnModel();
for (int i = HowManyColDelete.length-1; i>-1; i--)
table.getColumnModel().removeColumn (tableCModel.getColumn (HowManyColDelete [ i ]));
else
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Column is not selected!");
}It's little ex for me, I just try understand clearly how it's work (table models i mean). Here is code. All action with tables take place through menu items.
My brain is boiled, I've try a lot of variants of code, but did't get right result :((
It's code represent problem, which I've describe above. If you'll try remove column and then add it again, it will be ma-a-a-any colunms...
I understand, that my code just hide columns, not delete from table model....
But now I have not any decision of my problem...
Thanks a lot for any help. :)
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
class JTableF extends JFrame
Object [] [] data = new Object [0] [2];
JTable table;
DefaultTableModel model;
String [] columnNames = {"1", "2"};
TableColumnModel cm;
JTableF()
super("Table features");
setDefaultLookAndFeelDecorated( true );
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JMenuBar MBar = new JMenuBar();
JMenu [] menus = {new JMenu("A"), new JMenu("B")};
JMenuItem [] menu1 = {new JMenuItem("Add row"), new JMenuItem("Delete row", 'D'), new JMenuItem("Add column"), new JMenuItem("Delete column")};
menu1 [ 0 ].addActionListener(new AddL());
menu1 [ 1 ].addActionListener(new DelL());
menu1 [ 2 ].addActionListener(new AddC());
menu1 [ 3 ].addActionListener(new DelC());
for (int i=0; i<menu1.length; i++)
menus [ 0 ].add( menu1 [ i ]);
for (int i=0; i<menus.length; i++)
MBar.add(menus );
JPanel panel = new JPanel ();
model = new DefaultTableModel( data, columnNames );
table = new JTable (model);
cm = table.getColumnModel();
panel.add (new JScrollPane(table));
JButton b = new JButton ("Add row button");
b.addActionListener(new AddL());
panel.add (b);
setJMenuBar (MBar);
getContentPane().add(panel);
pack();
setLocationRelativeTo (null);
setVisible (true);
class DelC implements ActionListener
public void actionPerformed (ActionEvent e )
int [] HowManyColDelete = table.getSelectedColumns();
if (HowManyColDelete.length !=0)
TableColumnModel tableCModel = table.getColumnModel();
for (int i = HowManyColDelete.length-1; i>-1; i--)
int vizibleCol = table.convertColumnIndexToView(HowManyColDelete [ i ]);
tableCModel.removeColumn (tableCModel.getColumn (vizibleCol));
//cm = tableCModel;
else
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Column is not selected!");
class AddC implements ActionListener
public void actionPerformed (ActionEvent e)
//table.setColumnModel(cm);
Object NewColumnName = new String();
NewColumnName = JOptionPane.showInputDialog ("Input new column name", "Here");
int i = model.getRowCount();
int j = model.getColumnCount();
Object [] newData = new Object [ i ];
model.addColumn ( NewColumnName, newData);
class AddL implements ActionListener
public void actionPerformed (ActionEvent e)
int i = model.getColumnCount();
Object [] Row = new Object [ i ];
model.addRow ( Row );
class DelL implements ActionListener
public void actionPerformed (ActionEvent e)
int [] HowManyRowsDelete = table.getSelectedRows();
if (HowManyRowsDelete.length !=0)
for (int k = HowManyRowsDelete.length-1; k>-1; k--)
model.removeRow (HowManyRowsDelete[k]);
else
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Row is not selected!");
public static void main (String [] args)
javax.swing.SwingUtilities.invokeLater(new Runnable()
public void run()
JTableF inst = new JTableF(); -
Keyboard Access For a Column in JTable with default editor as JComboBox
I want to get Key board access for a column in JTable.
The user should be able to select from a drop down list for the column with default
editor set as JComboBox.
Presently,it works fine with mouse,also I am able to focus it with Keyboard using
ALT+Up keys,but how to make drop down list appear.
Plz help,it's urgent.
Thanks in AdvanceHi,
In addition to setting DO_SUM = 'X' you need to specify function in H_FTYPE field. It should be set to 'AVG' in your case.
ls_fielcat-do_sum = 'X'.
ls_fieldcat-h_ftype = 'AVG. -
Set the color of a row depending the value of the column in JTable?
Hi All,
I have a JTable that add rows when the user clicks on the button. In this way there can be any no. of rows in my table. My table contains five columns. When a new row is added , it is added with new data each time. Also the data of the rows keep on changing time to time.
My problem is that when the data value for the third column comes out to be -ve then color of the row should be red and if its value is +ve then the color of the row should be green.
I have tried for this in the way but it is not working properly.
public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int vColIndex)
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
if(change<0 && rowIndex<table.getRowCount() )
c.setForeground(Color.red);
c.setFont(new Font("TimesRoman",Font.PLAIN,11));
else if(change>0&&rowIndex<table.getRowCount() )
c.setForeground(new Color(20,220,20));
c.setFont(new Font("TimesRoman",Font.PLAIN,11));
return c;
where change is the value of the third column.
Any help is highly appreciated.
Thanx in advance.
Regards,
Har KrishanWhy do you have 3 postings on this topic all made within minutes of each other? (see [url http://forum.java.sun.com/thread.jspa?threadID=574547&tstart=0]here and [url http://forum.java.sun.com/thread.jspa?threadID=574543&tstart=0]here).
If you created a post by mistake then make a comment in the posting so people don't waste there time attempting to answer an old post.
where change is the value of the third column.How do you know "change" is the value of the third column? Did you use a System.out.println(...) to verify this.
A better approach is to use:
Object o = table.getModel().getValueAt(row, 2);
Then convert the Object to an int value and do your testing. This way you are guaranteed to be testing against the correct value. -
Serial number column in Jtable
Apologies for this distraction.
I read at this forum about a year ago how to add a s/n column in Jtable such that all the nos are automatically updated/adjusted when a row is added/deleted. the count starts from the top down serially and unaffected by sorting.
pls help!thanks a million BaltimoreJohn and especially camickr whose solutions to problems have been most helpful. applying the referenced post, i worked around the problem by resetting the values of the column having the serial number, based on table rowCount every time there is an operation on it(deleting, addition etc)
-
Hai Friends,
i need suggestions for the following,
How to freeze the columns in swing JTable.?????????????
I need to freeze 3 columns in JTable... how it can be done?
eg: its like the option used in excel for freezing the panes. like that, i need to freeze the column in JTable. so that, it wont be scrolled while scrolling the records, it should also be done in colmns of jtable for horizontal scroll n row for vertical scroll. is this can be done in JTable. Can u pls help me out in this...?
Regards,
Ciya.Did you actually try the code which was pointed out to you when you [url http://forum.java.sun.com/thread.jspa?threadID=701198]asked this question before? It does just what you want...
-
To setup scrollbars for individual columns in JTable
Hi,
I need to setup individual scrollbars for the
columns in JTable.
Can anybody help?
Thanks in advance.
Rgds,
sitaHi,
I need to setup individual scrollbars for the
columns in JTable.If i understand you correctly, you want to have a scrollbar for every column. If so, you will need a new instance of JTable and JScrollPane for every column, but that's a lot of work if you want to do it fancy.
The good news, is you can reuse the table model.
You could do something like that.
for (int i= 0; i < model.getColumnCount(); i++) {
JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
//container
someContainer.add(new ScrollPane(table), null/* or some layout constrains*/);
TableColumn tc = new TableColumn(i, 120);
tc.setHeaderValue(model.getColumnName(i));
table.addColumn(tc);
Can anybody help?
Thanks in advance.
Rgds,
sita -
Disable first Column in jTable
Hello people my problem is I use jTable and in the table I need to disable first column so that the user is not able to change edit the values that are listed in this column. Please help me with some code samples if possible. Thanks in advance.
A custom table model makes this simple. You just override the isCellEditable method and return false when the column index is 0.
-
Problem while inserting new Column in JTable
Hi,
I am facing Problem while inserting new Column in JTable.
I have a JTable with one inherited ColumnModel class.
When I am adding the column and moving towards the corresponding location, it moves successfully.
but if I am adding another column or making any changes to table column structure the table retains the defualtcolumn structure.
please help me to solve this..
Regards
Ranjith.........Maybe this example will help:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=579527 -
Hiding a column in jtable made from DefaultTableModel.
I have made my jtable from DefaultTableModel.
I want to keep one column in the jtable as hidden storing some data containing neccessary information like the "path of the file"
which need not be shown to the user.
Please tell me how I can hide one column in the Jtable.
please provide siome link or code for the same.
Tia,
Sarwadayanandabv wrote:
[http://search.sun.com/search/onesearch/index.jsp?qt=hide+column%2B+JTable&rfsubcat=&col=developer-forums]
My thought exactly.
db -
How do I set my side column width & height - Help ?
Please see my website I am building using a Dreamweaver template - it has a spry accordian menu in the right column - I am not skilled enough to understand how to set the height and width of this column Can someone help?
Here is spry css:
* SpryAccordion.css - Revision: Spry Preview Release 1.4 */
/* Copyright (c) 2006. Adobe Systems Incorporated. All rights reserved. */
/* This is the selector for the main Accordion container. For our default style,
* we draw borders on the left, right, and bottom. The top border of the Accordion
* will be rendered by the first AccordionPanelTab which never moves.
* If you want to constrain the width of the Accordion widget, set a width on
* the Accordion container. By default, our accordion expands horizontally to fill
* up available space.
* The name of the class ("Accordion") used in this selector is not necessary
* to make the widget function. You can use any class name you want to style the
* Accordion container.
.Accordion {
overflow: hidden;
width: 210px;
border-bottom: 1px solid #ffffff;
outline: none;
/* This is the selector for the AccordionPanel container which houses the
* panel tab and a panel content area. It doesn't render visually, but we
* make sure that it has zero margin and padding.
* The name of the class ("AccordionPanel") used in this selector is not necessary
* to make the widget function. You can use any class name you want to style an
* accordion panel container.
.AccordionPanel {
margin: 0px;
width: 210px;
padding: 0px;
/* This is the selector for the AccordionPanelTab. This container houses
* the title for the panel. This is also the container that the user clicks
* on to open a specific panel.
* The name of the class ("AccordionPanelTab") used in this selector is not necessary
* to make the widget function. You can use any class name you want to style an
* accordion panel tab container.
.AccordionPanelTab {
color: #ffffff;
margin: 0px;
cursor: pointer;
padding: 5px 10px 5px 30px;
font-weight: normal;
font-family: "Trebuchet MS";
font-size: 12px;
text-transform: uppercase;
-moz-user-select: none;
-khtml-user-select: none;
background-repeat: no-repeat;
background-image: url(../images/accordion_plus_closed.png);
background-position: 10px 7px;
border: 1px solid #ffffff;
border-bottom: none;
/* This is the selector for a Panel's Content area. It's important to note that
* you should never put any padding on the panel's content area if you plan to
* use the Accordions panel animations. Placing a non-zero padding on the content
* area can cause the accordion to abruptly grow in height while the panels animate.
* Anyone who styles an Accordion *MUST* specify a height on the Accordion Panel
* Content container.
* The name of the class ("AccordionPanelContent") used in this selector is not necessary
* to make the widget function. You can use any class name you want to style an
* accordion panel content container.
.AccordionPanelContent {
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
border-left: 1px solid #ffffff;
border-right: 1px solid #ffffff;
overflow: auto;
/* This is an example of how to change the appearance of the panel tab that is
* currently open. The class "AccordionPanelOpen" is programatically added and removed
* from panels as the user clicks on the tabs within the Accordion.
.AccordionPanelOpen .AccordionPanelTab {
color: #ffffff;
background-image: url(../images/accordion_plus_open.png);
border-bottom: none;
/* This is an example of how to change the appearance of the panel tab as the
* mouse hovers over it. The class "AccordionPanelTabHover" is programatically added
* and removed from panel tab containers as the mouse enters and exits the tab container.
.AccordionPanelTabHover {
color: #95c8fc;
/* This is an example of how to change the appearance of all the panel tabs when the
* Accordion has focus. The "AccordionFocused" class is programatically added and removed
* whenever the Accordion gains or loses keyboard focus.
.AccordionFocused .AccordionPanelTab {
/* This is an example of how to change the appearance of the panel tab that is
* currently open when the Accordion has focus.
.AccordionFocused .AccordionPanelOpen .AccordionPanelTab {
/* Custom classes */
.acontent {
padding: 5px 10px 10px 20px;
font-size: 10px;
.acontent p{
font-size: 12px;
color: #ffffff;
.acontent li a { color: #fffff; font-size: 12px; text-decoration: none;}
.acontent li a:visited { color: #0CF; }
.acontent li a:hover { color: #5872f4; } -
Problem with Hiding Columns in JTable
Hi.
Who knows how to hide column in JTable.
I try to get column:
MyTab.getColumnModel().getColumn(i)But there is no methods like SetVisible(false)
What to DO???http://www.google.com/search?hl=en&q=%2Bhide+%2Bcolumn+%2Bjtable&btnG=Google+Search
Maybe you are looking for
-
This may be a basic question... but in InDesign, how does one determine the pixel size of a text box? Specifically, we need to write text to specifications of 600 pixel width, and have no idea a) how to scale a text box to specific pixel width, b) ho
-
Lightroom mobile experience on iPad and iPhone
I've been using LR Mobile since the week it came out and have a few experiences to share--maybe I'm not understanding perfectly.. Last week, I updated my iPad to iOS 8.1.3. When I tasked it to enable offline editing for several collections, the progr
-
Connecting over a network address
I have just purchased a copy of apple remote desktop version 3 as I am going away to university week after next and need to be able to access my home computer to help my parents with any problems that they have. The problem is, I can access their com
-
Calculating discounts on total PO and not excluding unplanned cost
Hi friends, when User does MIRO they enter unplanned frieght cost in the gl account tab. Lets say P.O. is 500 freight is 100 total miro document will be $ 600 . Now when we pay to the vendor it calculates discount on the total amount[600] and not exc
-
Jobq slave wait taking too long
Dear all, On our production database server, I often see certain sessions taking way too long with the wait event "jobq slave wait". how can I prevent "jobq slave wait" events ? (or at least make it shorter or less frequent) I tried to search about t