Results 1 to 9 of 9
  1. #1
    daniel2008 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default 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?

  2. #2
    daniel2008 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    Is it possible that nobody know the answer for my problem?

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

  4. #4
    daniel2008 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    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 10:00 PM.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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...

  6. #6
    daniel2008 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    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...

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by daniel2008 View Post
    Anyway it seems like I get only some useless advices.
    Isn't that being a little harsh on Steve? I thought his advice was pretty far from useless myself and I certainly appreciate the effort he took to trying to help you here.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    > How can I combine this two codes:
    By learning Java.
    The Java™ Tutorials

    db

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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

  1. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 09:55 AM
  2. Regarding JTable
    By adeeb in forum SWT / JFace
    Replies: 0
    Last Post: 06-18-2008, 07:13 PM
  3. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  4. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 01:26 PM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •