Hi guys I'm making a JTable that, in the actual program, takes in music files and displays data about these files (one column displays the file's name, another its size, etc.) where each row contains data about a specific file. Anyway from the start I'd like to have the whole table set as uneditable, which I've done, but then later on in program have it where I could set any one of these cells to be editable.
For instance, when the user double clicks on one of the rows (doesn't matter which cell in that row), that music file will then play. But here's the tricky part that I'm up against: I want to have it where when you right click on one of the cells, and only on a cell in the first column (index 0), to then set that cell to be editable, while keeping all othere cells in the table uneditable. You know how you can double click any editable cell to edit it? Well that's what I'd like to have happen when you click once with the right mouse button to have happen with that particular cell. Then, once the user hits enter or deselects the cell, I'd like to have it set back to being uneditable. I'm guessing I could probably do that final part through tableChanged()? To give you an idea what I'm doing is that when the cell's value, or String value is changed, the program will then rename the song file to be named as the new value.
The column titles it looks like aren't displayed properly in this demo, and I don't know why, but I have it working fine in my real program, so don't worry about this error.
Here's my demo code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class Testing extends JFrame
{
private JTable table;
private DefaultTableModel model;
private JPanel panel;
public static void main(String[] args)
{
Testing test = new Testing();
}
public Testing()
{
model = new DefaultTableModel()
{
public boolean isCellEditable(int row, int column)
{
return false;
/*if (column == 0) I could do this but I only want a specific cell to be set editable AFTER run time
return true;
else
return false;*/
}
};
//Add some columns and rows to the table
for (int i = 0; i < 5; i++)
model.addColumn(i);
for (int i = 0; i < 5; i++)
model.addRow(new Object[] {i+"0",i+"1",i+"2",i+"3",i+"4"});
panel = new JPanel(new GridLayout(1,1));
table = new JTable(model);
//Process events associated with the mouse
table.addMouseListener
(
new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
//Detect right clicks
if (SwingUtilities.isRightMouseButton(e))
{
//This part compiles, but it doesn't do anything
/*model = new DefaultTableModel()
{
public boolean isCellEditable(int row, int column)
{
if ( "this is the row the user clicked on", "this is the column user clicked on")
return true; or return true ONLY for this specific cell, leaving all others uneditable
else
return false;
}
};*/
System.out.println("Right click!");
}
}
//Detect double clicks and then call the method to play the song specified
public void mouseReleased(MouseEvent e)
{
//Make sure that not only the event was a double click but also make sure it wasn't the right mouse button that did the clicking
if (e.getClickCount() >= 2 && !SwingUtilities.isRightMouseButton(e))
{
//Do something else
System.out.println("Double click!");
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
}
);
model.addTableModelListener
(
new TableModelListener()
{
public void tableChanged(TableModelEvent e)
{
//Set cell back to being uneditable here?
}
}
);
panel.add(table);
this.add(panel);
this.pack();
this.setSize(400,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}