خرید بک لینک

Vote count: -2

I am working on a class assignment which requires me to dynamically make buttons dependent on database information. The array value [i,0] is what pulls the name value for each of these created buttons. My next step is to cause each of these buttons to pull from a different array location in the database, [i,1], and when clicked, set a text area to that text. I am having issues tying the array location to a button to repeatedly call this array location as I have a series of buttons that the user will be able to click on at different times, but get the same answer from a button they have clicked before. I have tried a couple of things but might have not done them correctly so am open to anything that might work.

package yourstrulyls.History;

 //Imports
 import YT.Brand;
 import YT.Nav;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.sql.*;

 public class ButtonGenerator extends Panel {
 public ButtonGenerator(int inc, int pz) //Increments and page size
 {
    //Links user to database to pull data.
    String url = "jdbc:mysql://localhost:3306/yourstruly";
    String userid = "root";
    String password = "yoast";
    String sql = "SELECT * FROM lees_summit_history";

    //Begins try catch statement
     try (Coection coection = DriverManager.getCoection( url, userid, password );
         Statement stmt = coection.createStatement();
         ResultSet rs = stmt.executeQuery( sql ))
     {
             ResultSetMetaData md = rs.getMetaData();
             int columns = md.getColumnCount();  

             rs.last();
             int RowCount = rs.getRow();

             String db[][] = new String[RowCount][columns];

             //Gets data from table
             rs.beforeFirst();//get curser before first row.
             String FV;
             String SV;

             //Sets table data to a variable
             for (int x = 0; x < RowCount; x++)
             {
                rs.next();
                FV = rs.getString(1);
                SV = rs.getString(2);
                db[x][0] = FV;
                db[x][1] = SV;
             }

             int increment = Integer.toString(inc).isEmpty() ? 1: inc; //Used to bring up pages at a time
             int pagesize = Integer.toString(pz).isEmpty() ? 10: pz; //Values per page
             int i; //Variable that controls loops
             JButton JButtonC[], BackButton; //Buttons you'll need

             //Sets up GridBag constraints
             GridBagConstraints c = new GridBagConstraints();
             setLayout(new GridBagLayout());
             JScrollPane historyscroll = new JScrollPane(); //Scrollpane
             historyscroll.setVisible(true);
             c.anchor = GridBagConstraints.PAGE_START;
             c.fill = GridBagConstraints.BOTH;
             c.gridwidth = GridBagConstraints.REMAINDER;
             c.gridheight = 1;
             c.weightx = 3;
             c.weighty = 2;
             add(historyscroll, c);

             //Creates panel for textArea display
             JPanel list = new JPanel();
             historyscroll.setViewportView(list); //list will be the info that is stored.
             historyscroll.setBackground(Brand.White);
             list.setLayout(new GridBagLayout());

             //Dynamic JButton creation
             JButtonC = new JButton[db.length]; //Buttons per database entry

             // This will set the min/max button on the specific page
             for (i = 0; //DB Size - (Page Before * # Per Page)
                  i<= db.length-1; i++ // DB Size - (Page On * # Per Page)
                 )
             {
                 //Constraints for button creation
                 setBackground(Brand.White);
                 JButtonC[i] = new JButton();
                 c.gridwidth = 1;
                 c.weightx = 1;
                 c.anchor = GridBagConstraints.WEST;
                 c.weighty = 1;
                 c.gridheight = GridBagConstraints.REMAINDER;
                 JButtonC[i].setText(db[i][0]);
                 JButtonC[i].setBackground(Brand.Marigold);
                 list.add(JButtonC[i], c);

             //End of loop
             }         

          System.out.println(increment);
          //Dynamically displays text in textArea       
          JTextArea JTextAreaC = new JTextArea();
          JTextAreaC.setBackground(Brand.White);
          JTextAreaC.setVisible(true);
          JTextAreaC.setEditable(false);
          JTextAreaC.setLineWrap(true);
          c.gridheight = 3;
          c.weighty = 7;       
          add(JTextAreaC, c);

          //Back Button
          BackButton = new JButton("Back");

          if (increment > 1) // Make sure you can go back
          {
          c.anchor = GridBagConstraints.SOUTHWEST;  
          c.gridwidth = 1;
          c.gridheight = 1;
          c.weighty = 1;
          c.weightx = 1;
          add(BackButton, c);
          }

          // Calls to go back a page
          BackButton.addActionListener((ActionEvent e) -> {
              Back(increment, pagesize);
             });
     }

     catch (SQLException e)
     {
        System.out.println( e.getMessage() );
     }
 }

 public static void Back(int inc, int pz)
 {
    ButtonGenerator ButtonGenerator = new ButtonGenerator(inc-1, pz); //Just decrease the increments, that's all
    Nav.GoTo(ButtonGenerator);   
 }

}

asked 4 mins ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 30 فروردين 1395 ساعت: 21:43

صفحه بندی