Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9030873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:35:52+00:00 2026-06-16T07:35:52+00:00

I’m making a program for school which you can log the systems clipboard history,

  • 0

I’m making a program for school which you can log the systems clipboard history, my teacher said it’s good so far, but I need to add images. So I got a few images to represent an image, url, text, or a folder. But when I try to .addRow with the image, it shows the source of the image and not the actual image. Here’s my class

public class Main extends JFrame implements ClipboardOwner {

    private static final long serialVersionUID = -7215911935339264676L;

    public final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    private static DecimalFormat df = new DecimalFormat("#.##");

    public static ArrayList<NewEntry> history = new ArrayList<NewEntry>();

    private DefaultTableModel model;

    private JTable table;

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(new GraphiteLookAndFeel());
        Static.main.setVisible(true);
    }

    public Main() {
        super("Clipboard Logger");

        setSize(667, 418);
        setLocationRelativeTo(null);
        getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        model = new DefaultTableModel(null, new String[] { "Type", "Content", "Size", "Date" });
        table = new JTable(model) {

            private static final long serialVersionUID = 2485117672771964339L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                int r = table.rowAtPoint(e.getPoint());
                if (r >= 0 && r < table.getRowCount()) {
                    table.setRowSelectionInterval(r, r);
                } else {
                    table.clearSelection();
                }

                int rowindex = table.getSelectedRow();
                if (rowindex < 0)
                    return;
                if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
                    createAndShowPopupMenu(rowindex, e.getX(), e.getY());
                }
            }
        });

        JScrollPane pane = new JScrollPane(table);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        getContentPane().add(pane);

        Transferable trans = clipboard.getContents(this);
        regainOwnership(trans);

        setIconImage(Static.icon);
    }

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable transferable) {
        try {
            Thread.sleep(50);
            Transferable contents = clipboard.getContents(this);
            processContents(contents);
            regainOwnership(contents);
        } catch (Exception e) {
            regainOwnership(clipboard.getContents(this));
        }
    }

    public void processContents(Transferable t) throws UnsupportedFlavorException, IOException {
        System.out.println("Processing: " + t);
        DataFlavor[] flavors = t.getTransferDataFlavors();
        File file = new File(t.getTransferData(flavors[0]).toString().replace("[", "").replace("]", ""));
        System.out.println("HI"+file.getName());
        NewEntry entry = null;
        if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            System.out.println("String Flavor");
            String s = (String) (t.getTransferData(DataFlavor.stringFlavor));
            entry = new NewEntry(EntryType.TEXT, s, getSizeUnit(s.getBytes().length), DateFormat.getDateInstance().format(new Date()));
        } else if (isValidImage(file.getName()) && file.exists()) {
            System.out.println("Image Flavor");

            entry = new NewEntry(EntryType.IMAGE, file.getAbsolutePath(), getSizeUnit(file.length()), DateFormat.getDateInstance().format(new Date()));
        }
        history.add(entry);
        model.addRow(new Object[] { entry.getIcon(), entry.getContent(), entry.getSize(), entry.getDate() });
    }

    public void regainOwnership(Transferable t) {
        clipboard.setContents(t, this);
    }

    public boolean isValidImage(String filename) {
        for (String string : ImageIO.getReaderFormatNames()) {
            if (filename.endsWith(string)) {
                return true;
            }
        }
        return false;
    }

    public void createAndShowPopupMenu(int index, int x, int y) {
        final NewEntry entry = history.get(index);
        if (entry == null) return;
        JPopupMenu pop = new JPopupMenu();
        JMenu jm = new JMenu("Open in");
        JMenuItem jmi = new JMenuItem("Browser");
        jmi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Desktop.getDesktop().browse(new URI("file:///"+entry.getContent().replace("\\", "/")));
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        jm.add(jmi);
        jmi = new JMenuItem("Windows Explorer");
        jmi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Desktop.getDesktop().open(new File(entry.getContent()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        jm.add(jmi);
        pop.add(jm);
        pop.show(this, x, y);
    }

    public static String getSizeUnit(long dataSize) {
        double n = dataSize;
        String suffix = "B";
        if (n > 1000) {
            suffix = "KB";
            n /= 1000;
        }
        if (n > 1000) {
            suffix = "MB";
            n /= 1000;
        }
        if (n > 1000) {
            suffix = "GB";
            n /= 1000;
        }
        return df.format(n)+suffix;
    }
}

And here’s my EntryType class

public class EntryType {

public static final ImageIcon TEXT = new ImageIcon(Toolkit.getDefaultToolkit().getImage("./resources/text.png"));

public static final ImageIcon URL = new ImageIcon(Toolkit.getDefaultToolkit().getImage("./resources/url.png"));

public static final ImageIcon FILE = new ImageIcon(Toolkit.getDefaultToolkit().getImage("./resources/file.png"));

public static final ImageIcon IMAGE = new ImageIcon(Toolkit.getDefaultToolkit().getImage("./resources/image.png"));

}

How would I make it so when I do this line of code:

model.addRow(new Object[] { entry.getIcon(), entry.getContent(), entry.getSize(), entry.getDate() });

it actually shows the icon and not the source?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:35:53+00:00Added an answer on June 16, 2026 at 7:35 am

    JTable provides a renderer for images. Override getColumnClass() and return Icon.class for the column that has an icon.

    See Editors and Renderers part of How to Use Tables tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app for my school newspaper, which is run completely online
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.