Thursday, 26 April 2018

java - JTable Won't Show On JPanel

Hi I have created a Jtable and can get it to show on my frame yet not on the JPanel I have ontop of my JFrame. I can't seem to change


import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.
;
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.;
import javax.swing.table.
;

public class Main
{
DefaultTableModel table_model;
String[][] addressData = new String[10][5];
JPanel panel;
JFrame frame;
JButton loadData;
int count,index,row =0;
String thisLine;
ArrayList People = new ArrayList();


public Main()
{
//Creating JFrame and setting properties
frame = new JFrame();
frame.setResizable(false);
frame.setTitle("Address Book");
frame.setSize(800,600);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
//Creating JPanel and setting properties
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(new Color(77,81,84));
//Setting the table and Scroll Bars
this.table_model = new DefaultTableModel(addressData, new String[]{"First Name", "Surname", "Home Number", "Mobile Number", "Address", "Postcode"});
JTable table = new JTable(this.table_model);
table.setBounds(130, 40, 200, 200);
panel.add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
//Load Data button, reading file in and adding data to ArrayList then Array of Arrays
loadData = new JButton("Load File");
loadData.setBounds(10, 10, 100, 20);
loadData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
BufferedReader reader = new BufferedReader(new FileReader("file/address.buab"));
while ((thisLine = reader.readLine()) !=null)
{
if (row >= 4)
{
index++;
row = 0;
People.add(thisLine);
addressData[index][row] = People.get(count);
count++;
row++;
}
else
{
People.add(thisLine);
addressData[index][row] = People.get(count);
count++;
row++;
}
}
reader.close();
}
catch (IOException ex)
{
System.err.println("Input Exception, Check address.buab File");
}
}
});
panel.add(loadData);
//Auto sort on table fields
TableRowSorter sorter = new TableRowSorter(this.table_model);
sorter.setComparator(1, new Comparator()
{
public int compare(Integer o1, Integer o2)
{
return o1.compareTo(o2);
}
public boolean equals(Object obj)
{
return obj.equals(this);
}
});
table.setRowSorter(sorter);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
public static void main(String[] args)
{
new Main();
}

}


Any ideas on how I might go about setting the table visible ontop of my JPanel

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...