Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position properly buttons with SWING/JButton in table/panel/JFrame

Tags:

java

swing

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:

enter image description here

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.

like image 202
Leron_says_get_back_Monica Avatar asked Dec 09 '25 15:12

Leron_says_get_back_Monica


2 Answers

You can't do the following:

JScrollPane scrollpane = new JScrollPane(table);
scrollpane.add(button);//Add button 
add(scrollpane);

because the scrollpane can have only one Viewport which display the table.

I suggest:

add(scrollpane, BorderLayout.CENTER );

and

JToolBar toolbar = new JToolBar();
toolbar.add( button1 );
toolbar.add( button2 );
toolbar.add( button3 );
...
add( toolbar, BorderLayout.NORTH );

Se the documentation for JToolBar

like image 125
Aubin Avatar answered Dec 12 '25 03:12

Aubin


Use the swing layout managers : http://zetcode.com/tutorials/javaswingtutorial/swinglayoutmanagement/

I recommend you to use 2 panels: one for your table and another for your buttons using a proper layout like i said above

like image 40
Frank Avatar answered Dec 12 '25 04:12

Frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!