|
||
|
|||
|
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));
|
|
||||
|
Quote:
|
|
|||
|
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(); } } |
|
|||
|
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. |
|
||||
|
Quote:
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()); } 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 |
|
|||
|
Quote:
Quote:
Quote:
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? |
|
||||
|
Quote:
db |
|
||||
|
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:
Quote:
Quote:
Quote:
Last edited by aussiemcgr; 30-07-2010 at 01:41 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |