LinkLabel is not displaying correctly and showing random other elements in its background

Answered

Hi, I'm using the LinkLabel component to display a couple of hyperlinks, but they look like this.

This problem appears in both the new UI and the old.

I'm not sure what to do about this… I've built a DSL in Kotlin for UI and I'm using that to display these, but I don't think that's the problem. It's quite a bit of code too so I feel bad pasting it here, but I'll show some of it.

This is the code to display it with the DSL.

val (newPopup) = popupMenu {
  panel {
    border { customLine(JBColor.foreground(), 1) }
    if (jiraItem != null) {
      linkLabel {
        text = "Jira Item: $jiraItem"
        url = "https://mylink.com"
        border { emptyRight(2) }
      }
    } else {
      label {
        text = "Jira Item: Unknown"
        border { emptyRight(2) }
      }
    }
    if (pullRequestNumber != null) {
      linkLabel {
        text = "Pull Request: $pullRequestNumber"
        url = "https://mylink.com"
        border { emptyLeft(2) }
      }
    } else {
      label {
        text = "Pull Request: Unknown"
        border { emptyLeft(2) }
      }
    }
  }
}
newPopup.show(editor.component, point.x, point.y)

 

Here is the backing code for the LinkLabel. 

fun linkLabel(block: LinkLabelBuilder.() -> Unit): Pair<JLabel, Any?> {
  return LinkLabelBuilder().apply(block).build()
}

fun JComponentBuilder<*>.linkLabel(block: LinkLabelBuilder.() -> Unit) {
  val (component, constraints) = LinkLabelBuilder().apply(block).build()
  items.add(component)
  itemsConstraints.add(constraints)
}

class LinkLabelBuilder : LabelBuilder() {
  lateinit var url: String

  override fun build(): Pair<JLabel, Any?> {
    val label = LinkLabel<Any>(text, icon) { _, _ ->
      BrowserUtil.browse(url)
    }
    icon = PlatformIcons.WEB_ICON
    setProps(label)
    return Pair(build(label), constraints)
  }
}

There of course is more, since I'm not showing `JComponentBuilder` or `LabelBuilder`, but you get the idea. If you would like to see more, please let me know and I'll paste it in. I just want to keep this as brief as possible. `LabelBuilder.setProps` just sets a bunch of label-specific props if they are not null. In this case, just `text` and `icon`. `JComponentBuilder` sets the rest of the common `JComponent` props, again only if they aren't null. Here's the general pattern for the prop setting in the builder classes.

prop?.let {
  component.prop = it
}

Thanks!

0
2 comments

I should clarify that this is not a problem if I use a normal label manually constructed with `JLabel()`. Only the `LinkLabel`s are problematic.

0

Hi,

It's hard to tell what can be the reason without debugging.

I suggest using https://plugins.jetbrains.com/docs/intellij/internal-ui-inspector.html to inspect LinkLabel and a simple JLabel in the same place and compare which properties are different and try to change them in LinkLabel. The LinkLabel class extends JLabel, so it should be possible to make them behave the same way.

0

Please sign in to leave a comment.