Use JTable entries as buttons - java

I have a JTable which contains names returned from a search in database. I want to use the names as buttons, when we click on them the program redirect to another frame with more details of the nameholder. Can I do that ?

This is easy to do, add the mouse listener to the table and use columnAtPoint and rowAtPoint to determine what has been clicked.
If you want a table cell to look like a button, set a renderer that uses JButton.

I want to use the names as buttons, when we click on them the program redirect to another frame with more details of the nameholder.
You need to create a custom renderer and editor for your table. Read the section from the Swing tutorial on Concepts: Editors and Renderers for basic information.
Then you can check out Table Button Column which is an example of a render/editor that you can use. You will need to provide your own custom Action to display the details.

Related

How to add a control panel on a JTable row on MouseOver (hover)

I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.

How to create multi color rows on JTable

I built a form with Netbeans JFrame Form, I mean from netbeanse itself by right click on the project and then New and then JFrame Form. Also I use a Jtable by dragging that on the form. Now in my code I want to colorize the rows on Jtable. I have seen some idea from https://coderanch.com/t/476777/java/Color-Row-JTable But all the direction which I have seen is about overriding Jatble. My problem is In my program netbeans it self does the code in declaration JFrame element from where I can override the code or specifically in this case how can I make the rows colorized ?
If you have already put the JTable in the JFrame you can right click on it and then chose Customize Code.
It will present you a window where some code is written. You can add custom code on the white lines, not the gray ones unless you want to change an already defined property.
Remember though that you should implement your custom cell renderer first.
In case you want to change an already defined property of the JTable, you have to first change the Default Code drop down menu on the left of the property to custom creation.
I hope this will help you out.

How to render a Component in JTable using TableModel?

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.

multiple Jpanel with same functionality buttons

i want to create JFrame with multiple panel each panel displaying some information from database (say product_id and description ) and each frame have a button(add to cart) which will add the information from that panel to cart table in my database how will i implement this?
I would try to simply things by using a JTable to display such data, with each column displaying a database field. I'd add an extra column, say a Boolean column that displays as a check box, and then assess the state of that column when needing to decide which to add to a cart.
Edit You ask:
how will i know how much quantity of that product is selected,
Then add a quantity field as well, perhaps one that uses a JSpinner as its editor.
and also i am thinking to add image of that product also can i do it in Jtables
Absolutely. It knows how to display ImageIcons for instance. Please have a look at my answer to another question for an example.
You should create a JPanel (called PanelButton) which contents [Add to cart] button.
In other JPanels, you add PanelButton.
So, by this way, you can reuse your button without implement a lot of [Add to cart] button for each panel.

JTable with JButton as cell editor

I used a JButton as render and editor in JTable, with following problems:
The JTable row is not selected when JButton clicked.
When the JButton is clicked, how to get its location within JTable, in other words its columnIndex and rowIndex
Any help will be appreciated!
To display a button in a column you need to create:
a custom renderer to display the JButton
a custom editor to respond to the mouse click
Read the section from the Swing tutorial on How to Use Tables. The section on:
Using Custom Renders will explain the basics of using a renderer
Using Other Editors will explain the basics of using an editor
Working example are provided in the tutorial that you can download.
You can check out Table Button Column for one approach.
The code uses a single class to implement the custom renderer and editor that you will need for the column to display your text as a button.

Categories