3

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Leron
  • 9,546
  • 35
  • 156
  • 257
  • The JScrollPane is not designed to handle 2 components, you shouldn't add the button to the scroll pane. I'd rather add 1. have a BorderLayout in the frame 2. `frame.add(table, BorderLayout.CENTER);` 3. add the button into a JToolBar, and then `frame.add(toolBar, BorderLayout.SOUTH);` – ignis Oct 31 '12 at 15:07
  • Why should a button be covering part of the `JTable` header? What is the relevance of that button to the table? – Andrew Thompson Oct 31 '12 at 15:13

5 Answers5

7

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

Aubin
  • 14,617
  • 9
  • 61
  • 84
  • _scrollpane can have only one view_ strictly speaking, that's wrong :-) A scrollPane indeed _has_ several additional views, f.i. the scrollBars, row/column headers, corners. +1 for the suggesting a toolbar – kleopatra Oct 31 '12 at 15:17
  • Ok, JScrollpane can have only one viewport: setViewport(JViewport viewport) – Aubin Oct 31 '12 at 15:25
3

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

Frank
  • 790
  • 5
  • 10
  • Thanks I thought about two panels but my intention is to concentrate on the business logic and just get something that don't look terrible but am also thinking that in real world task I would use 2 panels instead of toolbar. Right now I just want the easiest solution that works with my current code. – Leron Oct 31 '12 at 15:20
2

The issue is that you don't have to call set[Mininum|Maximum|Preferred]Size() methods on Swing components(those methods have no effect if LayoutManager ignores their hints and eventually can broke the layout itself).

This is because in Swing the only responsible for establishing the right size of a Component's children is the LayoutManager. Have a look at this thread for further explanations, or at Oracle's tutorial.

If you need to layout properly your components, add a to your parent Component the right LayoutManager.

Community
  • 1
  • 1
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
1

I will save you alot of hair and time.

When working with swing, one shouldn't look ANY further than GridBagLayout.

Trust me, get to know how to use GridBag. It looks complex, it will break. But it's the only layoutmanager that is useful and worth learning as you might end up using it..... quite a lot.

So, remember - http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Shark
  • 6,513
  • 3
  • 28
  • 50
  • _only layoutmanager that is useful_ simply wrong: there are several 3rd party manager out there which are more useful, more flexible and at the same time more robust and much easier to use - f.i. MigLayout, FormLayout, DesignGridLayout. – kleopatra Oct 31 '12 at 15:22
  • Third parties add dependencies and deployment will be more delicate – Aubin Oct 31 '12 at 15:28
  • @kleopatra out of the default ones - yes, GridBag *is the only layoutmanager that is useful*. He reserves his right to develop a brand new one and show us how you can do things better than gridbag/mig in a less cumbersome way. But none of them is easier for a beginner. – Shark Oct 31 '12 at 16:30
0

If you need a quick way to make GUI, I suggest using the Netbeans GUI code generator, which gives you the ability to drag and drop Java Swing elements. Or you may install the WindowBuilder plugin for Eclipse.

Although these "code generators" are somewhat frowned upon, for you don't need to learn how to format things yourself, they are useful when learning.

Infact, these WYSIWYG editors taught me HTML. :)

turnt
  • 3,235
  • 5
  • 23
  • 39