2014-05-19 50 views
-1

因此,在对我的控件进行布局之后,我发现我的表似乎不显示。 标签(l1),组合框和提交按钮以我想要的方式显示,即使控制台消息工作正常。只是在搜索后应该出现的Jtable不会显示。JTable在JFrame中仅显示一次用于搜索查询

错误在哪里,应该如何纠正。这是我的代码: -


import java.awt.*; 
import java.awt.event.*; 
import java.io.File; 
import java.sql.*; 
import java.util.Vector; 

import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 

public class r_search_2 extends JFrame implements ActionListener { 

    JFrame frame1; 
    JLabel l0, l1, l2; 
    JComboBox c1; 
    JButton b1; 
    Connection con; 
    ResultSet rs, rs1; 
    Statement st, st1; 
    PreparedStatement pst; 
    String ids; 
    static JTable table = new JTable();; 
    String[] columnNames = {"SECTION NAME", "REPORT NAME", "CONTACT", "LINK"}; 
    String from; 
    Vector v = new Vector(); 
    JMenuBar menu = new JMenuBar(); 


    JPanel mainPanel = new JPanel(new BorderLayout()); 
    JPanel topPanel = new JPanel(new FlowLayout(SwingConstants.LEADING, 60,25)); 


    r_search_2() 
    { 

     b1 = new JButton("submit"); 

     //l0.setBounds(100, 50, 350, 40); 
     l1.setBounds(75, 110, 75, 20); 
     b1.setBounds(150, 150, 150, 20); 
     b1.addActionListener(this); 

     topPanel.add(l1); 

     try 
     { 

      File dbFile = new File("executive_db.accdb"); 
      String path = dbFile.getAbsolutePath(); 
      con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ= " + path); 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      st = con.createStatement(); 
      rs = st.executeQuery("select index_name from Index1"); 
      while (rs.next()) 
      { 
       ids = rs.getString(1); 
       v.add(ids); 

      } 
      c1 = new JComboBox(v); 
      c1.setEditable(true);c1.setSelectedItem(""); 
      c1.setBounds(150, 110, 150, 20); 


      topPanel.add(c1); 
      topPanel.add(b1); 
      mainPanel.add(topPanel, BorderLayout.PAGE_START); 

      st.close(); 
      rs.close(); 
     } catch (Exception e) { 
     } 
     // setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     if (ae.getSource() == b1) { 
      showTableData(); 
     } 
    } 

    public void showTableData() 
    { 

     DefaultTableModel model = new DefaultTableModel(); 
     model.setColumnIdentifiers(columnNames); 

     table.setModel(model); 
     table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
     table.setFillsViewportHeight(true); 
     JScrollPane scroll = new JScrollPane(table); 
     scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
     from = (String) c1.getSelectedItem(); 

     String section_name = ""; 
     String report_name = ""; 
     String contact_name = ""; 
     String link = ""; 


     try 
     { 

     pst = con.prepareStatement("select distinct Section.Section_Name,Report.Report_Name,Report.Link,Contact.Contact_Name " 
         + "FROM ((Section INNER JOIN Report ON Report.Section_ID=Section.Section_ID) INNER JOIN Contact ON Contact.Contact_ID=Report.Contact_ID) LEFT JOIN Metrics ON Metrics.Report_ID=Report.Report_ID " 
         + " WHERE Section.Section_Name LIKE '%"+from+"%' OR Report.Report_Name LIKE '%"+from+"%' OR Metrics.Metric_Name LIKE '%"+from+"%' OR Contact.Contact_Name LIKE '%"+from+"%' "); 
      ResultSet rs = pst.executeQuery(); 
      int i = 0; 
      while (rs.next()) { 
       section_name = rs.getString("Section_Name"); 
       report_name = rs.getString("Report_Name"); 
       contact_name = rs.getString("Contact_Name"); 
       link = rs.getString("Link"); 
       model.addRow(new Object[]{section_name, report_name, contact_name, link}); 
       i++; 
      } 
      if (i < 1) { 
       JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE); 
      } 
      if (i == 1) { 
       System.out.println(i + " Record Found"); 
      } else { 
       System.out.println(i + " Records Found"); 
      } 
     } catch (Exception ex) { 
      JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
     } 
     mainPanel.add(scroll); 
     mainPanel.revalidate(); 
     mainPanel.repaint(); 

    } 

    public static void main(String args[]) { 


     r_search_2 s=new r_search_2(); 
     //new r_search_2(); 
     JFrame fr=new JFrame("Search Executive Reports"); 
     //fr.add(s.getUI()); 
     fr.add(s.mainPanel); 
     fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     fr.setLocationByPlatform(true); 
     fr.setSize(1000, 400); 
     //fr.pack(); 
     fr.setVisible(true); 

     // new r_search_2(); 
    } 
} 
+0

