I am currently using FileWriter to write data into .txt tile.
Once I double click the row in the table, it links to another jFrame.
I wanted to set a button at the particular jFrame so that it is able to delete the row in the table. How can I perform the action?
Use the MVC (Model-View-Controller) paradigm.
Have a controller class, maybe containing the main method.
That holds the views (JFrame) and the data models (i.e. a DefaultTableModel).
That table model is passed to the table, which actually is also a listener to changes of the table model.
On button press let the button tell the controller that a row should be deleted.
This is done on the table model, and change events are fired. Automatically when using a DefaultTableModel. Or do it manually when using an AbstractTableModel.
MVC is not necessarily more direct, but there are not calls from one component to another components littered through the sources. It decouples things.
This can be done by adding a New button. Go into the codding of the button and add a simple sql statement saying to drop a row from the table And the another statement to show the table again by Select * from tablename;
Related
I created a custom table, with a custom model, a custom cell renderer and a custom cell editor. Every cell of this table is a panel which contains another custom table.
The problem is: (with or without the internal table) to interact with the cell, thus to trigger the event listener of the components in that cell(which is a panel), you need to click the table before.
I imagine it wants you to "enter the table" or select the cell before you can actually interact with its components but this is not functional.
Is there a way to avoid this issue?
A JTable displays data from an array of objects. Object data changes in the background and the table is updated. When rows are selected and the table data changes the row selections are lost. It is difficult for the user to select rows for an action when the selections are frequently lost.
Is there a way to stop the deselection?
Here's what I found using the debugger. The row selection is being cleared during the call to fireTableDataChanged, which I was using to apply the updates in the table.
Apparently that method redraws the table and the selection may not be valid (as suggested by the MadProgrammer comment above). I will still be needing that feature when objects (rows) are be added or removed.
So what I must do is find a way to notify the table model to make updates to without clearing the selection. I have chosen for now to use fireTableRowsUpdated when updates are to be applied that do not require restructuring the table and fireTableDataChanged otherwise.
So the code behind it is quite massive.
I have JTabbedPane with a Tab for each table(inside JSCrollPane ) and a Summary table which contains tables that uses the same models as models in the tabs. So when I add rows to the table everything renders properly. When one of the tables has its data changed everything renders OK , except the JButtons (the button in the first row always renders) . Clicking on the line fixes it , even if it changes again the button does not disappear.
When data changes I only fireDataChanged() for the model of contained tables, the model does fire repaint on the contained tables and the container table.
Each swing component can be in only one container. The problem comes if you try to put into two Jpanels, if you do that with 2 Jtables that use the same instance of a model ,what happens is originally everything renders properly , but will break as soon as you activate the component(button). It will stop rendering in the table where you pressed it , in some cases both.
Creating front-end (Java) user interactive table using MySQL as back-end
Now im trying to create user interactive form.
Untill now, I have created a form view, using swing components, JLable, JTextfield where user enters data. JButtons, 'new' 'save' 'delete' 'edit', which listens user action through ActionListner.
A table that 'extends' AbstractTableModel. Table values are the ResultSet values. i have used MySQL connectivity.
Table is displayed. User can now add new row to it, using 'New' and 'Save' button.
My problem is, i need the corresponding TextFields to display the corresponding row user selects in the displayed table, so that 'edit' and 'delete' would be user interactive. users are allowed to select table's row. Need Help.
Im sorry if any mistake in my question format and language. Thank you!
i need code for creating table ...
ple help me on tis....
i need code for creating table
How much will you pay? :)
..i need the corresponding TextFields to display the corresponding row user selects in the displayed table..
For that you can use,
Give a button, like EDIT and in the event handler for EDIT use something like,
jTextField.setText( jTable.getValueAt(
jTable.getSelectedRow(),
jTable.getSelectedColumn())
.toString()
);
2.As an alternative you can also utilize the event in jtable like mouseClicked with the above code
Now regarding the edit and delete , you may utilize the focusLost event of the textfield or use a EDIT or DELETE button, and carryout the updation, which will be much reasonable option.
Additionaly have a look at this : Using JDBC with GUI API
Does anyone know of a JTable based Swing component OR code example that can save to a file? i.e: provides a menu item or button that when clicked on prompts the user for a file location and saves the table's contents to a file (CSV, XLS, TXT, or whatever).
The easy part is looping through the rows and saving to a file. But there also needs to be a UI component ON the table itself that allows the user to initiate the save.
Write your own. All you do is use the table.getModel().getValueAt(...) method and loop through the rows and columns and then write the data to a file.
I have implemented this using the following approach:
Create an Action implementation: DelimitedExportAction. I typically add this action to a JToolBar, JMenuBar or JPopupMenu.
Expose a method void registerTable(JTable tbl). Internally this method adds a FocusListener to the JTable. When a given JTable gains focus set the tableToExport instance variable and enable the action.
When the actionPerformed(ActionEvent) method is called, invoke your export logic if tableToExport != null.
Rather than iterate over the TableModel I recommend iterating over the JTable, and for each row calling getValueAt(int, int) on the underlying TableModel, remembering to convert between view and model row / column indices. This is more intuitive to the end user as it means any sorting and filtering applied to the JTable is preserved during the export. (In my implementation I actually offer users the choice of exporting all data or visible data.)
Define a DelimitedExportFormatter whose role is to convert each object returned by getValueAt(int, int) to a String. Similar to when providing renderers to a JTable this class should permit you to provide a Format for a given Class or specific column, whereby a column specific format takes precedence. The default format applied to all other values should be: value == null ? "" : value.toString().
I allow the action to be configured in different modes (which also governs the action's icon):
Export to file: Launches a file chooser dialog allowing user to specify save destination.
Export to Excel: Saves the exported data as a temporary file and opens it within Excel.
Configurable export: Launches a dialog whereby the user can specify: row delimiter, field delimiter, columns to export, rows to export (all, visible, selected), save destination (Excel / File).
I'm afraid I can't provide the code as it is proprietary but hopefully this will put you on the right track. Good luck!
I recently created a very simple tutorial that exports data from JTable into excel file, using Tab-Separated Values(TSV) format. The application provides an Export button, which then triggers a dialog box (JFileChooser) to assist the user in specifying the file location/destination. I hope this helps somehow.
https://sites.google.com/site/teachmemrxymon/java/export-records-from-jtable-to-ms-excel
I don't know of any JTable-like Swing component that fulfills this exact need.
However, where would you expect the button to be placed on the table? In my opinion you would be better served by either adding the JTable to a JScrollPane and putting your "save" button on the JScrollPane or adding the JScrollPane to a JPanel and putting the "save" button on the JPanel. I don't see the logic behind having a button on the JTable itself.
If you want a menu item, you'd probably want to create the menubar and add the JTable to whatever container is holding the menubar. There's still no adding of a button to the table itself, mind you, but it would be the same thing visually.