miglayout with jtable with issue

半城伤御伤魂 提交于 2019-12-23 06:00:27

问题


I have used miglayout in swing applicaion.I have encounted one issue like jtable cant appear in whole frame it show only left side some area. i want to show untill at right side end of jframe edge

My implemantaion code is below

package test;



import java.awt.EventQueue;
import java.util.Arrays;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;


import net.miginfocom.swing.MigLayout;



public class ProductPanel2 extends JPanel {

private JLabel lblProd;
private JButton butAdd;
private JButton butRemove;
private JButton butEdit;
private JScrollPane scroll;
private JTable table;
private static final long serialVersionUID = 1L;
public JList<String> lst_Options;
private JScrollPane scr_Data;
private JComboBox<ComboItem> cmb_Suppliers;


//Generar de inmediato
private JButton btn_NewUpdate;
private JButton btn_Delete;
private JButton btn_Copy;
private JTable tbl_Settings;
private JLabel lbl_SelectedOption;
DefaultListModel<String> modeloOpciones;
public ProductPanel2() {

    initComponents();
}

private void initComponents() {



    lblProd = new JLabel("Product List: ");

    btn_NewUpdate = new JButton("Add");
    btn_Delete = new JButton("Remove");
    lbl_SelectedOption = new JLabel("Tipo de datos");
    JLabel lbl_Opciones = new JLabel("Opciones");

    lst_Options = new JList<String>();
    modeloOpciones = new DefaultListModel<String>();
    btn_Delete = new JButton("Eliminar");

    btn_NewUpdate = new JButton("Nuevo");
    scr_Data = new JScrollPane();
    cmb_Suppliers = new JComboBox<ComboItem>();
    modeloOpciones.addElement("Proveedores");
    modeloOpciones.addElement("Productos");
    modeloOpciones.addElement("Partidas");
    lst_Options.setModel(modeloOpciones);

    tbl_Settings = createTable();
    tbl_Settings.setFillsViewportHeight(true);
    scr_Data = new JScrollPane(tbl_Settings);        

    JPanel filterPanel=new JPanel();


    setLayout(new MigLayout("debug", "[96px][][94.00][grow][149.00px][2px][161px]", "[16px][240px,grow][12px][29px]"));

    //add(lbl_SelectedOption, "cell 1 0,alignx left,sgx");

    add(lst_Options, "cell 0 1 1 5,grow");


    filterPanel.add(scr_Data,"wrap, sg buttons");
//  filterPanel.add(cmb_Suppliers, "");
    add(filterPanel,"span 2 3, grow, wrap");
    scr_Data.setViewportView(tbl_Settings);
    lbl_Opciones.setHorizontalAlignment(SwingConstants.CENTER);
    add(lbl_Opciones, "cell 0 0,growx,aligny top");

    add(btn_Delete, "cell 6 3,alignx right,aligny bottom");
    add(btn_NewUpdate, "cell 4 3,alignx right,aligny bottom");

    refreshCombo(cmb_Suppliers);
}
private void refreshCombo(JComboBox<ComboItem> combo) {
    combo.removeAllItems();
    try {

            combo.addItem(new ComboItem("0","ashjish"));
            combo.addItem(new ComboItem("2","ashjish"));
            combo.addItem(new ComboItem("3","ashjish"));

    } catch (Exception e) {
        //log.logToFile("refreshCombo: " + e.getClass().getName() + ": " + e.getMessage(), 1);
        e.printStackTrace();
    }       
}
private JTable createTable() {

    String[] columnNames = "Name 1,Name 2,Name 3,Name 4,Name 5".split(",");

    int rows = 30;
    int cols = columnNames.length;
    String[][] data = new String[rows][cols];

    for(int i=0; i<rows; i++) {
        for(int j=0; j<cols; j++) {
            data[i][j] = "R"+i+" C"+j;
        }
    }
    JTable table = new JTable(data, columnNames);

    return table;
}
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            ProductPanel2 pane = new ProductPanel2();
            frame.setContentPane(pane);
            frame.setSize(1000,1000);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });        

}
}


回答1:


You should re-think, how you are using the MigLayout! Please consider reading the quickstart guide: http://www.miglayout.com/QuickStart.pdf

