• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HyperLink but not URL!!

 
Prashanth Bhanu
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

In my application i want to have a Jtable where i have set of swing component(lets say one Jtable column).When user clicks on that it should perform some acction like one for "save",One for "confirm",one for "Delete".When i click on these appropriate action should be taken
I should achive it without using JButton.If not JButton how do i capture the action??.If i use JLabel, appearence will be mild but cannot have "action event " on JLables..Please suggest as idea..
Thanks in advance
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, technically you could have an action event on a label but then it would be a button. A button isn't really that different from a JLabel aside from the events attached to the button.

If I were you, I would just use a JButton.

Remove the border: setBorderPainted(false);
Remove focus box: setFocusPainted(false);

After that you're left with pretty much a clickable label.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, if you really wanted a JLabel, just attach a MouseListener and on the mouseClicked event, do your action.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within the context of a column of a JTable, you will not be able to use any Component's (such as JButton) built-in ActionEvent handling. That is because JTable cells are drawn with TableCellRenderers and don't actually exist as Components. You can configure a renderer so that a table cell looks like a JButton (or some other component) but you have to handle user clicks yourself.

There are two ways to do that. You can listen for MouseEvents on the JTable and, when you get one, compute which table cell was clicked on. Or you can customize a TableCellEditor.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:
Within the context of a column of a JTable, you will not be able to use any Component's (such as JButton) built-in ActionEvent handling. That is because JTable cells are drawn with TableCellRenderers and don't actually exist as Components. You can configure a renderer so that a table cell looks like a JButton (or some other component) but you have to handle user clicks yourself.

There are two ways to do that. You can listen for MouseEvents on the JTable and, when you get one, compute which table cell was clicked on. Or you can customize a TableCellEditor.


You can use a JButton in a cell if you use a custom cell editor. So to say you can't and say you can is a bit contradicting. The following link shows you to make a custom cell editor that uses a JButton.

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editor
 
Prashanth Bhanu
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This problem is bugging me.I have added a JButton to the Jtable .I wrote a JTable

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}

setText( (value ==null) ? "" : value.toString() );
return this;
}

But when i cick on JButton,My cell gets editable.I dont want that to happen?.
2)How do i add just a sinle coulmn to the Jtable.But dont want header to appear?

Thanks in advance
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the link I provided? Did you create a custom cell editor?

public class ButtonEditor extends AbstractCellEditor implements TableCellEditor,....

And then apply that editor to your Table for that column?
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
You can use a JButton in a cell if you use a custom cell editor. So to say you can't and say you can is a bit contradicting.


Well you and I know how cell renderers/editors work, so for us it's a matter of semantics.

Yes it is possible to use a JButton in a JTable, but the JTable doesn't actually contain JButtons (except for at most one, briefly during cell editing), it just gives the illusion of containing JButtons.

Note that I didn't say you can't use a JButton in a JTable, I said you can't use a JButton's built-in ActionEvent handling. So I was playing the semantics game too.

For those that don't yet understand JTable renderers/editors (which would seem to include Prashanth Bhanu, and probably a majority of people on this forum) I think it is important to point of that if you want buttons (or sliders, etc) in a JTable you have to fake it. You have to provide a TableCellRenderer to paint the cell as if it contained that component, and you (usually) have to provide a TableCellEditor to maintain the illusion when the user clicks on the cell.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:


Well you and I know how cell renderers/editors work, so for us it's a matter of semantics.

Yes it is possible to use a JButton in a JTable, but the JTable doesn't actually contain JButtons (except for at most one, briefly during cell editing), it just gives the illusion of containing JButtons.

Note that I didn't say you can't use a JButton in a JTable, I said you can't use a JButton's built-in ActionEvent handling. So I was playing the semantics game too.

For those that don't yet understand JTable renderers/editors (which would seem to include Prashanth Bhanu, and probably a majority of people on this forum) I think it is important to point of that if you want buttons (or sliders, etc) in a JTable you have to fake it. You have to provide a TableCellRenderer to paint the cell as if it contained that component, and you (usually) have to provide a TableCellEditor to maintain the illusion when the user clicks on the cell.


Good response. I agree. Thanks Brian.
 
Prashanth Bhanu
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Well Brian Cole can you please suggest me a link which can give me the correct picture of the Editor and renderer concepts.If each section is with relevent examples then it would give me better picture of what is going on

Thanks
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prashanth Bhanu:
please suggest me a link which can give me the correct picture of the Editor and renderer concepts.


Well this is pretty much the same link that Mr. Bolinger gave you yesterday, but here you go:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

There are other sources for this kind of information, including books.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

Well thanks for the link.Thanks to Gregg as well.Wlll,I've understood editer and renderer to large extent.By using renderer i managed to achive what i wanted on a JTable.
Now I have a Java class which would create me a specific JButton and will return that created JButton upon calling a method which is inside that class.
Now i have another class where i'm having a JTable and i want to add a JButton which is obtained from the above class to this Jtable.But i cant do some thing like
Jtable.getColumnModel().getColumn(2).setEditer(editor)
Because all the button which will sit in subsequent cells will be having different label on them ex:new JButton("Gregg"),
new JButton("Cole") etc..
So i'm having difficulty to incorporate this.Can you please suggest me to use this Button as Editor to my particular colums cells in the Jtable.

Thanks in advance

Prashanth
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is where you are trying to create the new JButton("Gregg").


At this point, the cell renderer doesn't care what the button is. All it cares is that it is a JButton. What you need to do is feed in the appropriate JButton when you feed the column/row data to the table. So changing the example from the link a bit:



You could also feed in already created JButton's so that they have action listeners already attached to them. You just need to write your code to support that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic