如何将自定义 JTable 代码添加到 GUI Builder Nebeans?

发布于 2024-10-11 12:18:30 字数 1222 浏览 1 评论 0原文

如何将自定义 Jtable 添加到 GUI Builder netbeans 表单

    JPanel panel = new JPanel();
    String data[][] = {{"Vinod","MCA","Computer"},
                      {"Deepak","PGDCA","History"},
                      {"Ranjan","M.SC.","Biology"},
                      {"Radha","BCA","Computer"}};
    String col[] = {"Name","Course","Subject"};    
    DefaultTableModel model = new DefaultTableModel(data, col);
    table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    Object obj1 = GetData(table, 2, 2);
    System.out.println("Cell value of 3 column and 3 row :" + obj1);
    Object obj2 = GetData(table, 2, 1);
    System.out.println("Cell value of 2 column and 3 row :" + obj2);
    panel.add(pane);
    frame.add(panel);
    frame.setSize(500,150);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public Object GetData(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index, col_index);
  }  
}

how can add custom Jtable to GUI Builder netbeans form

    JPanel panel = new JPanel();
    String data[][] = {{"Vinod","MCA","Computer"},
                      {"Deepak","PGDCA","History"},
                      {"Ranjan","M.SC.","Biology"},
                      {"Radha","BCA","Computer"}};
    String col[] = {"Name","Course","Subject"};    
    DefaultTableModel model = new DefaultTableModel(data, col);
    table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    header.setBackground(Color.yellow);
    JScrollPane pane = new JScrollPane(table);
    Object obj1 = GetData(table, 2, 2);
    System.out.println("Cell value of 3 column and 3 row :" + obj1);
    Object obj2 = GetData(table, 2, 1);
    System.out.println("Cell value of 2 column and 3 row :" + obj2);
    panel.add(pane);
    frame.add(panel);
    frame.setSize(500,150);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public Object GetData(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index, col_index);
  }  
}

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

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

发布评论

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

评论(1

阳光下的泡沫是彩色的 2024-10-18 12:18:30

好的,现在您已经提供了一些详细信息,这就是您的答案。
有多种方法可以做到这一点。

一:使用 GUI

  • 将 JTable 组件放置在 GUI 设计器上后,
  • 右键单击表格单元格(JTable 的白色区域),
  • 从弹出菜单中选择“Table Contents..” - 这将显示定制器对话框。
  • 在“表模型”选项卡中选择“自定义代码”。
  • 将以下代码放入文本框中:

    new javax.swing.table.DefaultTableModel( new Object [][] {
        {"Vinod","MCA","计算机"},
        {"迪帕克","PGDCA","历史"},                
        {"Ranjan","M.SC.","生物学"},
        {"拉达","BCA","计算机"}},
        new String [] {"姓名","课程","主题"})
    
  • 保存更改。
    (现在请注意,jTable 可能无法在设计器上正确显示,但这没关系)

二:使用代码片段

  • 将 JTable 组件放置在 GUI 设计器上后,
  • 右键单击表格单元格( JTable 的白色区域)
  • 从弹出菜单中选择属性 - 这显示属性设置
  • 在属性对话框上,从
  • 网格上的顶部按钮(属性、绑定、事件、代码)切换到代码,单击编辑按钮(标记为“. ..')反对“全部设置代码后”选项。
  • 这将允许您在表格全部设置后编辑一些代码。将以下代码放入该编辑器中(假设您的表名称为 jTable1)

     jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {
                  {"Vinod","MCA","计算机"},
                  {"迪帕克","PGDCA","历史"},
                  {"Ranjan","M.SC.","生物学"},
                  {“拉达”,“BCA”,“计算机”}
        },
        新字符串[] {
            “姓名”、“课程”、“科目”
        }));
        jTable1.getTableHeader().setBackground(java.awt.Color.yellow);
    

注意:这也涵盖了颜色更改,因此此处可以更好地控制。

  • 保存您的更改

Ok, Now that you have provided some details, Here is the answer for you.
There are multiple ways to do this.

one: using GUI

  • Once you place the JTable component on the GUI designer,
  • Right-click on the table cells( white area of the JTable )
  • From popup menu, choose "Table Contents.." - this shows a customizer dialog.
  • Choose "Custom code" in "Table Model" tab.
  • Place the following code to the text box:

    new javax.swing.table.DefaultTableModel( new Object [][] {
        {"Vinod","MCA","Computer"},
        {"Deepak","PGDCA","History"},                
        {"Ranjan","M.SC.","Biology"},
        {"Radha","BCA","Computer"}},
        new String [] {"Name","Course","Subject"})
    
  • Save your changes.
    (Now notice that the jTable may not display properly on your designer, but that is OK)

two: using code fragments

  • Once you place the JTable component on the GUI designer,
  • Right-click on the table cells( white area of the JTable )
  • From popup menu, choose properties - this shows property settings
  • On the properties dialog, switch to code from top buttons ( properties, binding,events,code)
  • on the grid, click on the edit button(marked '...') against "After All-set Code" option.
  • This will allow you to edit some code after table is all set. Place the following code in to that editor (assuming your table's name is jTable1)

       jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {
                  {"Vinod","MCA","Computer"},
                  {"Deepak","PGDCA","History"},
                  {"Ranjan","M.SC.","Biology"},
                  {"Radha","BCA","Computer"}
        },
        new String [] {
            "Name","Course","Subject"
        }));
        jTable1.getTableHeader().setBackground(java.awt.Color.yellow);
    

note: this covers the color change as well, so better control here.

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