Also your variable naming needs some work, it is very unclear, which variable is for what.

In order to solve your problem, do this:

    tbl_Settings = createTable();
    tbl_Settings.setFillsViewportHeight(true);
    scr_Data = new JScrollPane(tbl_Settings);
    // I have created a new Dimension-object. Those dimensions are FIXED! You might want to put a new dimension in, that is relative to the window size.
    scr_Data.setPreferredSize(new Dimension(800, 800));

    JPanel filterPanel=new JPanel();

Change this line (where you are setting absolute widths and heights for rows and columns!):

setLayout(new MigLayout("debug", "[96px][][94.00][grow][149.00px][2px][161px]", "[16px][240px,grow][12px][29px]"));

To this:

setLayout(new MigLayout());

Remove all the cells you have added, because you have mixed them with other layout-parts, that are not cells:

add(lst_Options, "grow");
filterPanel.add(scr_Data,"wrap");
//  filterPanel.add(cmb_Suppliers, "");
add(filterPanel,"width 100%, growx, push, span, wrap");
scr_Data.setViewportView(tbl_Settings);
lbl_Opciones.setHorizontalAlignment(SwingConstants.CENTER);
add(lbl_Opciones, "growx,aligny top");
add(btn_Delete, "alignx right,aligny bottom");
add(btn_NewUpdate, "alignx right,aligny bottom");

Looks like this:


PS: I did not know that this would work:

String[] columnNames = "Name 1,Name 2,Name 3,Name 4,Name 5".split(",");

The usual way is:

String[] columnNames = {"Name 1","Name 2","Name 3","Name 4","Name 5"};

I've learned something!

EDIT:

I've learned, that split() works by using regular expression, so you have to be careful, how to use it!

This will return an empty field and NOT "aaa" and "bbb":

"aaa.bbb".split(".");

Update:

I have updated my answer, this is the code in the initComponents()-method. I again urge you to read more about the MigLayout and stop mixing cell-components like this add(lst_Options, "cell 0 1,grow"); with non-cell components. This is messy and hard to understand / fix. I removed those cells in my answer and added alignments for the buttons. Now everything works as expected.

private void initComponents() {

    lblProd = new JLabel("Product List: ");

    btn_NewUpdate = new JButton("Add");
    btn_Delete = new JButton("Remove");
    lbl_SelectedOption = new JLabel("Tipo de datos");
    JLabel lbl_Opciones = new JLabel("Opciones");

    lst_Options = new JList<String>();
    modeloOpciones = new DefaultListModel<String>();
    btn_Delete = new JButton("Eliminar");

    btn_NewUpdate = new JButton("Nuevo");
    scr_Data = new JScrollPane();
    cmb_Suppliers = new JComboBox<String>();
    modeloOpciones.addElement("Proveedores");
    modeloOpciones.addElement("Productos");
    modeloOpciones.addElement("Partidas");
    lst_Options.setModel(modeloOpciones);

    tbl_Settings = createTable();
    tbl_Settings.setFillsViewportHeight(true);
    scr_Data = new JScrollPane(tbl_Settings);
    tbl_Settings.setPreferredScrollableViewportSize(tbl_Settings.getPreferredSize());
    setLayout(new MigLayout("","[right]"));
    add(lbl_Opciones, "growx,aligny, top, wrap");
    add(lst_Options, "grow");

    add(scr_Data, "width 100%, growx, push, span, wrap");
    scr_Data.setViewportView(tbl_Settings);
    lbl_Opciones.setHorizontalAlignment(SwingConstants.CENTER);

    btn_Delete.setHorizontalAlignment(SwingConstants.RIGHT);
    btn_NewUpdate.setHorizontalAlignment(SwingConstants.RIGHT);
    add(btn_Delete, "bottom, span 2");
    add(btn_NewUpdate, "bottom");

    refreshCombo(cmb_Suppliers);
}

Looks like this:




回答2:


i dont want space betweeb combo and jtable whuich in mention at screenshot

package test;
import java.awt.EventQueue;
import java.util.Arrays;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;


import net.miginfocom.swing.MigLayout;




