Results 1 to 2 of 2

Thread: J tablw doubt

  1. #1
    shony1989 is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default J tablw doubt

    actually im retrieving data from database and displaying it in jtable after clicking on parts button.but my doubt is ...i have to display 10 rows at a time and its go on as i click on next button.here is my code which i have done so far

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Insets;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    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.Action;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.Icon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JToolBar;
    import javax.swing.SwingConstants;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableColumn;



    public class Example extends JFrame implements ActionListener {

    JFrame frame;
    JButton partsButton;
    JButton nextButton;
    JTable table;
    JPanel panel1;
    JPanel panel2;
    JToolBar toolbar;
    public void action() {

    PartsAction partsAction=new PartsAction("Parts", null,
    "this button displays the parts",new Integer(KeyEvent.VK_L));

    NextAction nextAction=new NextAction("Next", null, "" +
    "next ten rows of parts data", new Integer(KeyEvent.VK_L));

    partsAction.isEnabled();
    frame=new JFrame("Protus");

    toolbar=new JToolBar(JToolBar.HORIZONTAL);
    partsButton=new JButton(partsAction);
    nextButton=new JButton(nextAction);

    partsButton.addActionListener(this);
    nextButton.addActionListener(this);

    Font font=new Font("serif",Font.PLAIN,14);
    partsButton.setFont(font);
    nextButton.setFont(font);
    //button.setForeground(new Color(0xffffdd));

    //button.setMargin(new Insets(0, 0, 0, 0));
    //button.setBorder(null);
    //frame.setLayout(new FlowLayout());


    partsButton.setOpaque(false);

    panel1=new JPanel();
    panel2=new JPanel();

    panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
    //panel1.setBackground(Color.blue);
    panel1.add(partsButton);
    panel2.add(nextButton);

    toolbar.add(partsAction);
    toolbar.setForeground(new Color(0xffffdd));
    toolbar.setBackground(Color.BLUE);

    boolean b=toolbar.isRollover();
    toolbar.setRollover(b);

    frame.getContentPane().add(BorderLayout.WEST,panel 1);
    frame.getContentPane().add(BorderLayout.SOUTH,pane l2);

    frame.setSize(1000,1000);
    frame.setVisible(true);

    add(toolbar,BorderLayout.PAGE_START);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    if(e.getSource()==partsButton)
    {

    //DefaultTableModel model=new DefaultTableModel();
    Vector columnNames = new Vector();
    Vector data = new Vector();
    JPanel panel=new JPanel();
    Connection con = null;
    String url = "jdbc:derby://localhost:1527/myeclipse";
    String driver = "org.apache.derby.jdbc.ClientDriver";
    String user = "classiccars";
    String pass = "classiccars";
    try{
    Class.forName(driver);
    con = DriverManager.getConnection(url, user, pass);

    Statement st=con.createStatement();
    //st.setMaxRows(10);
    ResultSet rs = st.executeQuery("Select * from parts where rownum < 10");
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
    columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next()) {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++){
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    rs.close();
    st.close();
    }
    catch(Exception e1){
    System.out.println(e1);
    }
    table = new JTable(data, columnNames);
    TableColumn col;
    for (int i = 0; i < table.getColumnCount(); i++) {
    col = table.getColumnModel().getColumn(i);
    col.setMaxWidth(250);
    }
    //JScrollPane scrollPane = new JScrollPane( table );

    //panel.add( scrollPane );
    //frame.add(panel);
    //frame.setSize(1000,1000);

    // frame = new JFrame("Protus");
    //JPanel panel = new JPanel();
    //panel.setLayout(new BorderLayout());
    JScrollPane tableContainer = new JScrollPane(table);
    panel.add(tableContainer);
    frame.getContentPane().add(panel);
    //c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));

    frame.add(tableContainer,BorderLayout.SOUTH);
    frame.validate();


    //frame.pack();
    frame.setVisible(true);
    }

    }
    public static void main(String[] args) {

    TableDisplay gui=new TableDisplay();
    gui.action();
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: J tablw doubt

    When you say you want to display 10 rows at a time, what exactly does that mean? Your JTable should only contain 10 rows? Or the scrollable area should only show 10 rows? Something else?

    Code you post should be in the form of an SSCCE. Also, please use the code tags when posting code, otherwise it's unreadable.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Doubt
    By karthikeyan_raju in forum Advanced Java
    Replies: 11
    Last Post: 11-05-2009, 05:48 PM
  2. doubt
    By Chris555 in forum Networking
    Replies: 0
    Last Post: 08-05-2009, 03:58 PM
  3. Doubt
    By winkodes in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 12-20-2008, 09:34 PM
  4. doubt
    By shaju_inspira in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-18-2008, 09:38 AM
  5. doubt in jms
    By veena in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 01-05-2008, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •