Home > Enterprise >  How do I move a table to the left if it is in a JPanel?
How do I move a table to the left if it is in a JPanel?

Time:09-17

I am trying to move a table to the left side of a JPanel which is also inside a Window

What I get is this: My work

What I want is this:

What I want

I tried using .setBounds method but it does not work. Does anyone know what to do?

This is my code:

package Modulos;
import paneles.*;
import javax.swing.*;

public class ModuloAdmin extends JFrame {

    public ModuloAdmin(){
        //Crear tabla
        String[][] datosprueba = {
                {"001","Carlitos", "Casa","51202011"},
                {"001","Carlitos", "Casa","51202011"}
        };

        String[] TituloColumna = {"Código", "Nombre", "Dirección", "Teléfono"};
        JTable TablaSucursales = new JTable(datosprueba,TituloColumna);

        //Contenido de las pestañas
        JPanel PanelSucursales = new JPanel();

        PanelSucursales.add(TablaSucursales);
        PanelSucursales.add(new JScrollPane(TablaSucursales));
        JPanel PanelProductos = new JPanel();
        PanelProductos.add(new JLabel("Panel productos"));
        JPanel PanelClientes = new JPanel();
        PanelClientes.add(new JLabel("Panel Clientes"));
        JPanel PanelVendedores = new JPanel();
        PanelVendedores.add(new JLabel("Panel Vendedores"));

        //CREACION DE PESTAÑAS
        JTabbedPane Pestanias = new JTabbedPane();
        Pestanias.setBounds(20,80,700,700);
        Pestanias.add("Sucursales",PanelSucursales);
        Pestanias.add("Productos",PanelProductos);
        Pestanias.add("Clientes", PanelClientes);
        Pestanias.add("Vendedores",PanelVendedores);

        //vENTANA MODULO DE ADMIN
        JFrame VentanaModuloAdmin = new JFrame();
        VentanaModuloAdmin.add(Pestanias);
        VentanaModuloAdmin.setLayout(null);
        VentanaModuloAdmin.setSize(800,850);
        VentanaModuloAdmin.setLocationRelativeTo(null);
        VentanaModuloAdmin.setTitle("Módulo de administrador");
        VentanaModuloAdmin.setVisible(true);
        VentanaModuloAdmin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

CodePudding user response:

Use appropriate layout mangers and container management

Make use of appropriate layout managers

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTable table;

        public TestPane() {
            setBorder(new EmptyBorder(16, 16, 16, 16));
            setLayout(new GridLayout(0, 2));
            add(new JScrollPane(table));
            add(createButtonPane());
        }

        protected JPanel createButtonPane() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.ipadx = 16;
            gbc.ipady = 16;
            gbc.fill = gbc.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 0;

            panel.add(new JButton("Clear"), gbc);
            gbc.gridx = 1;
            panel.add(new JButton("Carga Masiva"), gbc);

            gbc.gridy  ;
            gbc.gridx = 0;
            panel.add(new JButton("Actualizar"), gbc);
            gbc.gridx = 1;
            panel.add(new JButton("Eliminar"), gbc);

            gbc.gridy  ;
            gbc.gridx = 0;
            gbc.gridwidth = 2;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weighty = 1;
            panel.add(new JButton("Exportar Listado a PDF"), gbc);

            return panel;
        }


    }

}

Seriously, layout managers solve one of the most difficult issues facing UI developers - the variable nature of the output (screen resolutions, font metrics, accessibility modifiers, a whole bunch of different stuff which changes how layouts need to be calculated).

They might seem difficult to start with, but once you get use to using them and learn how to make use of "compound layouts", which is demonstrated above, it will help you make better UIs more quickly

  • Related