how to modify a table getToolTipText when its already created












0















I want to add a tooltip for every cell, but I'm having trouble modifying a table getToolTipText when its already created with JTable table = new JTable(model);
I found that maybe with getTableCellRendererComponent could work, but i cant find how. Here is an example of what I have.



package com.QA;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;


public class cellrenderer {
private JScrollPane getContent() {
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new Object { "Name", "Age", "State" });
model.addRow(new Object { "Peter", 29, "Florida" });
model.addRow(new Object { "Gabriel", 32, "Oregon" });
model.addRow(new Object { "Hans", 27, "Texas" });
model.addRow(new Object { "Serge", 30, "Ohio" });
JTable table = new JTable(model);
TableColumnModel colModel = table.getColumnModel();
for(int j = 0; j < colModel.getColumnCount(); j++)
colModel.getColumn(j).setCellRenderer(new RowRenderer());
return new JScrollPane(table);
}

public static void main(String args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new cellrenderer().getContent());
f.setSize(400,140);
f.setLocation(200,200);
f.setVisible(true);
}
}

class RowRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
****TRYING TO SET TOOL TIP FOR EVERYCELL***


return this;
}
}


And this is the working code that i does what i want but only when the table is being created JTable table = new JTable(data, columns){....



package com.QA;


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import java.awt.event.MouseEvent;


public class Stooltipxcell extends JFrame
{
public Stooltipxcell()
{
//headers for the table
String columns = new String {
"Id", "Name", "Hourly Rate", "Part Time"
};

//actual data for the table in a 2d array
Object data = new Object {
{1, "John", 40.0, false },
{2, "Rambo", 70.0, false },
{3, "Zorro", 60.0, true },
};
//create table with data
JTable table = new JTable(data, columns){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);

try {
tip = getValueAt(rowIndex, colIndex).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}

return tip;
}};

//add the table to the frame
this.add(new JScrollPane(table));

this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}

public static void main(String args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Stooltipxcell();
}
});
}
}









share|improve this question


















  • 2





    DefaultTableCellRenderer has a setToolTipText() method. Just call it.

    – JB Nizet
    Nov 23 '18 at 22:29








  • 2





    See the section from the Swing tutorial on: Specifying Tool Tips For Cells

    – camickr
    Nov 23 '18 at 22:33











  • thanks for pointing me the way, here is what i added for it to work setToolTipText((String)table.getValueAt(row, column).toString());

    – tseres
    Nov 23 '18 at 22:51











  • Why don't you use value instead of table.getValueAt(row, column)?

    – JB Nizet
    Nov 23 '18 at 23:13











  • I dont know how to do that

    – tseres
    Nov 24 '18 at 0:11
















0















I want to add a tooltip for every cell, but I'm having trouble modifying a table getToolTipText when its already created with JTable table = new JTable(model);
I found that maybe with getTableCellRendererComponent could work, but i cant find how. Here is an example of what I have.



package com.QA;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;


public class cellrenderer {
private JScrollPane getContent() {
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new Object { "Name", "Age", "State" });
model.addRow(new Object { "Peter", 29, "Florida" });
model.addRow(new Object { "Gabriel", 32, "Oregon" });
model.addRow(new Object { "Hans", 27, "Texas" });
model.addRow(new Object { "Serge", 30, "Ohio" });
JTable table = new JTable(model);
TableColumnModel colModel = table.getColumnModel();
for(int j = 0; j < colModel.getColumnCount(); j++)
colModel.getColumn(j).setCellRenderer(new RowRenderer());
return new JScrollPane(table);
}

public static void main(String args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new cellrenderer().getContent());
f.setSize(400,140);
f.setLocation(200,200);
f.setVisible(true);
}
}

class RowRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
****TRYING TO SET TOOL TIP FOR EVERYCELL***


return this;
}
}


