Adding Actionlistener to a cell in a JTable?
is this possible? how would i go among doing this?
use the ListSelectionListener.
In "valueChanged(ListSelectionEvent e)" you get the row with "e.getFirstIndex()".
Similar Messages
-
How do I change the colour of text in a cell in a JTable?
I am trying to change the color of the text in a single cell in a JTable. I have tried not adding a cell renderer and it changes the colour of text in all cells.
I have tried adding a cell renderer to the table and it does the same ie all cells text colour changes.
I am able to add a cell renderer to a column and it changes all the cells in the column.
I cannot figure out how to change just one single cell's text colour without effecting the other cells.Ok, so if i create my own cell renderer do I set it as the default renderer for the whole table i.e.
table.setDefaultRenderer(MyCellRenderer, renderer);
Does this set one cell renderer for the whole table or is there an individual one for each cell? -
How to enable JScrollPane in a cell of the JTable
Hi,
I am able to place the components into the cell of the JTable. But I am unable to interact with the components like JTable or JComboBox after inseting them into a cell of the JTable.
This was the one of the Scenario:
Step 1: I created One JTable named as "insertTable"and adding to JScrollPane
Step 2: I'm able to inserted the newly created JTable ("insertTable") in to a cell of another JTable(like table inserting a table) using TableCellRenderer.
here was the problem. I am able to insert the newly created table into the cell. the "insertTable" size is greater the Cell size. So, the "insertTable" is appering with Horizantal and Vertical Scrollbars because the "insertTable" is added to JScrollBar. but I am unable to move the scrollbars.
please any one help me for this.you still didn't try to learn the difference between cellEditor vs cellRenderer - as you were advised to do more than once in recent posts.
If you do, the answer will be obvious (to you :-). As long as you don't there's nothing to help
Cheers
Jeanette -
How can i add a MouseMotionlistener to the cells in the JTable?
hi !
how can i add a MouseMotionlistener to the cells in the JTable?yes i have.but that is different from adding MouseMotionlistener to the cells for me .
i just want get the values where the mouse moves to . -
JComboBox Cell Editor in JTable
I've scouered the forums for an answer to my question, and while
finding other valuable advice, I have yet to find an answer to my
question. But first, a little description:
I have a JTable consisting of 5 columns:
col1= standard Object cell editor
col2= JComboBox cell editor
col3= JComboBox cell editor, values dependent on col2
col4= JComboBox cell editor, values dependent on col3
col5= JComboBox cell editor, values dependent on col4
Data structure looks like this:
col1= company object, containing vector of values for col2
col2= lease object, containing vector of values for col3
col3= well object, containing vector of values for col4
col4= pump object, containing vector of values for col5
col5= simply displayed.
I have a JButton that adds a new row to the table via dialog, then menu
options to add entries to the comboboxes/vectors. The kicker here is
that everything is fine up until I've added a pump, and click the cell
to view the entry. In my cellEditor class, I have a 'getSelected()'
method that returns 'combobox.getSelectedIndex()'. When 'edittingStopped()'
is thrown for any cell in this column, I get a null pointer in my
getSelectedIndex() method of the lease combobox - only in this pump
column. Even the part column works correctly. Code snips:
public class MyApplication ... {
private TableColumn leaseColumn;
private TableColumn wellColumn;
private TableColumn pumpColumn;
private TableColumn partColumn;
private LeaseDropDown leaseDropDown;
private WellDropDown wellDropDown;
private PumpDropDown pumpDropDown;
private PartDropDown partDropDown;
private int currentLease = 0;
private int currentWell = 0;
private int currentPump = 0;
public MyApplication() {
leaseColumn = pumpshopTable.getColumnModel().getColumn(1);
leaseDropDown = new LeaseDropDown(companies);
leaseColumn.setCellEditor(leaseDropDown);
DefaultTableCellRenderer leaseRenderer =
new DefaultTableCellRenderer();
leaseRenderer.setToolTipText("Click for leases");
leaseColumn.setCellRenderer(leaseRenderer);
//... same for lease, well, pump, part ...
leaseDropDown.addCellEditorListener(new CellEditorListener() {
public void editingCanceled(ChangeEvent e) {
} // end editingCanceled method
public void editingStopped(ChangeEvent e) {
updateCells();
} // end editingStopped method
}); // end addCellEditorListener inner class
//.... same inner class for well, pump, part ...
} // end MyApplication constructor
public void updateCells() {
currentLease = leaseDropDown.getSelectedLease();
//... get current well, pump, part ...
leaseDropDown = new LeaseDropDown(companies); // companies=Vector,col1
leaseColumn.setCellEditor(leaseDropDown);
//... same for lease, well, pump and part columns ...
} // end updateCells method
} // end MyApplication class
public class LeaseDropDown extends AbstractCellEditor
implements TableCellEditor {
private Vector companiesVector;
private JComboBox leaseList;
public LeaseDropDown(Vector cVector) {
companiesVector = cVector;
} // end LeaseDropDown constructor
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int rowIndex, int vColIndex) {
Company thisCompany = (Company) companiesVector.get(rowIndex);
Vector leasesVector = (Vector) thisCompany.getLeases();
leaseList = new JComboBox(leasesVector);
return leaseList;
} // end getTableCellEditorComponent method
public Object getCellEditorValue() {
return leaseList.getSelectedItem();
} // end getCellEditorValue method
public int getSelectedLease() {
JOptionPane.showInputDialog("Selected lease is: " +
leaseList.getSelectedIndex());
return leaseList.getSelectedIndex();
} // end getSelectedLease method
} // end LeaseDropDown class... LeaseDropDown can be extrapolated to well, pump, and part,
handing well the selected lease, handing pump the selected
lease and well, handing part the selected lease, well and pump.
I guess my question is how do I get the selected comboboxitem (I'd
settle for the entire combobox if there's no other way) to fill in the
next column? Why does the way I have it now work for the first 2 combobox
columns and not the third?I'll try to provide more details.
I use a JComboBox implementation as a cell in a JTable. The CombBox is editable . This is what I get when I try to type in something.
java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1507)
at java.awt.Component.getLocationOnScreen(Component.java:1481)
at javax.swing.JPopupMenu.show(JPopupMenu.java:921)
at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:177)
at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(BasicComboBoxUI.java:927)
at javax.swing.JComboBox.setPopupVisible(JComboBox.java:790)
at javax.swing.JComboBox.showPopup(JComboBox.java:775)
I read some related bugs on the sun site but I am not sure if this is a bug and if it is then has it been fixed or work-around provided.
any insights ?? -
Getting right end of text displayed in cells of a JTable column
Hey All,
Platform: WinXP, Java 5.0.
I have a simple one-column table whose cells contain pathnames of user-selected files. The pathnames may be quite long, and when they're too long for the fixed width of the column (the containing JFrame is not resizable), I want the trailing (right) end of the pathnames to visible (so the filenames are visible) and for elipsis to be used on the left side (omitting root directory, etc., if need be). I can't figure out how to do this. I've tried many different approaches. Currently I use a class that extends TableCellRenderer, but no combination of alignment settings in this class seems to do the trick.
I've noticed that in the case of JTextFields, this manner of display seems to be the default, no matter what alignment setting I choose. I can't seem to get the desired effect with the display of cells in my JTable though.
Anyone have any suggestions?
Thanks in advance.Great job camickr,
That did just what I wanted; you get the points for sure! I will say that It's surprising to me that Swing leaves so much work in calculation to be done for this simple idea.
Btw, I also want to mention here that there's another forum issue I raised and you responded to recently, and you never quite answered my question there:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5160875
That's got 10 pts riding on it too, if you care to revisit the issue...
Thanks very much for the good work on the CellRenderer. -
How do I change the colour of a selected cell in a jTable?
I have a Jtable that displays URL names in one column. There are several problems I'm having. The effect I'm trying to achieve is this:
When the user runs the mouse over the URL name the cursor should change into a hand (similar to what happens in an HTML hyperlink). I'm aware that the Cursor class can set the cursor graphic so i figure that i need a listener of some sort on each cell (so the cursor can change from an arrow to a hand) and also one to indicate when the cursor is not on a cell (so that it can change from a hand back into an arrow). Is this the right track?
Also, I've looked at the DefaultTableCellRenderer class (which, as i understand it, is responsible for how each cell in the jtable is displayed) for a method that will allow me to set the background of a selected cell (or row or column). I require this because each time i select a cell (or row) it becomes highlighted in blue. I would rather it just remained white and changed the cursor to a hand. I know there exists a method for setting the background for an unselected cell but none for a selected cell. Again, I'm not sure if I'm going down the right track with this approach.
Lastly, if the cell has been selected (by a mouse click) the font of the writing in the cell (i.e. The name of the URL) should change. This shouldn't be too much of a problem I think.
I do not expect anyone to provide code to do all of this but some general pointers would be extremely helpful as I do not know if I'm thinking on the right track for any of this. Having some (limited) experience with Swing I doubt there is a simple way to do this but I can only hope!
Thanks.
Chrishttp://www2.gol.com/users/tame/swing/examples/SwingExamples.html
there you can find some examples with CellRenderer's and so on ...
have fun -
How to get the coordinate of a cell in the jtable?
How to get the coordinate of a cell in the jtable?
How to get the point of a cell in the jtable?
Thanks for help!getCellRect(...);
-
How to insert textfield in a cell of a jtable ?
Hi,
i m not sure how to insert a textfield into a cell of a jtable which already contains some text in the cell .. Can anyone juz show mi a simple program to do this ??? thanks in advance =)Hi Michael,
i get wad u r trying to say ermm actually i did that already. But wad i need to do is such that in a cell, there will b some text displayed (which i have done so) but i also need to add an additional box for user to enter the values n in this case i haf choosen to add the jtextfield box .. But i m not sure how to ... sorry for the misunderstanding ~~~ n thanks for ya reply =) -
Help with editing a cell in a JTable. DESPERATE
Hi! Is there someone out there that could help with some source code for editing a cell in my JTable when I run it as an applet??
It works fine when I run it as an application.
I manage to select the row and write a number in it, but I can't get out of the cell (it seems that the program stops??). I want to enter an integer in a cell and when I click enter or with the mouse in an other cell the value that I enter should update some other cell( multiply the entered value with some fixed number and update a cell in a second coloumn)
I am really desperate now..... I have thought about using a MouseListener....
I am using a tablemodel that is from AbstractTableModel.Are you using some cell editors?
While converting inside them, you might be getting some exceptions (like parseexception)which is stopping you from proceeding further.
Are you using your own tablemodel with custom datatypes?
Hope this helps,
Ramkumar -
Making border of cell invisible in JTable
Hi i want to make border of cell invisible in JTable
any help appreciated
thanks in advanceCreate a renderer with no border - http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender .
-
hello,
I want to add Image related to particular file to cell of the JTable,
I already have name of the file in cell but want to display file or directory name with its related icon.
i am using TableCellRenderer ,but don't know how to keep both filename and icon.
tell if you are aware of it.
thanxThe cell renderer component for the DefaultTableCellRenderer is a JLabel and this has a setIcon() method. You can extend this DefaultTableCellRenderer and inside the getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) method you decide from the parameters which Icon (or ImageIcon) you want to display and then use setIcon() . -
3 jbuttons in a cell in a jtable
hi i have a problem entering 3 buttons into 1 cell i a jtable
can someone give me a good example how i put 3 buttons in the same cell
i have a button render and its working good with 1 button but i just cant find a way to enter 3 buttons in the same cell
tanks in advanceDoesn't sound like a good design, but if you must, have the renderer return a JPanel with the 3 buttons, probably in a GridLayout or BoxLayout.
I suppose you already know that the renderer only draws the components to the table cell, and your rendered buttons will not respond to mouse clicks or keyboard actions.
db -
How to do Multi Cell Selection in JTable
Dear Friends,
Any body know Multi cell selection in jtable......
ThanksTry overriding the changeSelection(....) method of JTable.
public void changeSelection(int row, int column, boolean toggle, boolean extend)
if (d.after(currentDate)
super.changeSelection(row, column, toggle, extend);
} -
Added actionListener, then GUI broke
Hi,
1. first i ran my menu GUI, it worked fine
2. then i added the actionListener code... now she no workie...
what did i do wrong???
ERROR MSG:
C:\jLotto\LotFrame.java:34: cannot resolve symbol
symbol : class ActionListener
location: class LottoFrame
new ActionListener(){
^
1 error
Tool completed with exit code 1
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.Event;
import javax.swing.JOptionPane; //print menu trial run
public class LotFrame extends JFrame
// Constructor
public LotFrame(String title)
setTitle(title); // Set the window title
setDefaultCloseOperation(EXIT_ON_CLOSE); // handle exit operation
setJMenuBar(menuBar); // Add the menu bar to the window
//MAIN MENU
JMenu fileMenu = new JMenu("File"); // Create File menu
JMenu findMenu = new JMenu("Find");
// Construct the file pull down menu
newItem = fileMenu.add("New"); // Add New item
openItem = fileMenu.add("Open"); // Add Open item
closeItem = fileMenu.add("Close"); // Add Close item
fileMenu.addSeparator(); // Add separator
saveItem = fileMenu.add("Save"); // Add Save item
saveAsItem= fileMenu.add("Save As..."); // Add Save As item
fileMenu.addSeparator(); // Add separator
printItem.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent e )
JOptionPane.showMessageDialog( LotFrame.this,
"adding actionListener at end of GUI setup",
"action at end", JOptionPane.PLAIN_MESSAGE);
}//end of actionPerformed
}//end of actionLinstener
);//endof .addActionListener
printItem = fileMenu.add("Print"); // Add Print item
menuBar.add(fileMenu); // Add the file menu
menuBar.add(findMenu);
}//end of constructor
private JMenuBar menuBar = new JMenuBar(); // Window menu bar
// File menu items
private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;
}//end of class LotFramejava.awt.event.*;
import java.awt.Event;
Maybe you are looking for
-
I have rented a movie on apple tv and wish to finish it on my mac book air. When I open iTunes on my mac book air I can not see the movie in iTunes. It is an authorised device and I can see other purchases just. How do I fix it?
-
ADF Controls not working throught Portlet Bridge
Hello, I have a JSF/ADF Faces very simple application, when I expose this application using Portlet Faces Bridge, JSF controls are working but ADF controls are not working. I am using: JDeveloper 10.1.3.3 OC4J 10.1.3.3 Standalone with PDK 10.1.3.2 Po
-
My iphone4 wont connect to my wi fi
I have a iphone 4s. its almost full wont update but also wont connect to my wifi. I have had issues with wifi connection since phone lost presumed stolen turned off then Found returned and turned back on hadnt been able to bluetooth or wifi connect s
-
Add label on y-axis and x-axis in excel using Report Generation toolkit
Hello, I want to add label in x-axis(Date/Time) and y-axis(Temperature O=oC) There are only header and data array connection, how can i add label to the chart? Thank you.
-
Hi, I need to know where are the caluculated values for freight FRC1 is getting stored in the purchase order conditions.