My problem is making my Main class and Journal class connect together, in the Main class users will input two variables and click the button to create the table, in the Journal class it will read the two variables.
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) {
// TODO add your handling code here:
}
and my Journal class, I tried making it read JTextField 1 and 2 but I do not know how to combine them.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Journal extends JPanel
{
public Journal()
{
String colN1 = "Date";
String colN2 = "Student";
int a = Integer.parseInt(newJournal.jTextField1.getText());
int b = Integer.parseInt(newJournal.jTextField2.getText());
int c = a*2/b;
int col = c*2;
String[] columnNames = new String[col];
for(int colF = 0; colF < col; colF++)
{
if(colF%2 == 0)
{
columnNames[colF] = colN1;
}
else
{
columnNames[colF] = colN2;
}
}
int row = b;
int d = 1;
int s = 1;
int x = 0;
Integer f = new Integer(1);
Object[][] data = new Object[row][col];
for (int col1 = 0; col1 < data[0].length; col1++)
{
for (int row1 = 0; row1<data.length; row1++)
{
if(d > b)
{
d = 1;
}
else if(s > a || x > 2)
{
s = 1;
x++;
}
else if (row1 > row)
{
row1 = 0;
}
else if(col1%2 == 0)
{
data[row1][col1]= "Day " + d;
d++;
}
else
{
data[row1][col1]= s;
s++;
}
}
}
for (int col2 = 0; col2 < data[0].length; col2++)
{
for (int row2 = 0; row2<data.length; row2++)
{
if(s > a || x > 2)
{
s = 1;
x++;
}
else if(s!=data[row2][col2]&&col2%2!=0)
{
data[row2][col2] = s;
s++;
}
}
}
for (int row3 = 0; row3<data.length; row3++)
{
for (int col3 = 0; col3 < data[0].length; col3++)
{
int rowA = row3 + 1;
int rowS = row3 - 1;
int rows = 1;
int colA = col3 + 1;
int colS = col3 - 1;
int cols = 1;
if(s > a || x > 2)
{
s = 1;
x++;
}
else if(s==data[rowA][cols] && s == data[rowS][cols])
{
cols=cols+2;
}
else if (s == data[rows][colA] && s == data [rows][colS])
{
rows++;
}
else if(s!=data[row3][col3]&&col3%2!=0)
{
data[row3][col3] = s;
s++;
}
}
}
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
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());
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
gui();
}
});
}
}
Currently your JTable
is locally scoped in the Journal
constructor, so it can't be accessed from anywhere else. What you want to do is declare it as a class-member and have a getter
for it.
public class Journal extends JPanel {
private JTable table;
public Journal() {
table = new JTable(...);
}
public JTable getTable() {
return table;
}
}
Now the table
can be accessed and retrieved from the Main
class using the getTable()
.
Also you'll want to use a TableModel
to update data dynamically. Yo can use a simple, already implemented DefaultTableModel . You want to set this model to the JTable
. Something like this
public Journal() {
...
DefaultTableModel model = new DefaultTableModel(data, columns);
table = new JTable(model);
}
So you have everything set in for your Main
class. In your actionPerfomed
you can do something like this
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String data1 = textField1.getText();
String data2 = textField2.getText();
JTable table = journal.getTable();
DefaultTableMode model = (DefaultTableModel)table.getModel();
model.addRow( new Object[] { data1, data2 } );
}
Side Notes
main
methods, as I assume the Main
class already has a main
method. Journal
class use a JFrame
in its main
method, and I assume your Main
class is a JFrame
also. What you can do is just make Journal
a JDialog
and add the JTable
to it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.