Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 29-07-2010, 02:40 PM
Member
 

Join Date: Jul 2010
Posts: 216
Thanks: 3
Thanked 26 Times in 23 Posts
aussiemcgr is on a distinguished road
Default JTable Questions

I have a few questions related to JTables so bear with me and if you could answer what you know, it would be appriecated.

My GUI has a few components, but 2 main ones.
1. A JList of "Chart" Objects. A "Chart" Object contains String Data Range, String Title, String Left Axis Label, String Bottom Axis Label, String Type, and ArrayList<SeriesInfo> list of series. (Those are obviously not their variable names). A "SeriesInfo" Object contains String Name, Point Start, Point End, String DataLabel, String DataRange, Color SeriesColor, and boolean PrimaryAxis.
2. A JTable with 5 columns. The data for those columns are, 1) String, 2) String, 3) CheckBox, 4) Color Picker/Background Colored, 5) String.

Apart from those, there are 3 JToggleButtons and 3 JTextFields.

How this is going to work is that when the user selects a Chart from the JList, it will update the 3 JToggleButtons, 3 JTextFields, and the JTable. The content of the JTable will be the Chart Object's SeriesInfo objects. So when the user selects a Chart from the JList, the JTable will need to dynamically change row count and add new data. The columns will get their data from the following: 1) SeriesInfo.Name, 2) SeriesInfo.DataRange, 3) SeriesInfo.PrimaryAxis, 4) SeriesInfo.SeriesColor, 5) SeriesInfo.DataLabel.

So, now that you are familiar with my design, here are my JTable questions:
1. How can I make the size of the JTable dynamically? The number of the Rows of the JTable will depend on the the length of the SeriesInfo ArrayList of the Chart Object. My current design makes a statically sized JTable.
2. How can I make the boolean CheckBox an actual CheckBox? At first I just had the true/false value, then I played around on Google and got the CheckBox to show instead of the text, but when you click in the CheckBox is just turns back into text and I get a bunch of errors about parsing the String when you try to click out of it.
3. How can I open a Color Picker Pop-up when the user clicks in a cell in the 4th column? I assume I need an ActionListener of some sorts, but I'm not sure where to go with it.

I think thats everything, I will continue to ask questions as I go.

My current JTable code (which I got from playing around on Google and trying things out will need some major overhaul) is this:
java Code:
String[] columnNames = {"Data Name",
                                "Data Range",
                                "Primary Axis",
                                "Color",
                                "Data Label"};

        Object[][] data = {
        {"DataName", "A3:A25",
         new Boolean(false), "Color Picker", "Label"}
        };

        final JTable table = new JTable(data, columnNames);

        table.getColumnModel().getColumn(2).setCellRenderer(new TableCellRenderer()
            {
                public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
                    {
                        boolean marked = (Boolean) value;
                        JCheckBox rendererComponent = new JCheckBox();
                        if (marked)
                        {
                            rendererComponent.setSelected(true);
                        }
                        return rendererComponent;
                    }
            });

        JScrollPane tableScroller = new JScrollPane(table);
        tableScroller.setPreferredSize(new Dimension(400, 70));



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 29-07-2010, 04:57 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 685
Thanks: 8
Thanked 150 Times in 142 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
Default Re: JTable Questions

Quote:
Originally Posted by aussiemcgr View Post

So, now that you are familiar with my design, here are my JTable questions:
1. How can I make the size of the JTable dynamically? The number of the Rows of the JTable will depend on the the length of the SeriesInfo ArrayList of the Chart Object. My current design makes a statically sized JTable.
2. How can I make the boolean CheckBox an actual CheckBox? At first I just had the true/false value, then I played around on Google and got the CheckBox to show instead of the text, but when you click in the CheckBox is just turns back into text and I get a bunch of errors about parsing the String when you try to click out of it.
3. How can I open a Color Picker Pop-up when the user clicks in a cell in the 4th column? I assume I need an ActionListener of some sorts, but I'm not sure where to go with it.
Create a custom TableModel (extend AbstractTableModel or DefaultTableModel) and set the table model to this custom model. Using the custom model will allow you to change the table quite dynamically (see How to Use Tables). Adding a ListSelectionListener to the ListSelectionModel will allow you to listen for selection in the table.
Reply With Quote
  #3 (permalink)  
Old 29-07-2010, 06:29 PM
Member
 

Join Date: Jul 2010
Posts: 216
Thanks: 3
Thanked 26 Times in 23 Posts
aussiemcgr is on a distinguished road
Default Re: JTable Questions

Ok, I could use some help as I go. I have never used TableModels, Table Rendering, or Vectors before.