And this is the working code that i does what i want but only when the table is being created JTable table = new JTable(data, columns){....



package com.QA;


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import java.awt.event.MouseEvent;


public class Stooltipxcell extends JFrame
{
public Stooltipxcell()
{
//headers for the table
String columns = new String {
"Id", "Name", "Hourly Rate", "Part Time"
};

//actual data for the table in a 2d array
Object data = new Object {
{1, "John", 40.0, false },
{2, "Rambo", 70.0, false },
{3, "Zorro", 60.0, true },
};
//create table with data
JTable table = new JTable(data, columns){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);

try {
tip = getValueAt(rowIndex, colIndex).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}

return tip;
}};

//add the table to the frame
this.add(new JScrollPane(table));

this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}

public static void main(String args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Stooltipxcell();
}
});
}
}









share|improve this question


















  • 2





    DefaultTableCellRenderer has a setToolTipText() method. Just call it.

    – JB Nizet
    Nov 23 '18 at 22:29








  • 2





    See the section from the Swing tutorial on: Specifying Tool Tips For Cells

    – camickr
    Nov 23 '18 at 22:33











  • thanks for pointing me the way, here is what i added for it to work setToolTipText((String)table.getValueAt(row, column).toString());

    – tseres
    Nov 23 '18 at 22:51











  • Why don't you use value instead of table.getValueAt(row, column)?

    – JB Nizet
    Nov 23 '18 at 23:13











  • I dont know how to do that

    – tseres
    Nov 24 '18 at 0:11














0












0








0








I want to add a tooltip for every cell, but I'm having trouble modifying a table getToolTipText when its already created with JTable table = new JTable(model);
I found that maybe with getTableCellRendererComponent could work, but i cant find how. Here is an example of what I have.



package com.QA;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;


public class cellrenderer {
private JScrollPane getContent() {
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new Object { "Name", "Age", "State" });
model.addRow(new Object { "Peter", 29, "Florida" });
model.addRow(new Object { "Gabriel", 32, "Oregon" });
model.addRow(new Object { "Hans", 27, "Texas" });
model.addRow(new Object { "Serge", 30, "Ohio" });
JTable table = new JTable(model);
TableColumnModel colModel = table.getColumnModel();
for(int j = 0; j < colModel.getColumnCount(); j++)
colModel.getColumn(j).setCellRenderer(new RowRenderer());
return new JScrollPane(table);
}

public static void main(String args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new cellrenderer().getContent());
f.setSize(400,140);
f.setLocation(200,200);
f.setVisible(true);
}
}

class RowRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
****TRYING TO SET TOOL TIP FOR EVERYCELL***


return this;
}
}


And this is the working code that i does what i want but only when the table is being created JTable table = new JTable(data, columns){....



package com.QA;


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import java.awt.event.MouseEvent;


public class Stooltipxcell extends JFrame
{
public Stooltipxcell()
{
//headers for the table
String columns = new String {
"Id", "Name", "Hourly Rate", "Part Time"
};

//actual data for the table in a 2d array
Object data = new Object {
{1, "John", 40.0, false },
{2, "Rambo", 70.0, false },
{3, "Zorro", 60.0, true },
};
//create table with data
JTable table = new JTable(data, columns){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);

try {
tip = getValueAt(rowIndex, colIndex).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}

return tip;
}};

//add the table to the frame
this.add(new JScrollPane(table));

this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}

public static void main(String args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Stooltipxcell();
}
});
}
}









share|improve this question














I want to add a tooltip for every cell, but I'm having trouble modifying a table getToolTipText when its already created with JTable table = new JTable(model);
I found that maybe with getTableCellRendererComponent could work, but i cant find how. Here is an example of what I have.



package com.QA;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;


public class cellrenderer {
private JScrollPane getContent() {
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new Object { "Name", "Age", "State" });
model.addRow(new Object { "Peter", 29, "Florida" });
model.addRow(new Object { "Gabriel", 32, "Oregon" });
model.addRow(new Object { "Hans", 27, "Texas" });
model.addRow(new Object { "Serge", 30, "Ohio" });
JTable table = new JTable(model);
TableColumnModel colModel = table.getColumnModel();
for(int j = 0; j < colModel.getColumnCount(); j++)
colModel.getColumn(j).setCellRenderer(new RowRenderer());
return new JScrollPane(table);
}

