如何突出JLabel的一部分? [英] How to highlight part of a JLabel?

查看:100
本文介绍了如何突出JLabel的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何人建议HTML之前,我稍后会解释为什么这不是一个选项。我有一个表,其中包含一个包含文本单元格的列。我需要能够突出显示每个单元格中的一些文本。因此,例如,如果单元格包含cat foo dog...我可能想要突出显示foo。

Before any one suggests HTML, I explain later why thats not an option here. I have a table that contains a column with text cells in it. I need to be able to highlight some of the text in each cell. So for example if the cell contained "cat foo dog"... I might want to highlight foo.

我当前的方法是使用将html放入的自定义TableCellRenderer一个JLabel组件,它被渲染并且一段时间都很好。然后我注意到,当单元格中的文本变得太长而不适合列宽时,它只是截断文本而没有正常的......,在这种情况下正常发生。因此,用户不知道他们没有看到更多的文字。另一个问题是,如果原始文本本身包含HTML,在我的情况下它有时会执行,则单元格将无法正确呈现。我知道我可以逃脱HTML,但我仍然会有一个流行的问题。

My current method is to use a custom TableCellRenderer that puts html into a JLabel component that gets rendered and for a time it was good. Then I noticed that when the text in the cell became too long to fit in the column width it just truncated the text without the normal "..." that happens normally in this case. Thus users didnt know there was more text they were not seeing. The other problem was that if the original text itself contained HTML, which in my case it does at times, the cell would not render correctly. I know I could just escape the html but I would still have the prevous problem.

如果我使用jlabel以外的组件,那么它会使我的表格单元看起来很奇怪。有没有人有什么建议?谢谢

If I use a component other than a jlabel then it makes my table's cells look strange. Does any one have any suggestions? Thanks

推荐答案

嗯,这是一个解决方案。

Well, here is a solution.

简而言之,您可以继承 JLabel 以手动绘制突出显示。重写 paintComponent 方法以进行实际绘制并使用 FontMetrics 计算应该绘制突出显示区域的位置。

In short, you can subclass JLabel to draw the highlight manually. Override the paintComponent method to do the actual drawing and use FontMetrics to calculate where the highlighted region should be drawn.

以下是令人难以忍受的详细信息:

Here is that answer in excruciating detail:

基本上,您可以创建 JLabel 这可以突出的东西。我愿意这样做;您可能希望以不同的方式执行此操作:

Basically, you can make a subclass of JLabel that can highlight stuff. I would do that like this; you may want to do it somewhat differently:

添加一个方法,告诉标签要突出显示哪个部分。这可能是这样的,假设您只需要一个突出显示的区域:

Add a method that tells the label which part to highlight. This could be something like this, assuming you just need one highlighted region:

public void highlightRegion(int start, int end) {
     // Set some field to tell you where the highlight starts and ends...
}

如果你需要多个区域,只需要一个ArrayList而不是一个简单的字段。 dehighlighting的方法也许有用。

If you need multiple regions, just have an ArrayList instead of a simple field. A method for dehighlighting would probably be useful too.

现在,你需要覆盖 paintComponent 方法的JLabel 。在这里,您需要执行几个不同的步骤,您可能希望以不同的方法或其他方式组织这些步骤。为简单起见,我将把它全部放在paint方法中。

Now, you need to override the paintComponent method of JLabel. Here you need to do several discrete steps, which you may want to organize in different methods or something. For simplicity, I'll put it all in the paint method.

@Override
protected void paintComponent(Graphics g) {
  ...

首先,你需要弄清楚它的物理尺寸。突出显示,你可以使用漂亮的 FontMetrics 类来完成。为您正在使用的 Font 创建 FontMetrics 类。

First, you need to figure out the physical dimensions of the highlight, which you can do using the nice FontMetrics class. Create the FontMetrics class for the Font you're using.

  FontMetrics metrics = new FontMetrics(getFont());

现在,您可以获得创建将成为亮点的矩形所需的所有信息。你需要起始位置,高度和宽度。为此,您需要 JLabel 的两个子字符串,如下所示:

Now you can get all the information you need to create a rectangle that will be the highlight. You'll need the starting position, the height and the width. To get this, you'll need two substrings of the JLabel's text as follows:

  String start = getText().substring(0, startOfHighlight);
  String text = getText().substring(startOfHighlight, endOfHighlight);
  //You may also need to account for some offsets here:
  int startX = metrics.stringWidth(start);
  int startY = 0; //You probably have some vertical offset to add here.
  int length = metrics.stringWidth(text);
  int height = metrics.getHeight();

现在,您可以在绘制标签的其余部分之前绘制突出显示的区域:

Now you can draw the highlighted region before drawing the rest of the label:

  g.fillRect(startX, startY, length, height);
  super.paintComponent(g);
}

当然,如果您希望突出显示跨越多行,则需要更多的工作。

Of course, if you want the highlight to span multiple rows, that will require more work.

如果你想知道,我之前已经写过类似的东西了。一时兴起,我决定从 JPanel 编写我自己的文本区域类型组件,这基本上就是我处理突出显示的方式。在实际项目中重新发明轮子可能是愚蠢的,但它确实教会了你可能有用的随机东西...

If you were wondering, I have actually written something like this before. On a whim, I decided to write my own text area type component from a JPanel, and this was basically the way I handled highlighting. Reinventing the wheel may be stupid in an actual project, but it does teach you random stuff that may come in useful...

这篇关于如何突出JLabel的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