2016-05-20 7 views
0

Ich weiß wirklich nicht, was ich falsch mache. Ich habe versucht, Code folgende anderer Leute, aber ich kann nicht scheinen zu verstehen, wie eine Bildlaufleiste hinzufügen, um meine JTableWie scrollbar zu erstellen JTable

Hier ist, was ich bisher:

import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Vector; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.GroupLayout; 
import javax.swing.GroupLayout.Alignment; 
import javax.swing.JButton; 
import javax.swing.LayoutStyle.ComponentPlacement; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableColumn; 
import javax.swing.table.TableModel; 
import javax.swing.JTextArea; 
import javax.swing.JTable; 
import javax.swing.JTextPane; 
import javax.swing.border.BevelBorder; 

public class TBB_SQLBuilder { 

    private JFrame frame; 
    private JTable table; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TBB_SQLBuilder window = new TBB_SQLBuilder(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public TBB_SQLBuilder() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 950, 900); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JTextArea textArea = new JTextArea(); 

     JButton button = new JButton("New button"); 

     table = new JTable(); 

     table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); 
     new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

     GroupLayout groupLayout = new GroupLayout(frame.getContentPane()); 
     groupLayout.setHorizontalGroup(
      groupLayout.createParallelGroup(Alignment.TRAILING) 
       .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup() 
        .addContainerGap() 
        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
         .addComponent(table, GroupLayout.DEFAULT_SIZE, 938, Short.MAX_VALUE) 
         .addGroup(groupLayout.createSequentialGroup() 
          .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE) 
          .addPreferredGap(ComponentPlacement.RELATED) 
          .addComponent(button))) 
        .addContainerGap()) 
     ); 
     groupLayout.setVerticalGroup(
      groupLayout.createParallelGroup(Alignment.LEADING) 
       .addGroup(groupLayout.createSequentialGroup() 
        .addContainerGap() 
        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
         .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE) 
         .addComponent(button)) 
        .addPreferredGap(ComponentPlacement.RELATED) 
        .addComponent(table, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE) 
        .addContainerGap(613, Short.MAX_VALUE)) 
     ); 
     frame.getContentPane().setLayout(groupLayout); 
     frame.add(new JScrollPane(table)); 


     button.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent ae){ 
        String getValue = textArea.getText(); 
        String connectDB = "jdbc:ucanaccess:///Users/sebastianzeki/Documents/PhysJava/My.mdb;"; 
        Connection conn; 
        try { 
         conn = DriverManager.getConnection(connectDB); 
         Statement st =conn.createStatement(); 
         ResultSet rsHNum = st.executeQuery(getValue); 

         table.setModel(buildTableModel(rsHNum)); 
          ((DefaultTableModel)table.getModel()).fireTableDataChanged(); // show changes 

        } catch (SQLException e) { 
         e.printStackTrace(); 
        }     
       } 
      }); 
    } 
    public static DefaultTableModel buildTableModel(ResultSet rs1) 
      throws SQLException { 
     ResultSetMetaData metaData = rs1.getMetaData(); 

     // names of columns 
     Vector<String> columnNames = new Vector<String>(); 
     int columnCount = metaData.getColumnCount(); 
     for (int column = 1; column <= columnCount; column++) { 
      columnNames.add(metaData.getColumnName(column)); 
     } 

     // data of the table 
     Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 

     while (rs1.next()) { 
      Vector<Object> vector = new Vector<Object>(); 
      for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) { 
       vector.add(rs1.getObject(columnIndex)); 

       System.out.println(rs1.getObject(columnIndex)); 
      } 
      data.add(vector); 
     } 
     return new DefaultTableModel(data, columnNames); 
    } 
+0

Also, was ist Ihre Frage? Ich frage nur, weil dein Code gut aussieht. Es fügt * JScrollPane * zum * JFrame * hinzu und dann wird * JTable * zu * JScrollPane * hinzugefügt. – Alexander

+0

Für bessere Hilfe, früher, ein [MCVE] oder [kurze, unabhängige, korrekte Beispiel] (http://www.sscce.org/). –

+0

Ich habe mehr Code hinzugefügt. Ich weiß wirklich nicht, wo das Problem liegt, also musste ich alle Layout-Sachen hinzufügen, falls das das Problem war. Hoffentlich wird nicht von einem Code-Dump beschuldigt werden. –

Antwort

3

Obwohl Sie eine JScrollPane verwenden, um Ihre Tabelle zu platzieren, fügen Sie nicht wirklich die scrollpane zu Ihrem GroupLayout. Stattdessen fügen Sie es der JFrame hinzu - was Ihr erster Fehler ist.

So sollten Sie zuerst scrollpanel erstellen und dann Tabelle hinzufügen; dann fügen Sie die scrollpane-GroupLayout wie unten

JScrollPane scrollPane = new JScrollPane(table); 
// Force the scrollbars to always be displayed 
scrollPane.setHorizontalScrollBarPolicy(
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
scrollPane.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 


GroupLayout groupLayout = new GroupLayout(frame.getContentPane()); 
groupLayout.setHorizontalGroup(
    groupLayout.createParallelGroup(Alignment.TRAILING) 
    .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup() 
     .addContainerGap() 
     .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) 
     .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 938, Short.MAX_VALUE) 

Also, wo immer Sie addComponent(table haben sollten Sie addComponent(scrollPane statt. Sehen wir uns nun zwei Zeilen an. Gleich nach der Erstellung von Scrollpanels.

scrollPane.setHorizontalScrollBarPolicy(
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
scrollPane.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

Diese werden Ihnen das Aussehen geben, das Sie suchen. Bildlaufleisten (deaktiviert), auch wenn nichts gescrollt wird.

+0

Aha. Das funktioniert. Danke vielmals –

2

Gerade JScrollPane erstellen und fügen Sie Tabelle es mit Werten ... siehe Beispiel unten.

public class MainView extends JFrame{ 

private JPanel mainPanel; 
private JTable table; 
private DefaultTableModel model_table; 
private JScrollPane scroll_table; 

public static void main(String[] args) { 
    MainView main = new MainView(); 
    main.setVisible(true); 
} 
public MainView() { 
    mainPanel = new JPanel(null); 
    setSize(500, 500); 

    table = new JTable(); 
    model_table = new DefaultTableModel(); 
    model_table.addColumn("1"); 
    model_table.addColumn("2"); 
    model_table.addColumn("3"); 
    model_table.addColumn("4"); 
    table.setModel(model_table); 

    for(int i=0;i<10;i++){        // add value to table 
      Vector<String> r = new Vector<String>(); 
      r.addElement("a"); 
      r.addElement("b"); 
      r.addElement("c"); 
      r.addElement("d"); 
      model_table.addRow(r); 
    } 

    scroll_table = new JScrollPane(table);   // add table to scroll panel 
    scroll_table.setBounds(5, 10, 300, 150); 
    scroll_table.setVisible(true); 
    mainPanel.add(scroll_table); 

    this.add(mainPanel); 
    } 
} 
+0

Ich habe bereits ein JSCROLLPANE in meinem Code erstellt. Es ist kein Problem, den Tisch zu bevölkern, sondern nur, wie die Leiste zum Anzeigen kommt. Auch wenn ich den Tisch bevölkert gibt es keine Scrillbar –

+0

Bildlaufleiste kommt, wenn Sie Werte an sie übergeben ... – Mohit