I'm reading info from file and put it in a table. The code for this is not mine, but is exactly what I need so I'm trying to keep it and make it useful for me. What I need to add are 5-6 buttons and the functionality for them, but being totally new to working with SWING
I'm having really hard time to complete both - keeping my existing code and adding the buttons on a proper positions.
This is my file, the first part is where the table is made and where I try to include the code for one button:
public class DataFileTable extends JPanel {
public DataFileTable(String dataFilePath) {
JTable table;
DataFileTableModel model;
Font f;
f = new Font("SanSerif",Font.PLAIN,24);
setFont(f);
setLayout(new BorderLayout());
model = new DataFileTableModel(dataFilePath);
table = new JTable();
table.setModel(model);
table.createDefaultColumnsFromModel();
//Try to add button
JButton button = new JButton("First Button");
button.setSize(80,20);
button.setVerticalAlignment(SwingConstants.BOTTOM);
button.setHorizontalAlignment(SwingConstants.RIGHT);
//End button part
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.add(button);//Add button
add(scrollpane);
}
and the second part is mostly the main
function:
public Dimension getPreferredSize(){
return new Dimension(400, 300);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Data File Table");
DataFileTable panel;
panel = new DataFileTable("customers.dat");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
frame.addWindowListener(new WindowCloser());
}
}
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
}
Here is a PrintScreen of what I get with this code:
I'm not looking for the best look and feel, but it would be really nice if I can keep the current code and add the buttons somewhere in the bottom. I tried to add the button directly to the frame form the main
with frame.add(button)
but then even though I have set a size the button takes all the space and the table is no more visible.