你可以尝试指定的BorderLayout对部分你想要哪张桌子;我认为它应该默认为中心,但没有具体的损害。 – arcy

回答

4

总评:

  1. 使用合理的变量名,如果你希望人们花时间阅读你的代码的时间。 “b1”,“l1”没有任何意义。
  2. 摆脱所有setBounds()声明。您的面板使用FlowLayout,FlowLayout将确定组件的大小/位置。

下次我输入任何东西进行搜索时,它都没有出现。

您正在将该表添加到BorderLayout的CENTER中。所以你现在有两个表格显示在CENTER中。 Swing以与表格添加相反的顺序绘制表格。所以第二张桌子首先被绘制,第一张桌子被绘制第二张。所以第一张桌子被绘在第二张桌子上面。

因此,您需要先删除旧的scollpane,然后再添加第二个滚动条。

但是有一个更简单的解决方案。正如谢尔盖已经提出只需要创建一个新的TableModel,并在现有的表更换为TableModel:

table.setModel(theNewModel); 

所以真的应该创建JTable中和滚动面板和它在创建初始的GUI添加到您的框架。然后,您的搜索逻辑将创建一个新的DefaultTableModel,并使用上面的代码更新表格。

编辑:

在您创建的所有Swing组件的基本代码可能是这样的构造:

mainPanel.add(topPanel, BorderLayout.PAGE_START); 
JScrollPane scrollPane = new JScrollPane(table); // added 
mainPanel.add(scrollPane, BorderLayout.CENTER); // added 

现在你已经创建并添加一个空表到你的GUI。模型是空的,所以没有什么可显示的。

现在在您的showTableData()方法中,您不需要创建更多的Swing组件,因此请删除创建该滚动窗格的所有这些代码行并将该scrollpane添加到该框架。

剩余的代码创建模型并将其添加到表中,然后将数据添加到模型中,以使其保持原样。尽管效率更高,但您可能希望将table.setModel(...)语句移动到方法的末尾,以便在所有数据加载到模型中后才更新表格。

+0

非常有帮助的指导方针!但是我仍然对你的更简单的解决方案感到困惑,那就是创建一个新的TableModel并替换现有表中的TableModel。真的很困惑,我应该在我的代码中修改。如果你能在我的代码中显示我,这将是非常好的。谢谢 ! – user3608233

+0

@ user3608233,请参阅编辑。 – camickr

+0

非常感谢你!我的问题终于解决了。只需添加行JScr​​ollPane scroll = new JScrollPane(table);在构造函数中修复了这个问题!非常感谢 ! :-) – user3608233

2

这是一个不好的做法,但你可以做以下

mainPanel.add(scroll); 
    mainPanel.revalidate() 
    mainPanel.repaint(); 

好的做法是使用CardLayout空面板时没有搜索结果必须证明。

+0

:感谢您的快速回复!所以我在代码中添加了上面的代码。 Jtable确实出现,但仅用于第一个搜索字符串。下次我输入任何东西进行搜索时,它都不会出现。我应该如何纠正? – user3608233

+0

您应该只添加一次表格。下次你应该在表格中设置另一个表格模型。但你应该看到我的另一个建议(使用CardLayout) –

+0

@Jarrod Roberson:在我看来,这不是一个重复的问题。请看看 – user3608233

0

的你做了什么

改进只是一个快速的重新设计:我很快就试图将来自所述码位的界面,希望这有助于

public class r_search_2 extends JFrame implements ActionListener { 

r_search_2(String Title) 
{ 
    super(Title); 

    init(); 
} 

private void init(){ 
    b1 = new JButton("submit"); 
    b1.addActionListener(this); 


    c1 = new JComboBox(v); 
    c1.setEditable(true);c1.setSelectedItem(""); 
    c1.setBounds(150, 110, 150, 20); 

    topPanel.add(c1); 
    topPanel.add(b1); 
    mainPanel.add(topPanel, BorderLayout.PAGE_START); 

    DefaultTableModel model = new DefaultTableModel(); 
    model.setColumnIdentifiers(columnNames); 

    table.setModel(model); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
    table.setFillsViewportHeight(true); 
    JScrollPane scroll = new JScrollPane(table); 
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    from = (String) c1.getSelectedItem(); 

    String section_name = ""; 
    String report_name = ""; 
    String contact_name = ""; 
    String link = ""; 

    mainPanel.add(scroll); 
    this.add(mainPanel); 
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    this.setLocationByPlatform(true); 
    this.setSize(1000, 400); 
    this.setVisible(true); 

} 


public static void main(String args[]) { 


    r_search_2 s=new r_search_2("Search Executive Reports"); 

} 
} 
+0

你应该注意你所做的改进。否则,这不是一个答案 –

+0

@peeskillet编辑 – QGA

+0

@QuentinTanioartino:感谢您的意见。但是我的问题依然存在。你能帮我修理吗?谢谢 ! – user3608233