public static void main(String args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new cellrenderer().getContent());
f.setSize(400,140);
f.setLocation(200,200);
f.setVisible(true);
}
}

class RowRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
****TRYING TO SET TOOL TIP FOR EVERYCELL***


return this;
}
}


And this is the working code that i does what i want but only when the table is being created JTable table = new JTable(data, columns){....



package com.QA;


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import java.awt.event.MouseEvent;


public class Stooltipxcell extends JFrame
{
public Stooltipxcell()
{
//headers for the table
String columns = new String {
"Id", "Name", "Hourly Rate", "Part Time"
};

//actual data for the table in a 2d array
Object data = new Object {
{1, "John", 40.0, false },
{2, "Rambo", 70.0, false },
{3, "Zorro", 60.0, true },
};
//create table with data
JTable table = new JTable(data, columns){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);

try {
tip = getValueAt(rowIndex, colIndex).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}

return tip;
}};

//add the table to the frame
this.add(new JScrollPane(table));

this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}

public static void main(String args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Stooltipxcell();
}
});
}
}






java swing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 22:24









tserestseres

266




266








  • 2





    DefaultTableCellRenderer has a setToolTipText() method. Just call it.

    – JB Nizet
    Nov 23 '18 at 22:29








  • 2





    See the section from the Swing tutorial on: Specifying Tool Tips For Cells

    – camickr
    Nov 23 '18 at 22:33











  • thanks for pointing me the way, here is what i added for it to work setToolTipText((String)table.getValueAt(row, column).toString());

    – tseres
    Nov 23 '18 at 22:51











  • Why don't you use value instead of table.getValueAt(row, column)?

    – JB Nizet
    Nov 23 '18 at 23:13











  • I dont know how to do that

    – tseres
    Nov 24 '18 at 0:11














  • 2





    DefaultTableCellRenderer has a setToolTipText() method. Just call it.

    – JB Nizet
    Nov 23 '18 at 22:29








  • 2





    See the section from the Swing tutorial on: Specifying Tool Tips For Cells

    – camickr
    Nov 23 '18 at 22:33











  • thanks for pointing me the way, here is what i added for it to work setToolTipText((String)table.getValueAt(row, column).toString());

    – tseres
    Nov 23 '18 at 22:51











  • Why don't you use value instead of table.getValueAt(row, column)?

    – JB Nizet
    Nov 23 '18 at 23:13











  • I dont know how to do that

    – tseres
    Nov 24 '18 at 0:11








2




2





DefaultTableCellRenderer has a setToolTipText() method. Just call it.

– JB Nizet
Nov 23 '18 at 22:29







DefaultTableCellRenderer has a setToolTipText() method. Just call it.

– JB Nizet
Nov 23 '18 at 22:29






2




2





See the section from the Swing tutorial on: Specifying Tool Tips For Cells

– camickr
Nov 23 '18 at 22:33





See the section from the Swing tutorial on: Specifying Tool Tips For Cells

– camickr
Nov 23 '18 at 22:33













thanks for pointing me the way, here is what i added for it to work setToolTipText((String)table.getValueAt(row, column).toString());

– tseres
Nov 23 '18 at 22:51





thanks for pointing me the way, here is what i added for it to work setToolTipText((String)table.getValueAt(row, column).toString());

– tseres
Nov 23 '18 at 22:51













Why don't you use value instead of table.getValueAt(row, column)?

– JB Nizet
Nov 23 '18 at 23:13





Why don't you use value instead of table.getValueAt(row, column)?

– JB Nizet
Nov 23 '18 at 23:13













I dont know how to do that

– tseres
Nov 24 '18 at 0:11





I dont know how to do that

– tseres
Nov 24 '18 at 0:11












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453485%2fhow-to-modify-a-table-gettooltiptext-when-its-already-created%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453485%2fhow-to-modify-a-table-gettooltiptext-when-its-already-created%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'