Vector Question:
How do I go about making 2d Vectors, or is it necessary, or how exactly does this work?

TableModel Question:
In the code from above, it had something to do with Table Rendering. Is that done in the TableModel, or where do I do that?


So based on that Java Tutorials site (in which I think they did a crap job explaining how JTables work), I have attempted to create a makeshift TableModel. How is this looking?:
java Code:
public class TableModel extends DefaultTableModel
    {
        Vector data;
        Vector columns;

        public TableModel(Vector dataV,Vector columnNames)
        {
            super(dataV,columnNames);
            data = dataV;
            columns = columnNames;
        }
        public int getColumnCount()
        {
            return columns.size();
        }
        public int getRowCount()
        {
            return data.size();
        }
        public String getColumnName(int col)
        {
            return columns.get(col).toString();
        }

    }
Reply With Quote
  #4 (permalink)  
Old 29-07-2010, 08:59 PM
Member
 

Join Date: Jul 2010
Posts: 216
Thanks: 3
Thanked 26 Times in 23 Posts
aussiemcgr is on a distinguished road
Default Re: JTable Questions

Ok, so i have currently been able to Dynamically change the number of rows. So that is checked off.

I still need help with:
1. Setting selectable JCheckBox in JTable
2. Opening Color Picker
3. Setting Specific Cell Background Colors (for only the cells with Color Pickers on them).

Any suggestions is apprieciated.
Reply With Quote
  #5 (permalink)  
Old 29-07-2010, 09:40 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 685
Thanks: 8
Thanked 150 Times in 142 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
Default Re: JTable Questions

Quote:
Originally Posted by aussiemcgr View Post
Ok, so i have currently been able to Dynamically change the number of rows. So that is checked off.

I still need help with:
1. Setting selectable JCheckBox in JTable
2. Opening Color Picker
3. Setting Specific Cell Background Colors (for only the cells with Color Pickers on them).

Any suggestions is apprieciated.
Tables aren't extremely intuitive until you start using them quite a bit.
You may wish to change your model TableMode to avoid naming conflictsl, since (indirectly) it is implementing TableModel.

1) By default checkboxes should appear with a boolean, but I've noticed some buggy looking behavior myself when relying on this. I can't recall exactly how I overcame this, but you may want to try returning the column class in your table model
java Code:
@Override public Class getColumnClass(int column){
    if ( column == 0 ){//or whichever column
        return Boolean.class;
   }
   return super.getColumnClass(column);
}

2) You could implement an action listener
java Code:
public void actionPerformed(ActionEvent e){
    int row = table.rowAtPoint(e.getPoint());
     int col = table.colAtPoint(e.getPoint());
}
This will return the row and column where the mouse click occurred. Alternatively you may wish to show a JPopUpMenu containing the Color chooser based upon the point location

3) You could implement the TableCellRenderer interface, and override the getCellRenderer function of JTable, returning the custom renderer when needed, and the default (super) renderer otherwise
Reply With Quote
  #6 (permalink)  
Old 29-07-2010, 09:55 PM
Darryl.Burke's Avatar
Member
 

Join Date: Mar 2010
Location: Madgaon, Goa, India
Posts: 198
Thanks: 4
Thanked 14 Times in 14 Posts
Darryl.Burke is on a distinguished road
Default Re: JTable Questions

ActionEvent doesn't have a getPoint() method. You probably meant to illustrate that with a MouseListener and mousePressed(...) or mouseClicked(...)

db
Reply With Quote
  #7 (permalink)  
Old 29-07-2010, 10:42 PM
copeg's Avatar
Moderator
 
6 Highscores

Join Date: Oct 2009
Posts: 685
Thanks: 8
Thanked 150 Times in 142 Posts
copeg will become famous soon enoughcopeg will become famous soon enough
Default Re: JTable Questions

Quote:
Originally Posted by Darryl.Burke View Post
ActionEvent doesn't have a getPoint() method. You probably meant to illustrate that with a MouseListener and mousePressed(...) or mouseClicked(...)

db
Doh! Thanks for the correction. My brain is running on low today
Reply With Quote
  #8 (permalink)  
Old 30-07-2010, 01:04 AM
Member
 

Join Date: Jul 2010
Posts: 216
Thanks: 3
Thanked 26 Times in 23 Posts
aussiemcgr is on a distinguished road
Default Re: JTable Questions

Quote:
Tables aren't extremely intuitive until you start using them quite a bit.
You may wish to change your model TableMode to avoid naming conflictsl, since (indirectly) it is implementing TableModel.
I actually just used the DefaultTableModel to do what I needed. However I know I'm going to have to create a new Table Model class to get done what I need, and I'm trying to put that off as long as possible.

