Get JTable to print tabular format - java

So I have a standard JTable and I want the user to be able to print it out. I used JTable.print() and that's fine for printing out an exact replica of the table, but I was hoping to have it more in a tabular format, with just the column names and then the data beneath them, no grids or anything. I thought this would be simple, but I have no clue what to do! Has anybody done this? If so, can someone provide me with code sample/example? Thank you.

You could disable the grid lines, for example...
JTable printTable = new JTable(table.getModel());
printTable.setSize(printTable.getPreferredSize());
JTableHeader tableHeader = printTable.getTableHeader();
tableHeader.setSize(tableHeader.getPreferredSize());
printTable.setShowHorizontalLines(false);
printTable.setShowVerticalLines(false);
printTable.print(JTable.PrintMode.FIT_WIDTH);
This uses a temporary, offscreen JTable to do the actually printing, so you will need to be sure to configure any required renderers, but the idea is sound.
This basic ensures that the JTable that is on the screen doesn't get updated with the changed, which could be kind of freaking to users.
It also allows you to change the TableHeader should you want to to ;)

Related

How would a dev make a JTable LOOK editable

I'm writing an application that has a JTable, and an edit button that sets the current selected row to be editable. Then once the user is done altering the data, they can click the edit button again (with text that now says "Save") to save the data.
The problem is though, when I set a row to be editable, there isn't a visible difference. I could add some code to the renderer to draw the editable cells a little differently, but I don't know what the proper way to make a cell look editable is. Change the color? Make it look like a JTextField? What's the standard method?
Thanks!
Really this is a user interface design question, not a programming one.
To do what you want you need to supply an appropriate cell renderer with the changes you desire but you will need to decide on your own settings. One option might just be to look at the difference between an editable and non-editable text area and apply those to all the cells on the table. This may be as simple as setting the renderers to disabled for any read-only rows.

Can you set the cells of JTable to spill over?

I'm trying to imitate some Excel spreadsheet functionality on my JTable, specifically what I've been calling cell spillover or cell overflow. This occurs when you fill a cell with text with a wider width than the cell is set to. If there is no text in the cell to its right, the words simply keep going into the next cell and beyond.
I feel like this would be accomplished somehow with TableCellRenderer, but I do not know how.
Any help would be much appreciated
I dont know because you did not give alot of detail but a simple use case would be:
When text is entered check for the amount of characters
Check for the width of the cell and look if the text fits
If the text doesnt fit check if the cell next to this one has text
If it has no text, resize the cell to fit the text and make the one next to it smaller.
Or something like that.
But i dont know if JTable allows resizing of individual cells, you should check the JTable API.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html
And i dont know if there is not a better way, so you should consult API documentation and examples.
For example: http://users.csc.calpoly.edu/~jdalbey/305/Lectures/TableCellRendererExamples.html
I hope it helps you.
No, a TableCellRenderer cannot span cells. As alternatives, consider these:
TablePopupEditor, a custom TableCellEditor, can be adapted to serve as a renderer.
A ListSelectionListener, shown here, can be added to an adjacent component in order to display details of the selected row, perhaps using a JScrollPane.

how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application
Rows will be displayed in the Table according to the text which is written in the search textfield.
Now i want to mark that particular text as per the shown in the second image with the yellow color
I know how to select a row or a particular cell.
but I don't know how to select a particular text inside the cell of any row in the table.
I am guessing you know how to search in JTable, so I am not pasting code of it here.
You can look over the SwingX library. It has this kind of function as you said predefined it it. You just need to add it to your table. This is where you can find it. Give it a try you will surely like it.
The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require.
The problem is how to implement it.
I would create a TableCellRenderer based on a JTextField, remove it's border and make it transparent. This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here.
The next problem is then knowing what to highlight. There are a number of possibilities.
You could provide a method in your table model that could return the current text that should be highlighted.
I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text.
Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other

How can I copy a table from Derby DataBase into Swing application applet?

Is there any way of loading complete table from DB into a Swing Cardlayout's Jpanel?
My try was by creating a label for each line, but then things looked very faulty as if I had a column that had more text it was shifting the next column text and whole thing is messy.
Any ideas?
Note: I found that there is something called Jtable, and it might help, but I do not know if I can do that my table cells will be transparent and will reflect a background image?
A JTable will definitely help, but there's a bit of a learning curve. You'll want to learn how to create and populate a TableModel, and then pass that model to an instance of JTable.
If you're trying to build a simple database browsing tool, you might try using SQL Squirrel: http://www.squirrelsql.org

Java Swing JTable sort

I have a JTable with column headers. When I click on a column header the data gets sorted. This is the default sort behavior.
The thing is that I need to remember the last column the user clicked to sort. Anyone knows which listener I need to implement in order to catch the column name that user clicked for sorting on the JTable?
The code is already implemented and I'm new to Swing. I just need to add that extra functionality. So any clues will be helpful.
Thanks in advance.
You can add a MouseListener to the JtableHeader. Then you just use the columnAtPoint(...) method to determine when a column is clicked.
Use Swing-X components, there is a JXTable which is more powerful than JTable.

Categories

Resources