這可能是一個模糊的查詢,所以請原諒我。複選框中的某些單元格,但不是全部,在特定的列 - JTable
定製的JTable(我修改了查詢,並將根據提供的SSCCE進行討論)。我必須創建一個JTable,以根據JTable中的選定複選框提供授權。此JTable的目的是向用戶顯示應用程序的所有菜單選項。此JTable有三列: 第一欄:類Bollean(複選框) 第二欄:String類(主菜單項) 第三欄:String類(子菜單項)
來提供授權用戶應選擇相應的複選框到子菜單項,最後選擇「授權」按鈕(我沒有包括授權按鈕,因爲我的授權功能工作正常)
現在UI要求是在JTable的第一列中,我應該顯示而不是在第一列的每個單元格中顯示覆選框(換句話說,不應顯示與主菜單項目對應的複選框)
產品圖如下預期輸出(雖然我得到的所有細胞與複選框第一列)
public class SwingSolution extends JPanel {
public SwingSolution() {
super(new GridLayout(1,0));
String[] columnNames = {"", "Main Menu", "Sub Menu"};
Object[][] data = {
{false, "File", ""},
{false, "", "New"},
{false, "", "Save"},
{false, "", "Close"},
{false, "Edit", ""},
{false, "", "Delete"},
{false, "", "Format"},
{false, "Project", ""},
{false, "", "Create New"},
{false, "", "Delete"},
{false, "", "Build"},
{false, "", "Properties"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
default:
return Boolean.class;
}
}
};
table.getColumnModel().getColumn(0).setMaxWidth(30);
table.getColumnModel().getColumn(1).setMaxWidth(100);
table.getColumnModel().getColumn(2).setMaxWidth(120);
table.setPreferredScrollableViewportSize(new Dimension(250, 195));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SwingSolution newContentPane = new SwingSolution();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我嘗試過各種事情的單元格渲染和GOOGLE上搜索有關的JTable和定製的細胞,但無法弄清楚。任何幫助將不勝感激
在你的數組聲明中有一個額外的逗號(雖然可能並非如此) – 2013-05-09 02:07:50
感謝您的注意..現在編輯(雖然它不會給出任何錯誤或程序行爲的變化) – Mady 2013-05-09 02:11:17
我有一個建議:你可以創建一個靜態表(其實很簡單),然後通過jQuery訪問其元素,例如,在我的在線測驗引擎中(鏈接到演示:http://webinfocentral.com/ )?實現的細節在我的文章中:單擊/選擇ASP.NET GridView或HTML表中的行(http://www.codeproject.com/Tips/209416/Make-the-entire-GridView-Row-clickable-and- selecta) – 2013-05-09 02:35:38