Quote:
MouseListener and mousePressed(...) or mouseClicked(...)
Ok, so I am not able attach a listener to a specific cell? If possible, some sort of Cell Selection Listener would be best. I am having trouble finding anything like this on Google though.

Quote:
3) You could implement the TableCellRenderer interface, and override the getCellRenderer function of JTable, returning the custom renderer when needed, and the default (super) renderer otherwise
I'll spend some time playing around with Table Rendering tomorrow at work and get back to you as I run into the inevitable problems.



I also have 1 question about the JList thing. I have attached a listener to react when an item is selected, but it seems to run the listener twice when it activates. This was a problem initially because it attempted to update the TableModel when one had not yet been created (which threw NullPointers at me like crazy) but a few if statements made a messy fix for that. Any suggestions regarding adding Listeners to JLists?
Reply With Quote
  #9 (permalink)  
Old 30-07-2010, 06:04 AM
Darryl.Burke's Avatar
Member
 

Join Date: Mar 2010
Location: Madgaon, Goa, India
Posts: 198
Thanks: 4
Thanked 14 Times in 14 Posts
Darryl.Burke is on a distinguished road
Default Re: JTable Questions

Quote:
Originally Posted by aussiemcgr View Post
Ok, so I am not able attach a listener to a specific cell? If possible, some sort of Cell Selection Listener would be best. I am having trouble finding anything like this on Google though.
Check out Rob Camick's Table Cell Listener, it may be what you're looking for.

db
Reply With Quote
  #10 (permalink)  
Old 30-07-2010, 01:38 PM
Member
 

Join Date: Jul 2010
Posts: 216
Thanks: 3
Thanked 26 Times in 23 Posts
aussiemcgr is on a distinguished road
Default Re: JTable Questions

Ok, so it can listen for when I change a value, but it doesnt listen for when the cell is selected.

So I played around with creating a TableColumnModelListener instead:
java Code:
public class TableSelectionListener implements TableColumnModelListener
    {
        public void columnAdded(TableColumnModelEvent e)
        {   }
        public void columnMarginChanged(ChangeEvent e)
        {   }
        public void columnMoved(TableColumnModelEvent e)
        {   }
        public void columnRemoved(TableColumnModelEvent e)
        {   }
        public void columnSelectionChanged(ListSelectionEvent e)
        {
            System.out.println(table.getSelectedRow()+","+table.getSelectedColumn());
        }
    }

And adding it to my table:
java Code:
TableColumnModelListener colListener = new TableSelectionListener();
        table.getColumnModel().addColumnModelListener(colListener);

But there is a problem. Just like with the JList, it runs the listener twice. Except this time, I get two different answers. When I click the top left cell, this prints out:
Quote:
-1,0
0,0
When I click the bottom left cell, this prints out:
Quote:
1,0
1,0
When I click the top right cell, this prints out:
Quote:
1,4
0,4
When I click the bottom right cell, this prints out:
Quote:
1,4
1,4
It does print out the correct answer for the bottom cells, except it does it twice. But I dont know what is going on for the top cells. Lastly, it doesnt appear to be reliable. Sometimes it responds, sometimes it doesnt.

Last edited by aussiemcgr; 30-07-2010 at 01:41 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
A few questions adenverd Java Theory & Questions 3 26-05-2010 08:34 AM
[SOLVED] Some serious questions, Time What's Wrong With My Code? 3 17-05-2010 07:52 AM
[SOLVED] Questions About Threads neo_2010 Threads 4 15-03-2010 01:04 PM
Java Questions! :) xs4rdx Java Theory & Questions 0 21-02-2010 12:40 PM
Some basic questions. trips Java Theory & Questions 5 21-07-2009 07:15 AM


100 most searched terms
Search Cloud
2359-unable-move action listener in java actionlistener actionlistener in java addactionlistener addactionlister cannot find symbol method create an abstract class called shape with abstract methods+java double to integer double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space format double java get files from folder java by threads get mouse position java how to convert list to map in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java cos java deallocate java double format java double to int java font attributes java format double java forum java forums java get mouse position java heap size exception java jbutton java nextline() java program to find dimensions of a room java programming codes using astirisks java programming forum java programming forums java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space jbutton actionlistener jbutton with key enter jtable questions in java jtext bold jtextarea font jxl.read.biff.biffexception: unable to recognize ole stream mean value decimal double java programmer forum transaction using gui and 2 dimensional array code in java two dimensional arraylist in java typecast in java

All times are GMT. The time now is 09:37 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.