I want to put an action whenever someone clicks a cell. open another gui for example. But how do i make a cell clickable BUT not editable? These are results for an sql query. I can't manage to make the table uneditable though. Do I need a listener or something? and if yes where should I put it?
Here is my code:
public class AllResultsFromDB extends JFrame
{
GUI ins = new GUI();
public AllResultsFromDB(GUI x)
{
Vector columnNames = new Vector();
Vector data = new Vector();
this.ins = x;
try
{
// Initializing GUI class in order to call getSelectedTable() method.
// GUI ins = new GUI();
//System.out.println(ins.getSelectedTable());
Login sgui = new Login();
String dburl = "jdbc:oracle:thin:#localhost:1521:ORCL";
Connection connection = DriverManager.getConnection( dburl, sgui.getUsername(), sgui.getPassword() );
// Fetch data from table specified by user
String query = "SELECT * FROM " + ins.getSelectedTable() + " ORDER BY id";
System.out.println(query);
Statement stmt = connection.createStatement();
ResultSet rset = stmt.executeQuery(query);
ResultSetMetaData metad = rset.getMetaData();
int columns = metad.getColumnCount();
// This loop gets the names of the columns
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( metad.getColumnName(i) );
//columnNames.addElement("PROFILES");
}
// This loop gets the data inside the rows
while (rset.next())
{
Vector row = new Vector(columns);
//Vector b = new Vector((Collection)button);
for (int i = 1; i <= columns; i++)
{
row.addElement( rset.getObject(i) );
}
data.addElement( row );
//data.addElement(b);
}
rset.close();
stmt.close();
connection.close();
// Create table with results
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object obj = getValueAt(row, column);
if (obj != null)
{
return obj.getClass();
}
}
return Object.class;
}
};
JScrollPane scroll = new JScrollPane( table );
getContentPane().add( scroll );
//table.addMouseListener(l);
//table.setEnabled(false);
//table.setDragEnabled(true);
JPanel panel = new JPanel();
getContentPane().add( panel, BorderLayout.SOUTH );
} catch (SQLException e) {
}
}
}
Start by taking a look at How to use tables
The isCellEditable method the TableModel determines of a cell is editable or not. This method should return false
When you supply column/data information to the JTable directly, the JTable creates a DefaultTableModel internally. This class's isCellEditiable method will return true by default.
By using something like DefaultTableModel, you can override this method without with to much trouble and set the model to the table directly.
Next, you need to attach a MouseListener to the table
Take a look at How to write a Mouse Listener
You can then use getSelectedColumn, getSelectedRow to get the selected cell.
You'll also need to use convertRowIndexToModel and convertColumnIndexToModel to convert between the view and model indices
Related
I am trying to show the data of a Table from MySQL server, into a JFrame I am creating. Although everything else works great i cant seem to get correct the TIME data from the corresponding columns.
My code, as well as an example of the output, follows!
package pkginterface;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.time.*;
public class Staff_Info extends JFrame
{
public Staff_Info()
{
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
String url = "jdbc:mysql://localhost:3306/cinema";
String userid = "root";
String password = "password";
String sql = "SELECT * FROM TimeTable";
try (Connection connection = DriverManager.getConnection( url, userid, password );
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql ))
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
JTable table = new JTable(dataVector, columnNamesVector)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
pack();
}
public static void main(String[] args)
{
Staff_Info frame = new Staff_Info();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
And here you can see the above output!
I am working on Ubuntu 14.04 with MySQL Workbench and NetBeans.
Both of the timetable_starttime and timetalbe_endtime fields, have the TIME datatype in their table creation as long as a valid insert field.
Here you can see the MySQL Select * statement for that table.
I figured out that there has something to do with Epoch time but I wasn't able to find an actual solution to the problem!
Any help would be greatly appreciated!
Thanks for your time!
As to this documentation, the call to rs.getObject(i) will return an instance of java.sql.Time for your TIME columns.
java.sql.Time is a subclass of java.util.Date
Unfortunately, JTable does not have a default Renderer in place for java.sql.Time. But is does have one for java.util.Date - which uses a short date format for display - defaulting to showing only the date part, which is all zero for a Time - so it's always Januar 1st 1970
So how to fix this?
Add a custom renderer to your JTable with a specific Renderer for Time:
TableCellRenderer tableCellRenderer = new DefaultTableCellRenderer() {
SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if( value instanceof Time) {
value = f.format(value);
}
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
};
table.setDefaultRenderer(Time.class, tableCellRenderer);
Good Luck.
How i refresh the JTable ?
Here is my Code.
public void itemStateChanged(ItemEvent evt)
{
String text=(String)to_Cmb2.getSelectedItem();
try
{
// Connect to an Access Database
Connection con=DriverManager.getConnection("jdbc:odbc:flightdsn");
Statement s=con.createStatement();
// Read data from a table
ResultSet rs = s.executeQuery("SELECT FlightNo,City,To,Arrives,Departs FROM I_Flights_Routes WHERE To ='"+text+"' ");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.addElement(md.getColumnName(i));
}
// Get row data
while (rs.next())
{
Vector<Object> row = new Vector<>(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
s.close();
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
// Create table with database data
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(50,10,400,200);
td.add(scrollPane);
// td is Jpanel object
td.setVisible(true);
}
You're creating an entirely new JTable -- don't do that. Use the same JTable but
modify the JTable's model if you want to add new data to existing data
or if you want to totally replace the data in the table, create a new DefaultTableModel (or other TableModel) and set your JTable's model with it via `setModel(...).
Also, as an aside you will want to avoid using null layouts and setBounds(...) as this creates very inflexible GUI's that look terrible on other platforms or other resolutions and are very difficult to upgrade and manage.
As mentioned in the header I cannot get my JTable to update with a new row unless I restart the program. When I restart, the new row is there and everything is as it should be. I have tried revalidating/repainting the panel and frame, I have tried the fire methods. I'm at a loss. Thanks in advance
ActionListener (in adminGUI class) for 'Add' button:
if(source.equals(add2)){
String c = itb.getText();
int a = main.getResults();
boolean matches = Pattern.matches("[A-Z][a-z]+", c);
if(matches == true){
main.addGenre(a, c);
}
String Method(in main class) to add a row to the database table:
public static void addGenre(int a, String b){
int rowsAdded;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:MovieDB");
Statement stmt = connect.createStatement();
String query = "INSERT INTO Genres (genre_id, genre_name)" + "VALUES(" + a + ", '" + b + "')";
rowsAdded = stmt.executeUpdate(query);
}catch(Exception exc){}
}
Method(also in main class) to increment the auto-increment-key column:
public static int getResults(){
int a = 0;
ResultSet ints = main.getResults("Select genre_id from Genres");
try {
while(ints.next()){
int d = ints.getInt("genre_id");
if(d>a){
a = d;
}
a++;
}
} catch (SQLException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
return a;
}
JTable details:
ResultSet rs1 = main.getResults("Select * from Genres");
JTable tab1 = new JTable(DbUtils.resultSetToTableModel(rs1));
DbUtils.resultSetToTableModel details :
public class DbUtils {
public static TableModel resultSetToTableModel(ResultSet rs) {
try {
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
Vector columnNames = new Vector();
// Get the column names
for (int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column + 1));
}
// Get all rows.
Vector rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
return new DefaultTableModel(rows, columnNames);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
"I cannot get my JTable to update with a new row unless I restart the program."
I think what you're expecting is that when the database table update, so should your JTable. It doesn't really work like that. You need to update the TableModel, and the JTable will be automatically updated
Since resultSetToTableModel returns a DefuaultTableModel, you can use either of the two methods from DefaultTableModel:
public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
public void addRow(Vector rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.
So when your are adding the data to the database, you also want to update the DefaultTableModel like this
public static void addGenre(Integer a, String b){
...
rowsAdded = stmt.executeUpdate(query);
if (rowsAdded > 0) {
DefaultTableModel model = (DefaultTableModel)tab1.getModel();
model.addRow( new Object[] { a, b });
}
}
Also noticed I changed the method signature to Integer instead of int so it will fit with the Object[] passed to addRow. The int you pass to it will get autoboxed to Integer
SIDE NOTES
Don't swallow you exception by putting nothing in the catch block. Put something meaningful that will notify you of any exceptions that may occur, like
catch(Exception ex) {
ex.printStackTrace();
}
You should also close your Connections, Statements, and ResultSets
You should use PreparedStatement instead of Statement, to avoid SQL injection.
private void resetListData() throws ClassNotFoundException, SQLException
{
Connection cne = null;
try {
Class.forName("org.sqlite.JDBC");
cne = DriverManager.getConnection("jdbc:sqlite:table.sqlite");
cne.setAutoCommit(false);
PreparedStatement psd = (PreparedStatement) cne.prepareStatement("Select * from Genres");
psd.execute();
ResultSet r = psd.getResultSet();
ResultSetMetaData rsmd = r.getMetaData();
int count = rsmd.getColumnCount();
String[] meta = new String[count];
for (int i = 0; i < count; i++)
{
String name = rsmd.getColumnName(i + 1);
meta[i] = name;
//System.out.println(name);
}
model = new DefaultTableModel(new Object[][]{}, new String[]{"name", "address"});
jTable1.setModel(model);
while (r.next())
{
Object[] row = new Object[count];
for (int i = 1; i <= count; ++i)
{
row[i - 1] = r.getString(i); // Or even rs.getObject()
}
model.addRow(row);
}
cne.close();
} catch (ClassNotFoundException | SQLException e) {
}
}
Use this code. so you can insert one row at the end of Jtable without restarting application.,
Thanks..
I create Jtable that store data from the database that I can delete/add contents' the Jtable with Jbutton. When I t try to run the code, I got this result:
When I add data into Jtable twice,there is data in it with an extra blank row
When I delete data into Jtable twice,data in a specific row is deleted but it add a transparent blank row too.
Why does this happen?
Here the code that I think there is a problem:
//add,delete button
final JToggleButton tglbtnAdd = new JToggleButton("Add");
final JToggleButton tglbtnDelete = new JToggleButton("Delete");
JButton button = new JButton("1");
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection dbconbt1 = DriverManager.getConnection("" +"jdbc:sqlserver://localhost;databaseName=Store;user=sa;password=");
Statement sqlstatement = dbconbt1.createStatement();
ResultSet dbresultset1 = sqlstatement.executeQuery("select * from Store.dbo.Product where ProductID = 'P-1'");
ResultSetMetaData rsmetadata = dbresultset1.getMetaData(); // Get metadata on them
int numcols = rsmetadata.getColumnCount(); // How many columns?
// Get column names
for (int i = 1; i <= numcols; i++) {
defaultmodel2.addColumn( rsmetadata.getColumnName(i));
}
if(tglbtnAdd.isSelected() == true) {
while (dbresultset1.next()) {
Vector<Object> row = new Vector<Object>(numcols);
for (int i = 1; i <= numcols; i++) {
row.addElement( dbresultset1.getObject(i) );
}
defaultmodel2.addRow(row );
}
}
if(tglbtnDelete.isSelected() == true) {
/**while (dbresultset1.next())
{
Vector<Object> row = new Vector<Object>(numcols);
row.removeElement( dbresultset1.getObject(0) );
}
*/
defaultmodel2.removeRow(0);
}
// Get row
dbresultset1.close();
sqlstatement.close();
dbconbt1.close();
Make your table structure on form load only, and just take your data from JTextField's (or from whatever you use) into Object array. Finally, add the object array to your model. For delete, just call model.removeRow() method.
I have created 2 JTable in my class,
for the first JTable the data comes from server to arraylist and then to JTable
for the second JTable the data comes from local access database to JTable (no arraylist)
Both JTables will be displayed together when user click a button.
But it takes up to 1min to load and display both jtable on the interface.
Is there any way to load them faster?
Sample code Table1
data = new ArrayList<Person>();
try
{
conn = dc.getConnection();
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while(rs.next())
{
String[] rowData = new String[4];
for(int i=1;i<=4;i++)
{
rowData[i-1] = rs.getString(i);
}
//database to arraylist
data.add(new Person(rowData[0],rowData[1],rowData[2],rowData[3]));
}
String[] colNames = {"LastName","FirstName","Email","Department"};
model = new DefaultTableModel(colNames,data.size())
sorter = new TableRowSorter<TableModel>(model);
int row = 0;
//arraylist to JTable
for(Person p:data)
{
model.setValueAt(p.lastName, row, 0);
model.setValueAt(p.firstName, row, 1);
model.setValueAt(p.email, row, 2);
model.setValueAt(p.dept, row, 3);
row++;
}
table.setModel(model);
table.setRowSorter(sorter);
Sample code of Table2
String[] colNames = {"Name","Email","Department","Status"};
model = new DefaultTableModel(colNames,500);
table.setModel(model);
try
{
conn = ac.getConnection();
stmt = conn.prepareStatement(insert);
rs = stmt.executeQuery();
int row = 0;
while(rs.next())
{
String[] rowData = new String[4];
for(int i=1;i<=4;i++)
{
rowData[i-1] = rs.getString(i);
}
//access database to jTable
model.setValueAt(rowData[0], row, 0);
model.setValueAt(rowData[1], row, 1);
model.setValueAt(rowData[2], row, 2);
model.setValueAt(rowData[3], row, 3);
row++;
}
The delay is caused by the time required to do the database queries. So you want to make sure you are doing two separate queries, each on a different thread so one query is not waiting for the other to finish.
This can be done by creating 2 SwingWorkers, one for the server access and the other for the local access.
you can load data to jtable easily through this
DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
tm.addRow(new Object[] {name,age,tel});
name age tel are values in column1, column2, column3