Skip to content

Fix SwingPanel size not scaling according to LocalDensity#3033

Merged
Alexander Maryanovsky (m-sasha) merged 1 commit into
jb-mainfrom
m-sasha/fix-swingpanel-density
May 7, 2026
Merged

Fix SwingPanel size not scaling according to LocalDensity#3033
Alexander Maryanovsky (m-sasha) merged 1 commit into
jb-mainfrom
m-sasha/fix-swingpanel-density

Conversation

@m-sasha

@m-sasha Alexander Maryanovsky (m-sasha) commented May 6, 2026

Copy link
Copy Markdown

Fix SwingPanel size not scaling according to LocalDensity

SwingInteropViewHolder.layoutAccordingTo was using the local density to convert between Compose and AWT coordinates. But the local density can be changed, whereas the AWT:compose factor is fixed (the density of the window).

Fixes https://youtrack.jetbrains.com/issue/CMP-9449

Testing

Tested manually with

fun main() = singleWindowApplication {
    var zoom by remember { mutableStateOf(1.1f) }
    val defaultDensity = LocalDensity.current.density
    CompositionLocalProvider(LocalDensity provides Density(defaultDensity * zoom, 1f)) {
        Column(Modifier.fillMaxSize()) {
            SwingPanel(
                factory = {
                    object: JComponent() {
                        override fun paint(g: Graphics) {
                            g.color = java.awt.Color.RED
                            g.fillRect(0, 0, width, height)
                        }
                    }
                },
                modifier = Modifier.border(1.dp, Color.Black).padding(1.dp).size(200.dp, 200.dp)
            )

            Spacer(Modifier.height(16.dp))

            Row(verticalAlignment = Alignment.CenterVertically) {
                Button(onClick = { zoom *= 1.1f }) { Text("Zoom In") }
                Button(onClick = { zoom /= 1.1f }) { Text("Zoom out") }
            }
        }
    }
}

and added a unit test.

Release Notes

Fixes - Desktop

  • Fix SwingPanel size not scaling according to LocalDensity. Note that this doesn't scale the Swing content of the SwingPanel.

MatkovIvan

This comment was marked as resolved.

@m-sasha Alexander Maryanovsky (m-sasha) merged commit c874bdf into jb-main May 7, 2026
28 of 29 checks passed
@m-sasha Alexander Maryanovsky (m-sasha) deleted the m-sasha/fix-swingpanel-density branch May 7, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants