有没有办法在其中插入“单元格”?在 MigLayout 中?

发布于 2024-10-03 23:30:56 字数 626 浏览 7 评论 0原文

我正在尝试创建一个可以根据事件动态插入组件的面板。该面板是基于行的,每行的组件数量可变。但是,我在现有组件之间插入组件时遇到问题。

例如,如果我有以下布局(线条代表 MigLayout 单元格):

+----+----+----+  
| X1 | X2 | X3 |
+----+----+----+----+
| Y1 | Y2 | Y3 | Y4 |
+----+----+----+----+

是否可以在 X 行和 Y 行之间创建一个单元格,以获得:

+----+----+----+  
| X1 | X2 | X3 |
+----+----+----+
| Z1 |
+----+----+----+----+
| Y1 | Y2 | Y3 | Y4 |
+----+----+----+----+

我尝试了 content.add(component, "cell 1 0, wrap") ; 但它将组件插入到 Y1 单元格中。

到目前为止,我唯一的解决方案是调用 content.add(component, "wrap", index);。然而,这要求我知道前面组件的总数。

I am trying to create a panel that can dynamically insert components based on events. The panel is row based with variable amount of components per row. However, I have problems inserting components between existing ones.

For example, if I have following layout (lines represent MigLayout cells) :

+----+----+----+  
| X1 | X2 | X3 |
+----+----+----+----+
| Y1 | Y2 | Y3 | Y4 |
+----+----+----+----+

Is it possible to create a cell between rows X and Y, to get:

+----+----+----+  
| X1 | X2 | X3 |
+----+----+----+
| Z1 |
+----+----+----+----+
| Y1 | Y2 | Y3 | Y4 |
+----+----+----+----+

I tried content.add(component, "cell 1 0, wrap"); but it inserts the component into the Y1 cell.

The only solution I have so far is to call content.add(component, "wrap", index);. However, this requires that I know a total count of preceding components.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

入怼 2024-10-10 23:30:56

“隐藏模式”约束可以帮助您:

new MigLayout("hidemode 3","","[][][]")

"hidemode" constrain help you:

new MigLayout("hidemode 3","","[][][]")
2024-10-10 23:30:56

您可以在 migLayout 中使用单元格的“绝对”定位,而不是使用“单元格”概念,并通过引用相邻单元格的坐标将单元格粘合在一起。您需要为单元格命名,以便在 migLayout 中引用它们。

然后,您可以随时通过更改单元格坐标中的名称引用来重新排列单元格,只需为受影响的单元格调用 setComponentConstraints(...) 即可。

如果您要插入单元格 Z,请

参阅 miglayout 演示应用程序。
一些代码可能看起来像:

myPanel.add(createCell("X1"), "pos 0 0 100 100");
myPanel.add(createCell("X2"), "pos X1.x2 0 200 X1.y2");

等等。

当插入单元格“Z”时,

myPanel.add(createCell("Z"), "pos 0 X1.z2 200 100");

只需将Y1的y参考从X1.y2更改为Z.y2。

我做了某事。类似于创建一种多分割面板,而且看起来效果很好。

Instead of using the "cell" concept, you could use "absolute" positioning for your cells in migLayout, and glue the cells together by referencing the coordinates of the neighbouring cells. You need to give names to your cells in order to reference them in migLayout.

Then, you can re-arrange the cells at any time by altering the name-references in the coordinates of the cells, you just need to call setComponentConstraints(...) for the affected cells.

f.i. you for insertige cell Z, you

See the miglayout demo application.
some code might look like:

myPanel.add(createCell("X1"), "pos 0 0 100 100");
myPanel.add(createCell("X2"), "pos X1.x2 0 200 X1.y2");

and so on.

When you insert cell "Z",

myPanel.add(createCell("Z"), "pos 0 X1.z2 200 100");

you only need to change the y reference of Y1 from X1.y2 to Z.y2.

I did sth. similar to create a kind of multi-splitpane, and it seems to work well.

柒七 2024-10-10 23:30:56

没有直接的方法来插入新组件,但是删除并重新添加所有现有组件非常简单,无需复制粘贴。其中重要的一点是,无论原始组件布局在哪里完成,都不必可访问,并且您不必将所有布局代码放在多个位置。

  Map<Component,Object> constraintMap = migLayout.getConstraintMap();
  Component[] allComps = jPanel.getComponents();
  jPanel.removeAll();
  for (Component c : allComps) {
    if ( condition_to_insert ) {
      jPanel.add(insertComponent, new CC());
    } 
    jPanel.add(c, constraintMap.get(c));

  }
  migLayout.invalidateLayout(jPanel);

There is no direct way to insert a new component, however it is fairly simple to remove and re-add all the existing components without copy-pasta. The important part in this is that wherever the original component layout is done does not have to be accessible, and you don't have to have all the layout code in more than one place.

  Map<Component,Object> constraintMap = migLayout.getConstraintMap();
  Component[] allComps = jPanel.getComponents();
  jPanel.removeAll();
  for (Component c : allComps) {
    if ( condition_to_insert ) {
      jPanel.add(insertComponent, new CC());
    } 
    jPanel.add(c, constraintMap.get(c));

  }
  migLayout.invalidateLayout(jPanel);
╰ゝ天使的微笑 2024-10-10 23:30:56

我不确定这是否是最好的解决方案,但您可以再次重新放置整个容器,包括您的新组件。我认为没有办法按照您想要的方式插入组件。

希望我错了。

但这样做应该不会有太多开销,而且用户会很快注意到!

i'm not sure if this is the best solution, but you could re-lay the whole container again, including your new component. I don't think there's a way to insert components the way you want to.

hopefully i'm wrong.

but it shouldn't be much overhead to do this and it would occur to fast for the user to notice!

计㈡愣 2024-10-10 23:30:56

一般来说,面板和 Swing 组件并不是真正为动态重组而设计的,所以我认为 psanton 的解决方案可能是最安全和最简单的方法。在使用现有代码重新添加元素之前,您可以通过调用 removeAll() 来重用该面板。

如果您自己使用索引的建议也有效,那么我会同意 - 跟踪模型中的前几行应该很容易实现,并且重组您的组件会更快一些,尽管您不太可能这样做除非您有数百个子元素,否则您会感受到差异。

Panels and Swing components in general are not really designed for dynamic restructuring, so I think psanton's solution is probably the safest and simplest way. You can reuse the panel by calling removeAll() on it before re-adding the elements using your existing code.

If your own suggestion of using an index also works, then I'd go with that - keeping track of preceding rows in your model should be very easy to implement, and restructuring your components will be a little faster, though it's unlikely that you'll feel the difference unless you have hundreds of sub-elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文