我有一个错误,我在追踪时遇到了问题,并且想知道是否有人见过这样的事情。我正在使用Java 8构建我的代码。我在Macintosh和Linux上运行/测试此代码。视窗。此错误仅发生在运行Windows操作系统的计算机上。 (还没试过linux)我在Windows 8上测试。
我有一个JComboBox与JTable和JTree在同一个窗口中,我已修改它以添加复选框。 JComboBox最初工作得很好。弹出并正常工作。但是一旦我与JTable或JTree交互,它就不再起作用了。它不再弹出。
当此窗格进一步添加到JTabbedPane时,我可以在此窗格中显示选项卡时再次运行它。回到JComboBox的那个。试图绕过这个bug(并更好地理解它)我尝试使用JPopupMenu而不是JComboBox。错误发生在JPopup上,并且更容易分辨它失败的地方。当我调试时,我发现我确实输入了mousePressed事件,并且mousePressed事件中的一切看起来都很好。但是JPopupMenu.show()函数没有正确执行。
@Override
public void mousePressed(MouseEvent e) {
topDirPopup.show(e.getComponent(), e.getX(), e.getY());
}
我可以显示更多代码,但我不确定哪些代码与弹出窗口交互。
有什么想法吗?有什么工作吗?
将它隔离到JTable(或JTree)的复选框中,但我使用标准的Swing Table,因此更容易隔离。非常基本的代码..不会从文件系统中读取任何内容。不对摆动物体进行任何修改。还是看错了。
我从代码中删除了大部分无用的东西。 主要测试类。
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Test extends JPanel {
Test() {
super(new BorderLayout());
FileSelectionPane fpane = new FileSelectionPane();
JLabel label = new JLabel("File Selection Pane Test");
this.add(fpane, BorderLayout.CENTER);
this.add(label, BorderLayout.NORTH);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("File Selection Test Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Test());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
我的FileTool窗格。是的我在BorderLayout中有一个BorderLayout。在这个例子中这很愚蠢,但我的实际代码更复杂,我在这两个代码之间有一个Tabbed Pane。
import java.awt.BorderLayout;
import java.awt.Panel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class FileSelectionPane extends Panel {
/**
*
*/
private static final long serialVersionUID = 1784004323821479260L;
/**
*
*/
JTable fileTable;
JComboBox<String> topDirComboBox;
FileSelectionPane() {
super(new BorderLayout());
createTopDirectoryComboBox();
FileSelectionTableModel tModel = new FileSelectionTableModel();
fileTable = new JTable(tModel);
JScrollPane jScrollPane2 = new JScrollPane(fileTable);
this.add(jScrollPane2, BorderLayout.CENTER);
this.add(topDirComboBox, BorderLayout.NORTH);
}
void createTopDirectoryComboBox() {
topDirComboBox = new JComboBox<String>();
topDirComboBox.addItem("C:\\Test\\Fake");
topDirComboBox.addItem("C:\\Test\\More");
topDirComboBox.addItem("C:\\Test\\View");
}
public class FileSelectionTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
static final int numCols = 4;
private String[] columnNames = {"..", "Name", "Type", "Size", "Creation Date", "Modification Date"};
private final static int checkCol = 0;
private final static int nameCol = 1;
private final static int typeCol = 2;
private final static int sizeCol = 3;
public int getColumnCount() { return numCols; }
public int getRowCount() { return 5; }
public String getColumnName(int col) {
return columnNames[col];
}
// Only the check item is editable.
public boolean isCellEditable(int row, int col)
{
return (col == checkCol) ? true : false;
}
Boolean tBool[] = new Boolean[10];
public void setValueAt(Object value, int row, int col) {
if (col == checkCol)
tBool[row] = (Boolean) value;
}
public Object getValueAt(int row, int col) {
Object rtnObject = null;
switch (col) {
case checkCol:
rtnObject = (Object) tBool[row];
break;
case nameCol:
rtnObject = (Object) "Testing";
break;
case sizeCol:
rtnObject = (Object) "5";
break;
}
return rtnObject;
}
/* Set up the types of each column */
public Class<?> getColumnClass(int col) {
// getValueAt(0, c).getClass();
Class<?> rtnValue = null;
switch (col)
{
case checkCol:
rtnValue = Boolean.class;
break;
case nameCol:
rtnValue = String.class;
break;
case sizeCol:
rtnValue = String.class;
break;
case typeCol:
rtnValue = String.class;
break;
}
return rtnValue;
}
}
}