public class ProductPanel2 extends JPanel {

private JLabel lblProd;
private JButton butAdd;
private JButton butRemove;
private JButton butEdit;
private JScrollPane scroll;
private JTable table;
private static final long serialVersionUID = 1L;
public JList<String> lst_Options;
private JScrollPane scr_Data;
private JComboBox<ComboItem> cmb_Suppliers;


//Generar de inmediato
private JButton btn_NewUpdate;
private JButton btn_Delete;
private JButton btn_Copy;
private JTable tbl_Settings;
private JLabel lbl_SelectedOption;
private JPanel filterPanel;
private JLabel lbl_cmb_supplier;
private JButton btn_search;

public ProductPanel2() {

    initComponents();
}
private void initComponents() {

    lblProd = new JLabel("Product List: ");
    lbl_cmb_supplier = new JLabel("Proveedor");
    btn_NewUpdate = new JButton("Add");
    btn_Delete = new JButton("Remove");
    lbl_SelectedOption = new JLabel("Tipo de datos");
    JLabel lbl_Opciones = new JLabel("Opciones");
    cmb_Suppliers = new JComboBox<ComboItem>();     

    btn_Delete = new JButton("Eliminar");
    btn_search = new JButton("Search");
    btn_NewUpdate = new JButton("Nuevo");
    scr_Data = new JScrollPane();
    JComboBox<String> cmb_Suppliers = new JComboBox<String>();
    JButton btn_New = new JButton("Nuevo");
    JButton btn_Delete = new JButton("Cancelar");
    JButton btn_PDF = new JButton("Crea PDF");
    JButton btn_Send = new JButton("Envia por correo");
    tbl_Settings = createTable();
    tbl_Settings.setFillsViewportHeight(true);
    scr_Data = new JScrollPane(tbl_Settings);
    tbl_Settings.setPreferredScrollableViewportSize(tbl_Settings.getPreferredSize());
    filterPanel = new JPanel();
    /*setLayout(new MigLayout("","[right]"));
    add(lbl_Opciones, "growx,aligny, top, wrap");
    filterPanel.add(lbl_cmb_supplier, "split 2, wrap");
    filterPanel.add(cmb_Suppliers, "wrap");     
    filterPanel.add(btn_search, "grow, spany, wrap");
    add(filterPanel, "growx 5,split 2, flowy, top, sgx");
    add(scr_Data, "width 100%, growx, push, span, wrap");
    scr_Data.setViewportView(tbl_Settings);
    lbl_Opciones.setHorizontalAlignment(SwingConstants.CENTER);*/


     setLayout(new MigLayout("debug", "[122px][129px][23px][45px][148px,grow]", "[][100px,grow][]"));
        add(lbl_Opciones, "growx, wrap");
        filterPanel.add(lbl_cmb_supplier);
        filterPanel.add(cmb_Suppliers);     
        filterPanel.add(btn_search, "grow, spany, wrap");
        add(filterPanel, "wrap");
        add(scr_Data, "width 100%, growx, push, span, wrap");


        add(btn_New);
        add(btn_PDF);
        add(btn_Send);
        add(btn_Delete);    

    refreshCombo(cmb_Suppliers);
}
private void refreshCombo(JComboBox<String> combo) {
    combo.removeAllItems();
    try {



    } catch (Exception e) {
        //log.logToFile("refreshCombo: " + e.getClass().getName() + ": " + e.getMessage(), 1);
        e.printStackTrace();
    }       
}
private JTable createTable() {

    String[] columnNames = "Name 1,Name 2,Name 3,Name 4,Name 5".split(",");

    int rows = 30;
    int cols = columnNames.length;
    String[][] data = new String[rows][cols];

    for(int i=0; i<rows; i++) {
        for(int j=0; j<cols; j++) {
            data[i][j] = "R"+i+" C"+j;
        }
    }
    JTable table = new JTable(data, columnNames);

    return table;
}
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            ProductPanel2 pane = new ProductPanel2();
            frame.setContentPane(pane);
            frame.setSize(1000,1000);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });        

}
}


来源:https://stackoverflow.com/questions/37958560/miglayout-with-jtable-with-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!