【发布时间】:2020-12-11 19:08:35
【问题描述】:
我有一个 java 8 程序,其中 Parent 是一个 JFrame,它有菜单、几个按钮、一个文本字段和一个具有固定数量的不可编辑行的 JTable。无法动态更改行数和数据。
菜单有UIManager.getInstalledLookAndFeels()列表
最初 JTable 行的边框是可见的
如果 LookAndFeel 更改为 [Nimbus javax.swing.plaf.nimbus.NimbusLookAndFeel],然后尝试任何其他 LookAndFeel,行边框就会消失。
我正在使用SwingUtilities.updateComponentTreeUI(parentFrame) 申请 LnF。 LnF 适用于包括 JTable 在内的所有组件,但是一旦应用 Nimbus LnF 并在选择任何其他 LnF 之后,行边框就会消失。
作为一个选项repaint() 没有任何区别。
在图片中
- (1) 是程序在行边界可见的地方启动
- (2) 应用 Nimbus LnF 时
- (3) LnF 更改为金属,但行边框不可见
请提出建议。
示例代码:
package com.sv.runcmd;
import com.sv.core.logger.MyLogger;
import com.sv.swingui.SwingUtils;
import com.sv.swingui.component.AppExitButton;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static com.sv.core.Constants.SP_DASH_SP;
import static com.sv.swingui.UIConstants.EMPTY_BORDER;
public class LnFExample extends JFrame {
public static void main(String[] args) {
new LnFExample().initComponents();
}
private static final String APP_TITLE = "LnF";
private DefaultTableModel model;
private JTable tblCommands;
private JMenuBar mbarSettings;
public LnFExample() {
super(APP_TITLE);
SwingUtilities.invokeLater(this::initComponents);
}
/**
* This method initializes the form.
*/
private void initComponents() {
Container parentContainer = getContentPane();
parentContainer.setLayout(new BorderLayout());
JButton btnExit = new AppExitButton(true);
createTable();
JPanel topPanel = new JPanel(new GridLayout(2, 1));
topPanel.add(btnExit);
topPanel.setBorder(EMPTY_BORDER);
JPanel lowerPanel = new JPanel(new BorderLayout());
JScrollPane jspCmds = new JScrollPane(tblCommands);
lowerPanel.add(jspCmds);
parentContainer.add(topPanel, BorderLayout.NORTH);
parentContainer.add(lowerPanel, BorderLayout.CENTER);
btnExit.addActionListener(evt -> exitForm());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm();
}
});
createAppMenu();
setPosition();
}
private final MyLogger logger = MyLogger.createLogger("rc.log");
private void createAppMenu() {
mbarSettings = new JMenuBar();
JMenu menuSettings = new JMenu("Settings");
menuSettings.add(getThemesMenu());
mbarSettings.add(menuSettings);
setJMenuBar(mbarSettings);
}
public UIManager.LookAndFeelInfo[] getAvailableLAFs() {
return UIManager.getInstalledLookAndFeels();
}
public JMenu getThemesMenu() {
JMenu menu = new JMenu("Theme");
int i = 'a';
int x = 0;
for (UIManager.LookAndFeelInfo l : getAvailableLAFs()) {
JMenuItem mi = new JMenuItem((char) i + SP_DASH_SP + l.getName());
if (i <= 'z') {
mi.setMnemonic(i);
}
int finalX = x;
mi.addActionListener(e -> applyTheme(finalX, l));
menu.add(mi);
i++;
x++;
}
return menu;
}
UIManager.LookAndFeelInfo themeToApply;
public void applyTheme(int idx, UIManager.LookAndFeelInfo lnf) {
themeToApply = lnf;
SwingUtilities.invokeLater(this::applyLnF);
}
public void applyLnF() {
try {
UIManager.setLookAndFeel(themeToApply.getClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(this);
}
private void createTable() {
model = SwingUtils.getTableModel(new String[]{"Col1"});
createRows();
Border borderBlue = new LineBorder(Color.BLUE, 1);
tblCommands = new JTable(model);
}
private void createRows() {
for (int i = 1; i <= 10; i++) {
model.addRow(new String[]{"Row- " + i});
}
}
private void setPosition() {
// Setting to right most position
pack();
GraphicsConfiguration config = getGraphicsConfiguration();
Rectangle bounds = config.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
int x = bounds.x + bounds.width - insets.right - getWidth();
int y = bounds.y + insets.top + 10;
setLocation(x, y);
setVisible(true);
}
/**
* Exit the Application
*/
private void exitForm() {
setVisible(false);
dispose();
logger.dispose();
System.exit(0);
}
}
【问题讨论】:
-
(1-) 之前有人问过这个问题:(stackoverflow.com/questions/65254253/…) 并已删除。建议是提供minimal reproducible example。我还没有看到。
-
顺便说一句,我复制了您的问题,当我从 Nimbus 转到 Metal
JTable行边框时没有正确呈现,但是其他 L&F 不会发生这种情况,并且确实认为这是一个潜在的错误,但是请使用@camickr 所说的最小可重复示例进行更新,也许当您表现出一些努力时,其他人也会这样做 -
正如@DavidKroukamp 所说,这可能是我向 Oracle JDK 团队报告的潜在 jdk 错误。
-
@shaILU 是的,这确实是个 bug,确保你安装了最新的 jdk 8 版本,如果仍然出现,请等待 Oracle 响应
-
@shaILU,现在已删除反对票 - 不要担心反对票,并在第一时间发布适当的问题。顺便说一句,您还没有发布minimal reproducible example (1-)。您仍在使用各种第 3 部分类,因此我们无法复制/粘贴/编译和测试您粘贴的代码。不需要这些课程。创建 JTable 需要一条语句:
new JTable(3, 3);。数据无关紧要。你的问题是关于边界的。
标签: java swing jframe jtable look-and-feel