tags:

views:

44

answers:

2

Hi, i am very stuck. Hopefully a kind person on here can help me with a line of code to make my jtable sretch a little further across the panel its in. At the moment it just stays really small in the middle of the screen! I have tried many approaches, just recently failing at trying to change the widths of the columns (as the page / gui will be of fixed size anyway). Anyway, the code for the JTable is at the bottom. I hope you can be of assistance! Its just one method and class so its easy to copy and paste and run (if you can be bothered that is, i understand if you cant!)

import javax.swing.*;
import javax.swing.table.TableColumn;

import java.awt.*;

public class second extends JFrame
{
 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 public second()
 {

 }

 public static void main(String[] args)
 {
  second sec = new second();
  sec.createFrame();
 }

 @SuppressWarnings("deprecation")
 private void createFrame()
 {
  JFrame frame = new JFrame("Second attempt");
  JPanel content = new JPanel(new BorderLayout(5,10));
  frame.getContentPane().add(content);
  frame.setResizable(false);

  JPanel requestor = new JPanel(new GridLayout(1, 4, 15, 20));
  content.add("North", requestor);  
  requestor.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Requestor"));

  JPanel col1 = new JPanel();
        BoxLayout boxLayout1 = new BoxLayout(col1, BoxLayout.Y_AXIS);
        col1.setLayout(boxLayout1);


        JPanel col2 = new JPanel();
        BoxLayout boxLayout2 = new BoxLayout(col2, BoxLayout.Y_AXIS);
        col2.setLayout(boxLayout2);


        JPanel col3 = new JPanel();
        BoxLayout boxLayout3 = new BoxLayout(col3, BoxLayout.Y_AXIS);
        col3.setLayout(boxLayout3);

        JPanel col4 = new JPanel();
        BoxLayout boxLayout4 = new BoxLayout(col4, BoxLayout.Y_AXIS);
        col4.setLayout(boxLayout4);

        requestor.add(col1);
        requestor.add(col2);
        requestor.add(col3);
        requestor.add(col4);    

        //create JLabels
        JLabel requestorId = new JLabel("RequestorID");    
        JLabel org = new JLabel("Organisation");
        JLabel orgAddress = new JLabel("Organisation Address");
        JLabel otherAdd = new JLabel("Other Address");
        JLabel title = new JLabel("Title");
        JLabel phoneNo = new JLabel("Phone Number");
        JLabel firstName = new JLabel("First Name");
        JLabel surname = new JLabel("Surname");
        JLabel emailAdd = new JLabel("E Mail Address");
        JLabel dept = new JLabel("Department");
        JLabel fax = new JLabel("Fax Number");
        JLabel spacer = new JLabel("Spacer");


        //create text areas
        JTextField reqIDTxt = new JTextField();


        JTextField orgTxt = new JTextField();
        JTextField orgAddTxt = new JTextField();
        JTextField otherAddTxt = new JTextField();
        JTextField titleTxt = new JTextField();
        JTextField phoneNoTxt = new JTextField();
        JTextField firstNameTxt = new JTextField();
        JTextField faxNoTxt = new JTextField();
        JTextField surnameTxt = new JTextField();
        JTextField emailTxt = new JTextField();
        JTextField deptTxt = new JTextField();
        JTextField spacerTxt = new JTextField();


        col1.add(requestorId);
        col1.add(reqIDTxt);
        col1.add(title);
        col1.add(titleTxt);
        col1.add(firstName);
        col1.add(firstNameTxt);
        col1.add(surname);
        col1.add(surnameTxt);
        col1.add(dept);
        col1.add(deptTxt);

        col2.add(org);
        col2.add(orgTxt);
        col2.add(phoneNo);
        col2.add(phoneNoTxt);
        col2.add(fax);
        col2.add(faxNoTxt);
        col2.add(emailAdd);
        col2.add(emailTxt);
        col2.add(spacer);
        col2.add(spacerTxt);

        col3.add(orgAddress);
        col3.add(orgAddTxt);

        col4.add(otherAdd);
        col4.add(otherAddTxt);

  JPanel buttonsPanel = new JPanel(new BorderLayout());
  content.add("Center", buttonsPanel);

  JPanel buttons = new JPanel(new FlowLayout());
  buttonsPanel.add("North", buttons);

  buttons.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray)));

        JButton addBtn = new JButton("Add");
        JButton deleteBtn = new JButton("Delete");
        JButton clearAllBbtn = new JButton("Clear All");
        JButton copyReqBtn = new JButton("Copy Requestor");
        buttons.add(addBtn);
        buttons.add(deleteBtn);
        buttons.add(clearAllBbtn);
        buttons.add(copyReqBtn);

  JPanel checkBoxPanel = new JPanel(new BorderLayout());
  buttonsPanel.add("Center", checkBoxPanel);

  JPanel checkBoxHolder = new JPanel(new BorderLayout());
  checkBoxPanel.add("North", checkBoxHolder);


  JCheckBox chkBox = new JCheckBox("Search");
        checkBoxHolder.add(chkBox);

        JPanel bottomHalf = new JPanel(new BorderLayout());
        checkBoxPanel.add("Center",bottomHalf );


     //---------------------------------------------------------------------------------------------------------------------   
        Dimension d = new Dimension(100, 200);

        JPanel splitForTable = new JPanel();
        BoxLayout lay = new BoxLayout(splitForTable, BoxLayout.X_AXIS);
        splitForTable.setLayout(lay);
        bottomHalf.add("North",splitForTable );

        JPanel rbtn = new JPanel();
        BoxLayout layoutRbtn = new BoxLayout(rbtn, BoxLayout.Y_AXIS);
        rbtn.setLayout(layoutRbtn);
        rbtn.setPreferredSize(d);
        splitForTable.add(rbtn);
        rbtn.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Search Criteria"));

        JTextField btnBox = new JTextField();
        btnBox.setMaximumSize(new Dimension(10500, 20));
        ButtonGroup group = new ButtonGroup();
        btnBox.setColumns(15);
        JRadioButton firstNameRbtn = new JRadioButton("First Name");
        JRadioButton surnameRbtn = new JRadioButton("Surname");
        JRadioButton orgNameRbtn = new JRadioButton("Organisation Name");
        JRadioButton deptRbtn = new JRadioButton("Department");
        JRadioButton addresRbtn = new JRadioButton("Address");
        JRadioButton addresOtherRbtn = new JRadioButton("Address Other");

        group.add(firstNameRbtn);
        group.add(surnameRbtn);
        group.add(orgNameRbtn);
        group.add(deptRbtn);
        group.add(addresRbtn);
        group.add(addresOtherRbtn);

        rbtn.add(btnBox);
        rbtn.add(firstNameRbtn);
        rbtn.add(surnameRbtn);
        rbtn.add(orgNameRbtn);
        rbtn.add(deptRbtn);
        rbtn.add(addresRbtn);
        rbtn.add(addresOtherRbtn);

        rbtn.add(Box.createVerticalGlue());
        JPanel jtablePanel = new JPanel(new FlowLayout());

        splitForTable.add(jtablePanel);
        jtablePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), "Results"));


        //construct dummy data for table at the moment
        Object[][] data = {
             {"Kathy", "Smith",
              "Snowboarding", new Integer(5), new Boolean(false)},
             {"John", "Doe",
              "Rowing", new Integer(3), new Boolean(true)},
             {"Sue", "Black",
              "Knitting", new Integer(2), new Boolean(false)},
             {"Jane", "White",
              "Speed reading", new Integer(20), new Boolean(true)},
             {"Joe", "Brown",
              "Pool", new Integer(10), new Boolean(false)},
              {"Ton", "Parker",
                  "Pool", new Integer(10), new Boolean(false)},
              {"Sky", "Sports",
                  "Pool", new Integer(12), new Boolean(true)},
              {"Ricki", "Lambert",
                  "Football", new Integer(10), new Boolean(true)},
              {"Simon", "Gayton",
                  "Chemistry", new Integer(15), new Boolean(false)},
              {"Tom", "Andrews",
                  "Golf", new Integer(45), new Boolean(false)},
                 {"Lionel", "Messi",
                  "Football", new Integer(10), new Boolean(true)},
                 {"Tom", "Andrews",
                      "Golf", new Integer(45), new Boolean(false)},
                 {"Tom", "Andrews",
                        "Golf", new Integer(45), new Boolean(false)},
                 {"Tom", "Andrews",
                          "Golf", new Integer(45), new Boolean(false)},
         };

        String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};





       //THIS IS WHERE ALL TABLE INFORMATION IS USED 
        JTable table = new JTable(data, columnNames); 
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(250, 200));
        table.setFillsViewportHeight(true);

        //scrollPane.add(table);
        jtablePanel.add(scrollPane);

        JPanel southBtn = new JPanel(new FlowLayout());
        content.add("South", southBtn);

        JButton bottomOkBtn = new JButton("Ok");
        JButton bottomCancelBtn = new JButton("Cancel");
        JButton bottomApplyBtn = new JButton("Apply");

        southBtn.add(bottomOkBtn);
        southBtn.add(bottomCancelBtn);
        southBtn.add(bottomApplyBtn);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.show();
 }

}
+1  A: 

The problem is because of the line scrollPane.setPreferredSize(new Dimension(250, 200)); You are setting the width of the scrollpane here.

I commented out this line and the table filled the "results" panel.

But:
There are multiple things wrong in the code.
For instance frame.show(); in the last line: the show method is deprecated. Use the setVisible method
You are using JTextFields for the addresses, this means no new lines, the entire address will be in a single line. Try using a JTextArea with a JScrollPane

There are possibly others, but i did not look at the code completely.
If you are trying to develop a UI fast, I suggest using an IDE with drag drop swing development (eclipse with visual editor, or netbeans)

Nivas
+1  A: 

A Jtable has a column model

you can set desired columns width on it

TableColumnModel colModel=table_.getColumnModel();
TableColumn col=colModel.getColumn(i);
col.setPreferredWidth(w);
Peter