不能在单元格中对齐左侧的文本,而在右侧的图标
我正在尝试实现一个非常简单的事情,我尝试了不同的选择,但似乎没有任何选择。 我有一个简单的jtable,在我使用jlabel的列中以显示文本和图标。 我只想让文本在单元格的左侧,右侧的图标。
如果我使用此片段:
label.setHorizontalTextPosition(SwingConstants.LEFT);
label.setHorizontalAlignment(SwingConstants.RIGHT);
我只是按照自己的意愿将图标放在右边,但是文本立即在图标的左侧。 因此,我正在设置图标和文本差距。
我是这样设置的:
int width = getWidth();
label.setSize(width, getHeight());
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
int textwidth = (int)(label.getFont().getStringBounds(label.getText(), frc).getWidth());
label.setIconTextGap(width - textwidth - label.getIcon().getIconWidth());
调试它,似乎做应该做的事情,但是我得到了这个结果:
... textWidth 2次)这是结果:
顺便说一句,如果我再次减少差距,则显示文本,但当然,我没有得到我想要的事情保持一致。
我想实现的是一个类似于编辑器组件的渲染器,该组件是一个组合框,如下所示:
这不是优先任务对我来说,但我真的很想了解如何管理这一点。
编辑:
我还尝试使用一个容器(也是jpanel)来添加2个jlabels并按照@madprogrammer的建议更好地管理文本和图标,但是使用单元格下的代码为空,只有背景是可见
public java.awt.Component getTreeTableCellRendererComponent(TreeTable treeTable, Object value, boolean selected, boolean hasFocus, int row, int column) {
Container container = new Container();
JLabel label = new JLabel();
JLabel icon = new JLabel();
label.setText("XXX");
icon.setIcon(myIcon);
if (row % 2 == 0) {
container.setBackground(new java.awt.Color(220, 245, 230));
}
else {
container.setBackground(new java.awt.Color(240, 255, 240));
}
label.repaint();
icon.repaint();
container.add(label, BorderLayout.WEST);
container.add(icon, BorderLayout.CENTER);
container.revalidate();
container.doLayout();
container.setVisible(true);
container.repaint();
return container;
}
的上面的代码运行而不投掷例外。
I am trying to achieve a very simple thing, I tried different options but none seems to work.
I have a simple JTable, in a column I use JLabel in order to show the text and the icon.
I just want to have the text to be on the left of the cell and the Icon to the right.
if I use this snippet:
label.setHorizontalTextPosition(SwingConstants.LEFT);
label.setHorizontalAlignment(SwingConstants.RIGHT);
I just get the icon on the right as I want, but the text is immediately on the left of the icon.
So I am setting the icon and text gap.
I am setting it like this:
int width = getWidth();
label.setSize(width, getHeight());
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
int textwidth = (int)(label.getFont().getStringBounds(label.getText(), frc).getWidth());
label.setIconTextGap(width - textwidth - label.getIcon().getIconWidth());
Debugging it, it seems to do what is supposed to do, but I got this result:
I tried to add a bit of tolerance (subtracting textwidth 2 times) this is the result:
By the way, the text is shown if I reduce the gap again, but of course, I do not get things aligned as I want.
What I simply would like to achieve is a renderer similar to the editor component which is a combo box as you can see below:
This is not a priority task for me but I would really like to understand how to manage this.
EDIT:
I also tried to use a Container (also a JPanel) in order to add 2 JLabels and manage the text and the icon better as suggested by @MadProgrammer, but using the code below the cell is empty, only the background is visible
public java.awt.Component getTreeTableCellRendererComponent(TreeTable treeTable, Object value, boolean selected, boolean hasFocus, int row, int column) {
Container container = new Container();
JLabel label = new JLabel();
JLabel icon = new JLabel();
label.setText("XXX");
icon.setIcon(myIcon);
if (row % 2 == 0) {
container.setBackground(new java.awt.Color(220, 245, 230));
}
else {
container.setBackground(new java.awt.Color(240, 255, 240));
}
label.repaint();
icon.repaint();
container.add(label, BorderLayout.WEST);
container.add(icon, BorderLayout.CENTER);
container.revalidate();
container.doLayout();
container.setVisible(true);
container.repaint();
return container;
}
the code above runs without throwing exceptions..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这个旧代码围绕着演示使用面板作为渲染器的概念。前3个字符将显示在左侧,其余的则在右侧:
Had this old code lying around demonstrating the concept of using a panel as a renderer. The first 3 characters will be displayed on the left and the remainder on the right: