Summary row in JTable
I have created a JTable with some information. I want to have the last row of the table as a summary row so I want it to stand out. How would I go about putting the text in the last row of the table in bold font?
Thanks for help!
I looked at this example, but I thought that while the structure of the tutorial is good, the code might not be so great. Take for example the number renderer class, I won't repeat it here (might violate copyright) but the setValue method gets a number instance each time it is invoked to render a cell. My understanding is that this is inefficient, as getting an instance is a fairly involved procedure (it involves looking up locale information for instance). Also the code makes heap allocations which could potentially overwhelm the garbage collector (from what I've read).
I'm learning java, and I used the example to make a decimal renderer. The main purpose of this is to show the number for the default locale and the specified number of decimal places:
import java.text.NumberFormat;
import java.text.DecimalFormat;
import javax.swing.table.DefaultTableCellRenderer;
public class DecimalRenderer extends DefaultTableCellRenderer {
int precision = 0; //my addition
Number numberValue; //moved from setValue()
NumberFormat nf; //moved from setValue()
public DecimalRenderer(int p_precision) {
super(); //same as example
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); //same as example
precision = p_precision; //my addition
nf = NumberFormat.getNumberInstance(); // moved from setValue
nf.setMinimumFractionDigits(p_precision); // my addition
nf.setMaximumFractionDigits(p_precision); // my addition
public void setValue(Object value) {
if ((value != null) && (value instanceof Number)) {
numberValue = (Number) value; // re-use the class member, not a local member
value = nf.format(numberValue.doubleValue()); // same as example
super.setValue(value);
} By re-using the number instance and making the variables class level fields as opposed to local members of the setValue function, I expect to avoid the problems I mentioned above.
Have I understood the situation correctly? I guess that experienced people would be aware of this, but I get annoyed by the examples I see as they typically give 'expedient' examples which seldom represent 'best practice'. Often, I can see that something probably isn't best practice, but I don't know what the best practice is.
Similar Messages
-
Hi
I have a jtable and I want to add a summary row right under the header. I added a row and I specified in the sorting class to start from 1 instead of 0, which made my summary not sortable. but, when I scroll down, the summary row disappears.
How can I make it not disappear? the same problem with printing. I'm using the table.print method and it prints only in the first page, while I want to print on every page.
I think I should add it as part of the header? I have no idea how to do that.
any ideas or help?
Thanks,
Benbak
Message was edited by:
BenbakI think you will find something here: http://www.physci.org/codes/tame/
-
In a ADG is it possible to have a pie chart in a summary row?
In a ADG is it possible to have a pie chart in a summary row?
The segments would be worked out from a funtion applied to the data
in that grouping.
Thanks :)The JTable is a tabular component. So it will always have enough columns to display the maximum row length. you don't have to pur a value in every cell.
There are methods in JTable to remove horizontal and vertical lines and you could implement a renderer to make empty cells look like the background.
Cheers
DB -
How can i create a grid with summary row
Hello Professionals,
I'm wondering how could i create a grid like the grid below, i want to create a grid with summary row,
i have tried to create it using collapsing but it didn't work as required.
Any suggestions?, i want to know just the starting point so i can make deep investigations.
Thanks in Advance,Hi Karem,
this can be achieved by just assigning a datatable containing the data plus some formatting of grid. Meaning there is no feature for that.
The datatable can be filled manually or by sql query. Then you have to attach some events for updating the values ( validate after for gid item ).
A small example for a sql query showing last month quotations and orders with summary :
select 1 as Sort,cast(DocNum as varchar) as DocNum,DocTotal,convert(varchar, DocDate,104) from OQUT where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
UNION ALL
Select 2 as Sort,'Summary ( Quotation ) : ',sum(DocTotal), convert(varchar, DATEADD(month, -1, GETDATE()),104)+' - '+convert(varchar, GETDATE(),104) from OQUT where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
UNION ALL
select 3 as Sort,cast(DocNum as varchar) as DocNum,DocTotal,convert(varchar, DocDate,104) from ORDR where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
UNION ALL
Select 4 as Sort,'Summary ( Order ) : ',sum(DocTotal), convert(varchar, DATEADD(month, -1, GETDATE()),104)+' - '+convert(varchar, GETDATE(),104) from ORDR where DocDate between DATEADD(month, -1, GETDATE()) AND GETDATE()
ORDER by Sort
regards,
Maik -
How to set background color of row in JTable
Hi,I want to set different background color to rows in JTable according to some value in the this row.
eg.
no name isGood
1 aaa yes (this row's background color is red)
2 bbb no (this row's background color is blue)
3 ccc yes (this row's background color is red)
4 ddd yes (this row's background color is red)
5 eee no (this row's background color is blue)
thanksthanks!*_*
-
How to set background color in row of JTable ?
i am new in java please tell me about How to set background color in row of JTable ? please example code. Thnak you.
Here is an example: http://www.javaworld.com/javaworld/javaqa/2001-09/03-qa-0928-jtable.html
For more info on how to use tables read the swing tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
ICE -
How to hide row in JTable?
Hi all,
How to hide some specific rows in JTable for user view filtering purpose?
ThanksTry to use the Table Model.
The "getValueAt" Methode decide what to Display.
So a simple "if" command can hide the complete row - or a single Statement.
public Object getValueAt(int row, int col) {
ArrayList al = new ArrayList();
StueliTeil tabellenzeile = (StueliTeil) getDaten().get(row);
switch (col) {
case 0 :
return tabellenzeile.getUmfang();
case 1 :
return tabellenzeile.getTakt();
... -
Inserting row in JTable (runtime)
i need to inserting row in JTable in runtime so tell me what it do
dear farhanaj ,
try this code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
/* <applet code=tableadd.class width=200 height=200>
</applet>
public class tableadd extends JApplet implements ActionListener
Object[] data = new Object[5];
DefaultTableModel defaulttablemodel = new DefaultTableModel();
JTable jtable=new JTable(defaulttablemodel);
JPanel jpanel= new JPanel();
private JPanel topPanel,jpPanel;
public JTable table;
JButton jbutton1 = new JButton("create new row");
JButton jbutton2 = new JButton("create new col");
//JButton jbutton1=new Jbutton("r"),jbutton2=new Jbutton("c");
public tableadd()
for(int column=0 ; column<5 ; column++)
defaulttablemodel.addColumn("column" + column);
for(int row=0 ; row<5;row++)
for(int column=0 ;column<5;column++)
data[column]="cell" row "," + column;
defaulttablemodel.addRow(data);
//getContentPane().add(new JScrollPane(jtable) ,BorderLayout.CENTER);
//getContentPane().add(new JPanel() ,BorderLayout.SOUTH);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jtable , v, h);
getContentPane().add(jsp, BorderLayout.CENTER);
topPanel = new JPanel();
jpPanel=new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel,BorderLayout.NORTH);
getContentPane().add(jpPanel,BorderLayout.SOUTH);
jpPanel.add(jbutton1);
jpPanel.add(jbutton2);
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actionPerformed(ActionEvent e)
if (e.getSource() == jbutton1)
int numberrows = defaulttablemodel.getRowCount();
int numbercolumns=defaulttablemodel.getColumnCount();
Object[] data = new Object[numbercolumns];
for(int column=0 ; column<numbercolumns;column++)
data[column]="cell" numberrows "," +column;
defaulttablemodel.addRow(data);
else
if (e.getSource() ==jbutton2)
int numberrows=defaulttablemodel.getRowCount();
int numbercolumns=defaulttablemodel.getColumnCount();
defaulttablemodel.addColumn("column" + numbercolumns);
for(int row=0; row<numberrows ; row++)
defaulttablemodel.setValueAt("cell" row "," +numbercolumns ,row , numbercolumns);
System.out.println(row);
//jtable.sizeColumnsToFit(0); -
How to set different font for a particular row in jtables?
How to set different font size and font type for a particular row in jtable?
More than enough sample code here:
[http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]
db -
How to select a row in Jtable at runtime
how to select a row in Jtable at runtime.
use
setRowSelectionInterval(int fromRowIndex, int toRowIndex);example if your table has 10 rows then u want to select the rows from 4 to 8 then use
setRowSelectionInterval(3, 7);if you want to select just one row for example 5 then use
setRowSelectionInterval(5, 5); -
How to remove a row from JTable
Hi!
I'm used to remove rows from JTables getting the model and doing a removeRow(num) like this:
((DefaultTableModel)jTable1.getModel()).removeRow(0);
But with ADF and JDeveloper the model says it's a JUTableBinding.JUTableModel but its not accessible.
How to remove a row in Jdeveloper 10.1.3.4.0?Or maybe is just better to refresh data in the jTable but I do not know either like doing it.
-
How to apply different colors to specific rows in JTable
hi,
Anybody could tell me the way of colorising the rows in jtable.. I want to apply different colors to diff rows..
If i get a piece of code that could help me a lot..
thanks in advance,
Sapnayou'll find the answer at http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
-
Using Previous function in summary rows?
Hi,
I have a requirment, where I have to use the value got in previous column in the summary row.
The scenario is as follows.
There are Product, Quantity on Hand , Order Type and Date column. I am using cross tab, As I have to use the details of columns for each date.
So date will be spreaded across table as there are more dates.
In summary column, I would like to do a calcuation for each date. And I should use the calculated amount on one date in the next date and the calcuation continues.
I am trying to use previous function, but its showing computation error.
I am attaching a excel sheet with a sample example for easy understanding.
The calculation which I used in summary row is avialble in formula section, when we select the column.
Thanks in Advance.
Regards
GowthamHi BOCP,
Yes, I am trying to use this function in Summary after Break.
Sorry, I missed out attachment. And I didn't find a way to attach it.
Suresh,
I didn't find last() function in WebI Editor. I am using BO XI R2.
Thanks a lot.
Regards,
Gowtham Sen. -
Tabular form calculated summary row
Hopefully this may be a quick question with either a yes or a no
I have a tabular form with columns
Project Name , Wk1 , Wk2 , Wk3 etc ,
basically so PM's can track the hours against a project
I have used the sum checkboxes to create a summary row
but they also want a row that calculates how many hours remaining for each week under the week no cols , obviously subtracted ffrom the hours budget for each week assigned to the project and person
I've done the summary and calculation in a view but this isn't acceptable as a UNION query in a tabular form
I've also tried another region under the Tabular form but as the project name is a variable length its not easy keeping the Wk cols aligned
thanks in advance
ChrisHi Gus/Paul,
I had the same requirements as you, resulting from the fact that newly added rows may not be visible to the user until he/she has scrolled down sufficiently, but, by lowering my standards (something I excel at :D ), I was able to find an acceptable compromise.
As you probably noticed yourselves, when you "edit" the ADD button, you can see that a call is made to the addRow() javascript function. I therefore took a look at the javascript code - foolish really as I am an oracle DBA from the Jurassic period. Needless to say it scared the pants off me... So, fuelled by cowardice, I snatched at an inferior-but-dead-easy-to-implement alternative solution whereby the page is automatically "scrolled" to the bottom of the form when the "ADD" button is clicked.
Should this "cop out" work for you, you can implement it in the following way:
1. Create a new HTML region immediately after the tabular form, containing the following source: <font color="blue"><a name="bottom_of_page"></a></font>
2. Amend the action when the "ADD" button is clicked to scroll down to the new region by doing the following:
- edit "ADD" button
- Go to "Action when button Clicked" section
- Amend "URL Target" from
<font color="blue">Javascript:addRow();</font>
<br>to
<font color="blue">Javascript:addRow();window.location='#bottom_of_page'</font>
<br>
<br>Regards,
Amr. -
Hello I am new To JAVA SWING
I have 2 questions related to JTable
1) How do i remove selected row in JTable ...for instance, I click on the row to delete (It is selected) , later I press the delete button, and then row is removed .... any hints ?
2) If I would like to save the rows in a JTable into a database , .... How can iterate through the rows in the JTable..each row .... the rows are saved in a List object...and a List Object is what I pass as a parameter in a class method in order to be saved into Database.
Thanks,
deibys1) use the removeRow(...) method of the DefaultTableModel
20 use table.getModel().getValueAt(...) to get the data from the model and create your list Object.
Maybe you are looking for
-
How do I remove my old iPhone from my icloud account
How can I remove my old iPhone from my icloud account , if I go into the App Store on my iPhone 4S , where it says purchased , not on this iPhone I have apps I'd like to delete how do I do this ?
-
Windows 7 - Driver Availability Chart For Creative Products is nearly useless
<span class="text">[url="http://support.creative.com/kb/showarticle.aspx?sid=605">Windows 7 - Driver Availability Chart For Creative Products[/url] is outdated Windows pre-release drivers (at least for the Audigy SB0090 series) back from July. This a
-
Looking for some shortcut's in Oracle SQL developer
1- How to view a table's script. ( without going on the table in schema) 2- Single record view , from the output of select query result. ( without rightclick on the result and selecting single record view....too much cumbersome)
-
How do I improve the GPS connection for my iPhone when I run/cycle? It's beyond finding open sky.. Using RunTracker App and not having luck
-
How do I set up my devices to wirelessly synch my Notes using iCloud among iPad, iPhone 5S and Outlook 2013 running on Windows 8.1?