tablemodel

Create JTable from ArrayList of Objects - Java

自古美人都是妖i 提交于 2019-11-29 12:19:23
How do I display my "Click" objects in a JTable? ArrayList<Click> myClicks = new ArrayList<Click>(); Click click = new Click(620, 1028); Click click2 = new Click(480, 230); myClicks.add(click); myClicks.add(click2); It should look something like this: |---Mouse X---|---Mouse Y---| | 620 | 1028 | | 480 | 230 | Which is really... | | v v click.getX() click.getY() click2.getX() click2.getY() I don't want to use a 2D Object[][] because it makes more sense to keep them as Click objects as long as possible. I know I probably have to extend the TableModel interface but I'm not sure how. I'd just like

Making JTable cells uneditable

为君一笑 提交于 2019-11-29 11:29:16
I am trying to make all the cells of a JTable uneditable when double clicked by the user. I have read a lot of forum posts and the general consensus is to create a new table model class, extend DefaultTableModel and then override method isCellEditable(int row, int column). I did all of this and now when I run my program (applet) Nothing shows up in the cells. NOTE I have a prof this semester that does not think applets are outdated... Code for the table model: public class MyTableModel extends DefaultTableModel { public boolean isCellEditable(int row, int column) //override isCellEditable /

How to set a custom object in a JTable row

我与影子孤独终老i 提交于 2019-11-29 10:39:09
I should tell this first, this is NOT about Rendering a Table cell. Here is the TableModel that i'm building using a 2D array based on a User object in my DB. List<User> userList = userManagerService.getAllUsers(); /* String[] col_user = {"Username", "Name", "Phone", .... } */ String[][] data = new String[userList.size()][col_user.length]; int i = 0; for (User user : userList) { String[] userdata = new String[col_user.length]; userdata[0] = user.getUserUsername(); userdata[1] = user.getUserName(); userdata[2] = user.getUserPhone(); userdata[3] = user.getUserNic(); userdata[4] = user

What is the best way to listen for changes in JTable cell values and update database accordingly?

独自空忆成欢 提交于 2019-11-29 06:46:45
I'm building and app with multiple JTables and I need to detect when cell value change occurs so I can update it in the database. I tried TableModelListener and overriding tableChanged , but it fires only when I click away (click on another row) after I have edited a cell. Any other way to do this? You can implement the CellEditorListener interface, as shown in this example . Note that JTable itself is a CellEditorListener . It may also be convenient to terminate the edit when focus is lost, as shown here : table.putClientProperty("terminateEditOnFocusLost", true); More Swing client properties

Swing: catching exceptions from TableModel

ε祈祈猫儿з 提交于 2019-11-28 14:43:05
I have a TableModel that may throw an exception on its setValueAt method if user enters an invalid value: public class MyTableModel extends AbstractTableModel { public void setValueAt(Object value, int rowIndex, int columnIndex) { String valueStr = (String) value; // some basic failure state if(valueStr.length()>5) { throw new ValidationException("Value should have up to 5 characters"); } this.currentValue = valueStr; } } Question is: how can another class catch this exception? It may show a popup message, or update a status bar, or paint the cell red. Whatever I chose to do, I don't think the

Create JTable from ArrayList of Objects - Java

核能气质少年 提交于 2019-11-28 06:17:21
问题 How do I display my "Click" objects in a JTable? ArrayList<Click> myClicks = new ArrayList<Click>(); Click click = new Click(620, 1028); Click click2 = new Click(480, 230); myClicks.add(click); myClicks.add(click2); It should look something like this: |---Mouse X---|---Mouse Y---| | 620 | 1028 | | 480 | 230 | Which is really... | | v v click.getX() click.getY() click2.getX() click2.getY() I don't want to use a 2D Object[][] because it makes more sense to keep them as Click objects as long as

Making JTable cells uneditable

孤街醉人 提交于 2019-11-28 05:21:20
问题 I am trying to make all the cells of a JTable uneditable when double clicked by the user. I have read a lot of forum posts and the general consensus is to create a new table model class, extend DefaultTableModel and then override method isCellEditable(int row, int column). I did all of this and now when I run my program (applet) Nothing shows up in the cells. NOTE I have a prof this semester that does not think applets are outdated... Code for the table model: public class MyTableModel

Mass produce JTables

本小妞迷上赌 提交于 2019-11-28 02:27:39
I want to make 25 JTables. I generate the table names by doing: for(int i=0; i < 26; i++) { TableNames[i] = "Table" + i + ""; ... How can I go about using these String names in the array as the new JTable names? i.e. TableNames[i] = new JTable(model){ ... Instead of an array, consider using a List<JTable> or List<TableModel> . Pass the name to the table's constructor or factory method. The example below uses the Component 's name, but a JComponent 's client property may be more versatile. Update : The revised Java 8 example below illustrates how to add new tables dynamically. import java.awt

What is the best way to listen for changes in JTable cell values and update database accordingly?

烂漫一生 提交于 2019-11-28 00:01:47
问题 I'm building and app with multiple JTables and I need to detect when cell value change occurs so I can update it in the database. I tried TableModelListener and overriding tableChanged , but it fires only when I click away (click on another row) after I have edited a cell. Any other way to do this? 回答1: You can implement the CellEditorListener interface, as shown in this example. Note that JTable itself is a CellEditorListener . It may also be convenient to terminate the edit when focus is

How to add checkbox in Jtable populated using rs2xml

假如想象 提交于 2019-11-27 08:29:27
问题 I don't know how to go about i have this frame i wish to populate a JTable and add a checkbox. public static void update_table() { try { String sql="SELECT * FROM equipments"; PreparedStatement update = con.prepareStatement(sql); ResultSet result = update.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(result)); table.setVisible(true); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 回答1: I assume you mean to want to add an additional column