Results 1 to 9 of 9
Thread: Help with JTable rendering
- 01-04-2009, 08:23 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Help with JTable rendering
The problem is simple:
How can I combine this two codes:
1.
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
public class CTest
{
public static void main(String args[])
{
JFrame jf=new JFrame("Table with cell spanning");
CMap m=new CMap1();
TableModel tm=new DefaultTableModel(15,20);
jf.getContentPane().add(new JScrollPane(new CTable(m,tm)));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.show();
}
}
///////////////////////////////////////
interface CMap
{
/**
* @param row logical cell row
* @param column logical cell column
* @return number of columns spanned a cell
*/
int span (int row, int column);
/**
* @param row logical cell row
* @param column logical cell column
* @return the index of a visible cell covering a logical cell
*/
int visibleCell(int row, int column);
}
////////////////////////////////////////
class CTable extends JTable {
public CMap map;
public CTable(CMap cmp, TableModel tbl) {
super(tbl);
map=cmp;
setUI(new CTUI());//
setValueAt(" Daniel",3,3);
setValueAt("l",5,5);
}
public Rectangle getCellRect(int row, int column, boolean includeSpacing){
// required because getCellRect is used in JTable constructor
if (map==null) return super.getCellRect(row,column, includeSpacing);
// add widths of all spanned logical cells
int sk=map.visibleCell(row,column);
Rectangle r1=super.getCellRect(row,sk,includeSpacing);
if (map.span(row,sk)!=1)
for (int i=1; i<map.span(row,sk); i++){
r1.width+=getColumnModel().getColumn(sk+i).getWidt h();
}
return r1;
}
public int columnAtPoint(Point p) {
int x=super.columnAtPoint(p);
// -1 is returned by columnAtPoint if the point is not in the table
if (x<0) return x;
int y=super.rowAtPoint(p);
return map.visibleCell(y,x);
}
}
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
class CMap1 implements CMap
{
public int span(int row, int column) {
if ((row==3) &&(column==3)) return 6;
if ((row==4)&& (column==7)) return 2;
if ((row==9)&&(column==5)) return 3;
return 1;
}
public int visibleCell(int row, int column) {
if ((row==3)&& (column>=3)&&(column<9))
return 3;
if ((row==4)&&(column>=7)&&(column <9))
return 7;
if ((row==9)&&(column>=5)&&(column<8))
return 5;
return column;
}
}
////////////////////////////////////////////////////
class CTUI extends BasicTableUI
{
public void paint(Graphics g, JComponent c) {
Rectangle r=g.getClipBounds();
int firstRow=table.rowAtPoint(new Point(0,r.y));
int lastRow=table.rowAtPoint(new Point(0,r.y+r.height));
// -1 is a flag that the ending point is outside the table
if (lastRow<0)
lastRow=table.getRowCount()-1;
for (int i=firstRow; i<=lastRow; i++)
paintRow(i,g);
}
private void paintRow(int row, Graphics g)
{
Rectangle r=g.getClipBounds();
for (int i=0; i<table.getColumnCount();i++)
{
Rectangle r1=table.getCellRect(row,i,true);
if (r1.intersects(r)) // at least a part is visible
{
int sk=((CTable)table).map.visibleCell(1,1);
paintCell(row,sk,g,r1);
// increment the column counter
i+=((CTable)table).map.span(row,sk)-1;
}
}
}
private void paintCell(int row, int column, Graphics g,Rectangle area)
{
int verticalMargin = table.getRowMargin();
int horizontalMargin = table.getColumnModel().getColumnMargin();
Color c = g.getColor();
g.setColor(table.getGridColor());
g.drawRect(area.x,area.y,area.width-1,area.height-1);
g.setColor(c);
area.setBounds(area.x + horizontalMargin/2,
area.y + verticalMargin/2,
area.width - horizontalMargin,
area.height - verticalMargin);
if (table.isEditing() && table.getEditingRow()==row &&
table.getEditingColumn()==column)
{
Component component = table.getEditorComponent();
component.setBounds(area);
component.validate();
}
else
{
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(renderer, row, column);
if (component.getParent() == null)
rendererPane.add(component);
rendererPane.paintComponent(g, component, table, area.x, area.y,
area.width, area.height, true);
}
}
}
AND
2.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
/**
* @author romi
*
*/
public class Table {
JTable table;
boolean selectDeselect = false;
/**
* @param args
*/
public static void main(String[] args) {
new Table();
}
public Table() {
JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");
String data[][] = {{"xxx","xxx","1"},
{"xxx","xxx","2"},
{"xxx","xxx","3"},
{"xxx","xxx","4"}};
String col [] = {"Col1","Col2","Col3"};
DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model);
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_I NTERVAL_SELECTION);
final CustomTableCellRenderer renderer = new CustomTableCellRenderer(table);
table.setDefaultRenderer(Object.class, renderer);
JScrollPane pane = new JScrollPane(table);
frame.add(pane, BorderLayout.CENTER);
JButton button = new JButton("Green");
JButton buton2 = new JButton("Blue");
JButton buton3 = new JButton("Yellow");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int rowIndexStart = table.getSelectedRow();
int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
int colIndexStart = table.getSelectedColumn();
int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxS electionIndex();
//Check each cell in the range
for (int i = rowIndexStart; i <= rowIndexEnd; i++) {
for (int j = colIndexStart; j <= colIndexEnd; j++) {
if (table.isCellSelected(i, j)) {
// cell is selected
renderer.isOtherColor[i][j] = !renderer.isOtherColor[i][j];
}
}
}
renderer.shit1 = true;
}
});
buton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int rowIndexStart = table.getSelectedRow();
int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
int colIndexStart = table.getSelectedColumn();
int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxS electionIndex();
//Check each cell in the range
for (int i = rowIndexStart; i <= rowIndexEnd; i++) {
for (int j = colIndexStart; j <= colIndexEnd; j++) {
if (table.isCellSelected(i, j)) {
// cell is selected
renderer.isBlueColor[i][j] = !renderer.isBlueColor[i][j];
}
}
}
renderer.shit2 = true;
}
});
buton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int rowIndexStart = table.getSelectedRow();
int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
int colIndexStart = table.getSelectedColumn();
int colIndexEnd = table.getColumnModel().getSelectionModel().getMaxS electionIndex();
//Check each cell in the range
for (int i = rowIndexStart; i <= rowIndexEnd; i++) {
for (int j = colIndexStart; j <= colIndexEnd; j++) {
if (table.isCellSelected(i, j)) {
// cell is selected
renderer.isBlueColor[i][j] = !renderer.isBlueColor[i][j];
}
}
}
}
});
JPanel p = new JPanel();
p.add(button);
p.add(buton2);
frame.add(p, BorderLayout.SOUTH);
frame.setSize(500,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
boolean[][] isOtherColor = null;
boolean shit1 = false;
boolean shit2 = false;
boolean[][] isBlueColor = null;
boolean[][] isGreenColor = null;
public CustomTableCellRenderer(JTable table) {
int linii = table.getModel().getRowCount();
int coloane = table.getModel().getColumnCount();
isOtherColor = new boolean[linii][coloane];
isBlueColor = new boolean[linii][coloane];
for (int i = 0; i < linii; i++) {
for (int j = 0; j < coloane; j++) {
isOtherColor[i][j] = false;
}
}
for (int i = 0; i < linii; i++) {
for (int j = 0; j < coloane; j++) {
isBlueColor[i][j] = false;
}
}
}
public Component getTableCellRendererComponent (JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
if (isSelected) {
} else {
if (isOtherColor[row][column]) {
//set background marker
setBackground(Color.green);
if(table.getBackground()==Color.blue)
setBackground(Color.green);
}else
if (isBlueColor[row][column]) {
//set background marker
setBackground(Color.blue);
if(table.getBackground()==Color.green)
setBackground(Color.blue);
} else{
//reset background
setBackground(table.getBackground());
}
}
return cell;
}
}
}
........ So I can have both: spanned cells and changing background of the selected cells color by pushing a button?
- 01-05-2009, 05:49 PM #2
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Is it possible that nobody know the answer for my problem?
- 01-05-2009, 08:32 PM #3
That's a lot of code... I have a couple of thoughts. Spanned cells: look at setCellSelectionEnabled for allowing selection of ranges of cells. Selected cell color: setSelectionBackground. Beyond that, you may want to look at simplifying your design.
- 01-07-2009, 08:53 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Any solution it's ok in the first code it is used BasicTableUI which is defenitory for spanning columns and TableCellRender interface which as we all know is different than the second code becouse this one extends DefaultTableCellRender which is a class and has the setBackground() method implemented in the class. Mixing both codes we will have 2 render propreties for the Jtable class:
TableCellRender for "CTest" (TableCellRenderer renderer = table.getCellRenderer(row, column);) AND
DefaultTableCellRender for "Table" (final CustomTableCellRenderer renderer = new CustomTableCellRenderer(table);)
I realy don't know how to do it.
I've watched the above link code so far and try to understand it, hopping it might be usefull, but it doesen't compile unfortunately:
java2s.com/Code/Java/Swing-Components/MixedTableExample.htm]Mixed Table Example : Grid Table*«*Swing Components*«*Java[/url]
The problem seems to be here:
302 dataVector = new Vector(0);
303 setColumnIdentifiers(columnNames);
Any way the code is more complex than what I want.
I ask again. Can someboy help me please?Last edited by daniel2008; 01-07-2009 at 09:00 PM.
- 01-08-2009, 01:09 AM #5
First, start with an empty project and make a prototype.
Create a JTable.
Implement TableModel to hold the data you need. The big thing here is that you can store the values *any* way you want. I suggest avoiding arrays. You can make each column something like an ArrayList, with entries representing rows. Override each method. If you start at the top and work down, you will be done fairly quickly, and you will understand what the JTable is doing more completely.
Implement TableCellRenderer. That is really easy, because it only has one method. Have it return some sort of Component, such as a JLabel where you set its preferred size. You can then easily control the color, and you can display text as well.
JTable keeps track of which cells are selected, and it will tell your TableCellRenderer the status of each cell when it asks for each component.
At this point, look at the API and the tutorial. Start with something simple and fuss with it until it works, then add another feature.
Last thing. Java in general and Swing in particular are industrial strength. What I mean is, you can do anything except write an operating system (quiet, C++ people!), but you need to know a lot to do even fairly simple things. Once you know, then the simple things are fast and easy. But you have to learn first. Sorry for the negativity...
- 01-17-2009, 07:41 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Thanks Steve, but your advice is my limitation in what I want to do. Many good java programers said that it's hard to do what I want becouse this implies a lot of codding skills so it's not worth it, becouse they have to spend a lot of time for something they don't need to. So this is a challenge, or not.... Anyway it seems like I get only some useless advices.
Thanks anyway...
-
- 01-18-2009, 12:44 AM #8
> How can I combine this two codes:
By learning Java.
The Java™ Tutorials
db
- 01-18-2009, 03:51 AM #9
I understand your frustration, but advice indicating that something can't easily be done can save you from wasting a great deal of time and effort. That's hardly worthless.
Frankly, I don't recall ever seeing a striped combo box, in any environment. Given that stripes are usually used to help the eye move across columns of data, stripes don't seem to useful in a combo box. The best solution might be to simply drop the requirement...
Similar Threads
-
JLabel Rendering
By random4534 in forum New To JavaReplies: 3Last Post: 12-16-2008, 08:55 AM -
Regarding JTable
By adeeb in forum SWT / JFaceReplies: 0Last Post: 06-18-2008, 06:13 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
How to add in a new row in Jtable?
By Ry4n in forum AWT / SwingReplies: 0Last Post: 01-18-2008, 12:26 PM -
Help with JTable
By fernando in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 06:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks