-1

I need to have 1.the JButton open the JTable in the Journal class 2.transfer the ints in the JTextFields to the variables a and b in the Journal Class

I used netbeans to create the Main class and my own methods to create the table class please help! Thanks! This is the part of the Main class Netbeans tells me to edit when I right click > Events > Action > Action Performed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     int data1 = Integer.parseInt(jTextField1.getText());
     int data2 = Integer.parseInt(jTextField2.getText());

}   

Here is my Code:

package Components;

/**
 *
 * @author dustinpx2014
 */

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Journal extends JPanel
{
    private JTable table;
    private int a;//Number of Students
    private int b;// Length of Trip
    public Journal() 
    {

        String colN1 = "Date";
        String colN2 = "Student"; 

        int c = a*2/b; // Determining # of Journals
        int col = c*2; // Creating two columns for every journal(Day and Student)
        String[] columnNames = new String[col]; //For Loop: inserting column names
        for(int colF = 0; colF < col; colF++)
        {
            if(colF%2 == 0)
            {
                columnNames[colF] = colN1;
            }
            else
            {
                columnNames[colF] = colN2;
            }
        }
        int row = b; //row = number of days
        int d = 1; //day number
        int s = 1; //student number
        int x = 0; //counter for the # of times students write
        int z = 1; // student number(no limit)
        int z1 = a/2; // student number 2
        int x1 = 0; // counter for the # of times students write 2
        int x2 = 0;
        int w = 1;
        Object[][] data = new Object[row][col];

        for (int col1 = 0; col1 < data[0].length; col1++)
        {
            for (int row1 = 0; row1<data.length; row1++)//goes through the table by column
            {
                if(d > b) //reseting the date
                {
                    d = 1;
                }
                if(s > a && x <= 2) //reseting student number and adds count
                {
                    s = 1;
                    x++;
                }
                if(z > a)
                {
                    z = 1;
                }
                if(z1 > a && x1 <= 2)
                {
                    z1 = 1;
                    x1++;
                }
                if (w>a)
                {
                    w++;
                }
                if(col1%2 == 0) //inserts the day
                {
                    data[row1][col1]= "Day " + d;
                    d++;
                }
                else if (s!=data[row1][col1])//inserts student number
                {
                    data[row1][col1]= s;
                    s++;
                    z++;
                    w++;
                }
                for (int col2 = 1; col2 < data[0].length; col2++)
                {
                    for (int row2 = 0; row2<data.length; row2++)//goes through the table by column
                    {
                        if(z == data[row2][col2] && col2%2!=0)//checks for repeats and replaces them
                        {

                            for (int y = 0; y < col; y++) //checking all columns
                            {
                                if (z1 == a/2 && x1 <= 5) //adds count
                                {
                                    x1++;
                                }
                                else if(z1 > a && x1 <= 5)
                                {
                                    z1 = 1;
                                    x1++;

                                }
                                if(z == data[row2][y])
                                {
                                    data[row2][col2] = z1;
                                    z1++;
                                    w++;
                                }

                            }
                        }
                    }
                }
                for (int row3 = 1; row3 < data.length; row3++)
                {
                    for (int col3 = 0; col3<data[0].length; col3++)//goes through the table by rows
                    {
                        if(w == data[row3][col3] && col3%2!=0)//checks for repeats and replaces them
                        {
                            for(int y2 = 0; y2 < col; y2++)
                            {
                                if(
                                row3<row-1 && 
                                row3> 1 && 
                                w==data[row3+1][y2] && 
                                w==data[row3-1][y2] &&
                                w==data[row3][y2]
                                ) //checks rows
                                {
                                    data[row3][col3] = 0;

                                }
                            }
                        }
                    }
                }
            }
        }

        JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    public JTable getTable() {
        return table;
    }

    private static void gui()
    {
        JFrame gui = new JFrame();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setTitle("Journal List");
        gui.setSize(700,200);
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
        gui.add(new Journal());
    }   

Please help! thanks :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    You've told us "needs" but have yet to ask a specific question. Please clarify for us just what is tripping you up as well as give us more pertinent details of your problem. For example, what class holds your JButton's ActionListener? How is the Journal class associated with this class? What do you mean by "open the JTable" since this does not have obvious meaning? Again, please clarify since we can only understand that which you tell and show us and nothing more. – Hovercraft Full Of Eels Feb 10 '14 at 17:06
  • I have two classes Main with JTextFields and Jbutton and Journal with a Jtable extending JPanel. I need to use the Main class to operate the Journal class how do you use JButton to open up the JPanel and how do you use the variables in JTextField and apply them to variables a and b? – user3286337 Feb 10 '14 at 17:10
  • This has nothing to do with Netbeans so don't add it in the title. There is also no need to add the major tag in the title. – Andrew Thompson Feb 11 '14 at 00:24

1 Answers1

2

I have two classes Main with JTextFields and Jbutton and Journal with a Jtable extending JPanel. I need to use the Main class to operate the Journal class how do you use JButton to open up the JPanel and how do you use the variables in JTextField and apply them to variables a and b?

I assume that your Main class has a Journal field, say called journal, and that this JPanel has been added to your main. To call methods of your Journal instance, simply call methods on the journal variable. For instance Journal should have public getter methods that would allow Main to query the data held by its fields.

Note that I don't see any JTextFields in Journal, but if Journal were my class, again, I'd give it a public method to allow it to get information out of the JTable, and in that method, I'd call one of JTable's methods to get the information that your Journal method could return. A decent JTable method you could use is the getValueAt(int row, int column). In fact your Journal method could simply wrap that method:

public class Journal {
  private JTable table;

  // ...... lots more code

  public Object getValueAt(int row, int column) {
    // first check that row and column are not out of bounds, and deal with it
    return table.getValueAt(row, column);
  }
}

Now if you're also looking to display this JTable on button press, then you will need to add it to one of the containers in your Main class, and then call revalidate and repaint on the same container after changing its contents. It's important to know and understand the layout manager used by that container as well since some accept new components with greater ease than others.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373