I have a JTable of data. I want it so that when a row is selected, an event can occur. With a JList, I would simply add a ListSelectionListener. Any way to do this with a JTable?
You can still use your ListSelectionListener with JTable too (take a look at the JavaDocs of ListSelectionListener: JTable also implements it, so you would use it like for your JList).
See an example (but if you already used it with JList, than you know how to use use it with JTable too).
Related
I have successfully displayed a JTable using an AbstractTableModel, but I want to add delete button for each row in the last column, in the getValueAt method that returns Object there is no way that I can return JButton, JLabel or any JComponent that is clickable. I tried that and I only get the object description toString.
Is there another solution to render JComponent in a JTable without using the TableModel approach?
Is there another solution to render JComponent in JTable without using the TableModel approach?
The TableModel is used to hold the data for the data for the model.
The JTable implements the view of the data for each column. The renderer is just a picture of the data. You can easily render the data to look like a button, however a renderer does not respond to any event.
The JTable does support editors and that is how you interact with real components. When you edit a normal cell a JTextField is placed in the cell location so you can type data into the cell and then the data is save to the model.
So if you want to click on a button then you need to use a button as an editor.
Check out Table Column Button for a class that uses a JButton as the renderer and editor. You then provide the class an Action to be invoked when the button is clicked.
Read the section from the Swing tutorial on Concepts: Renderers and Editor for more information. There is also a section on Using Other Editors.
One way : TableColumn.setCellEditor(jbutton_instance) on a hand added column.
So Suppose I create a JTable with a specific number of columns and rows. Is it possible to add a JPanel to a specific cell after the JTable is already initialized? The JTable would be empty at first and then I would change a cell at row X and column Y to instead contain a JPanel which would contain an amount of ImageIcons. I don't have any code to put here really, I just need to know what I would have to use to accomplish this and how. Thank you.
To change the data in the table you use:
table.setValueAt(...);
This will update the data in the TableModel.
However, adding a JPanel to the table is not the way a table is designed to be used. Typically you add data to the model. Then you use a renderer to display the data in a cell.
Start by reading the section from the Swing tutorial on Using Custom Renderers for more information and examples.
I'm looking for a possibility to show a List with comments in Java.
So I thought I could use the JList and make an own CellRenderer.
The problem is that I want to show the name, the date and the comment in one Item of the list.
How could this be realized with a JList and a CellRenderer? Or do I have to use something else instead of the JList?
It sounds as if you want a JTable rather than a JList. That will give you different columns where you can put your name, date and comment.
I'm sure you could also solve this using a CellRenderer that is a JPanel on which you put whatever you want, but I'd advise you to try out JTable first.
If you use a JLabel as the ListCellRenderer component (like DefaultListCellRenderer) you could prefix the text with "<html>" and format the "layout" of the list cells with HTML.
I have a question with respect to JScrollPane and a JTable.
I have added a JTable to a JScrollPane, and added the JScrollPane to the JPanel. When I click on 'show' button, the JTable will be filled with contents from the database.
I also have another button reset, clicking on which will remove the contents of the JTable, and the JScrollPane. It is supposed to be doing that, but what happens is that, even after clicking the button, all the contents of the JTable and the JScrollPane still exists.
I used revalidate(), reinstantiate(), etc, but of no use. How do I make it work?
Assuming you are using a DefaultTableModel, then you just do:
model.setRowCount(0);
In order to remove a row from a JTable, you need to remove the target row from the underlying TableModel. If, for instance, your TableModel is an instance of DefaultTableModel, you can remove a row by doing the following:
((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);
Updation 2
To know the rows and delete from jtable
int rows = myJTable.getRowCount();
for(int i=0;i<rows;i++)
((DefaultTableModel)myJTable.getModel()).removeRow(i);
I’m working on an in-house app that tracks a bunch of tasks. I wanted to have a simple task monitor that would list the task name and the task’s status. I need this to look just a little nice, I’m no designer so whatever I do is going to suck, but a basic text display won’t work for the project requirements.
What I am essentially attempting to do is show something similar to the Firefox download window, the I-Tunes download window, and well I could name more but they all look basically the same. In each of these apps, each of the ‘progress panels’ is selectable. So to implement this I thought it would be simple to just use a list of JPanels each with a JProgressBar and a JLabel, each of which can just accept focus to determine if it and others are selected. I thought this was going to be an easy task, but if I use a JList it just displays text. I then just figured I would show all the task panels in a larger panel, but I cannot get the inner panels to recognize focus.
Is there a pattern for this? Is there a rolled standard solution that I just have not found? Or is there a better method for doing this? I don’t want to re-invent the wheel, but I thought this was just going to be simple.
It sounds like what you may be looking for is an JList.
You can add your items to the JList's by first adding your "task" to the JList object's ListModel (see the Create a Model section from The Java Tutorials), and then you'll want to assigned a custom ListCellRenderer which will accept your "task" and render on the JList as a JPanel in the list itself. The key here is to make your custom ListCellRenderer be able to display your "task" in the JList the way you want to have it show on the JList.
Take a look into the Writing a Custom Cell Renderer section from the How to Use Lists page of The Java Tutorials. It will describe how to make your custom ListCellRenderer so you can represent your "task" as anything you want.
To keep it short, you will implement the ListCellRenderer interface by implementing the getListCellRendererComponent which will return a Component which is the representation of your task in the JList. You'll probably want to either construct or instantiate your JPanel in this method and return it as the Component.
The standard way of doing this kind of things is to use JTable (or JList) as a container.
You don't have to use default renderes fot table cells, but you can specify your own renderer for specific cells. Take a look at CellRenderer
How about a JTable (which you can set to allow multiple rows to be selected) with an internal JPanel occupying the single cell in each row, which contains a JProgressBar and a JLabel. Or you could use a JList with the same structure as I just described.