user6350130
user6350130

Reputation:

Unable to change location and size of JTable

I'm making a JTable in Java.

Here is my code. I have put the table on a panel (jtjp1), jtjp1 is put on adminjp1. adminjp1 is put on adminjf1

Code

 package Hotel_room_reservation_system;

import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;

public class Hotel_room_reservation_system extends JFrame implements ActionListener {

public JFrame adminjf1;
public JTable jt1;
public JPanel adminjp1;
public JPanel jtjp1; 

public Hotel_room_reservation_system() {
    guimake();
}

public void guimake() {
   adminjf1 = new JFrame("ADMIN");

   adminjp1 = new JPanel();
   jtjp1 =new JPanel(new BorderLayout());
   jt1=new JTable();
   String[] columns = {"Name", "Age", "Gender"};

   String[][] data = {{"John", "18", "Male"},
                     {"Daisy", "19", "Female"},
                     {"Dave", "23", "Male"},
                     {"Jake", "30", "Male"}};

   jt1 = new JTable(data, columns)                 
   {
        public boolean isCellEditable(int data, int columns)
        {
           return false;
        }


        public Component prepareRenderer(TableCellRenderer r, int data, int columns)
        {
               Component c = super.prepareRenderer(r, data, columns);

               if ((data % 2 == 0))
                   c.setBackground(Color.WHITE);

               else
                    c.setBackground(Color.LIGHT_GRAY);

               return c;
        }
    };



    JScrollPane jps = new JScrollPane(jt1);
    //   jt1.setFillsViewportHeight(true);
    jt1.setFillsViewportHeight(true);
    jtjp1.setLocation(10,10);

    adminjf1.setSize(900, 900);
    adminjf1.setLayout(null);
    adminjp1.setBackground(Color.WHITE);
    adminjp1.setSize(800, 800);
    jtjp1.setSize(550,180);
    jtjp1.setBackground(Color.red);


    adminjp1.setLocation(20, 25); 
    jtjp1.add(jps);
    adminjp1.add(jtjp1);
    adminjf1.add(adminjp1);
    adminjf1.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Hotel_room_reservation_system();
        }
    });
}

@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }
 }

Problems

  1. I'm not able to move the size of the panel named jtjp1.
  2. I'm not able to change location of jtjp1 to the left or down. I have put jtjp1 on adminjp1.

Upvotes: 1

Views: 524

Answers (1)

Jan Bodnar
Jan Bodnar

Reputation: 11637

The reason why the table component is not resizing is that you set a so called null layout manager with the setLayout(null). Setting this means that you should take care of locating and resizing components in your GUI. This is what the layout managers do.

Once you use a layout manager, such as GroupLayout, the example works as expected.

package com.zetcode;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;

public class HotelReservationSystemEx extends JFrame {

    private JTable table;

    public HotelReservationSystemEx() {

        initUI();
    }

    private void initUI() {

        String[] columns = {"Name", "Age", "Gender"};

        String[][] data = {{"John", "18", "Male"},
        {"Daisy", "19", "Female"},
        {"Dave", "23", "Male"},
        {"Jake", "30", "Male"}};

        table = new JTable(data, columns) {
            @Override
            public boolean isCellEditable(int data, int columns) {
                return false;
            }

            @Override
            public Component prepareRenderer(TableCellRenderer r, int data, int columns) {
                Component c = super.prepareRenderer(r, data, columns);

                if ((data % 2 == 0)) {
                    c.setBackground(Color.WHITE);
                } else {
                    c.setBackground(Color.LIGHT_GRAY);
                }

                return c;
            }
        };

        //table.setFillsViewportHeight(true);

        JScrollPane spane = new JScrollPane(table);

        createLayout(spane);

        setTitle("Hotel reservation system");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);        
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(arg[0])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
        );

        pack();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            HotelReservationSystemEx ex = new HotelReservationSystemEx();
            ex.setVisible(true);
        });
    }
}

In addition, you are unnecessary creating a JPanel and instantiating the JTable twice.

Here is the screenshot:

Screenshot from the example

Upvotes: 2

Related Questions