简体   繁体   中英

How to add JButton after a JTable

I have a JTable with some data like this SSCCE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.awt.Color;
class kanji{
    public static void main(String args[]){ 
        JFrame frame = new JFrame("Kanji");
        JPanel pane = new JPanel();
        JTable table = new JTable();
        pane.setLayout(new BorderLayout());
        JButton agreg = new JButton("Agregar");         
        DefaultTableModel model = new DefaultTableModel(get_data(), get_header());      
        JFrame hk = new JFrame("Historial de Significados");
        Image icon = Toolkit.getDefaultToolkit().getImage("JLPT.jpg");
        ImageIcon ima = new ImageIcon("JLPT.jpg");
        table = new JTable(model){
            @Override
            public boolean isCellEditable(int row, int col){
                switch(col){
                    case 0:
                        return false;
                    case 1:
                        return false;
                    case 2:
                        return true;
                    default:
                        return false;
                }
            }
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    default:
                        return Boolean.class;
                }
            }
        };
        DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                setForeground(Color.blue);
                setHorizontalAlignment(JLabel.CENTER);
                setFont(new Font("Microsoft JhengHei", Font.BOLD, 50));
                return this;
            }
        };
        DefaultTableCellRenderer r2 = new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                setHorizontalAlignment(JLabel.LEFT);
                setFont(new Font("Microsoft JhengHei", Font.BOLD, 13));
                return this;
            }
        };
        table.getColumnModel().getColumn(0).setCellRenderer(r);
        table.getColumnModel().getColumn(1).setCellRenderer(r2);
        TableColumn column = null;
        for (int i = 0; i < 3; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i==0) {
                column.setMaxWidth(80);
                column.setMinWidth(80);
            }
            else{
                if(i==1){
                    column.setPreferredWidth(470);
                }
                else{
                    column.setMaxWidth(50);
                    column.setMinWidth(50);
                }
            }
        }
        table.setRowHeight(table.getRowHeight()+70);
        table.setModel(model);
        table.getTableHeader().setReorderingAllowed(false);
        JScrollPane scroll = new JScrollPane(table);
        pane.add(scroll, BorderLayout.CENTER);
        pane.add(agreg, BorderLayout.SOUTH);
        frame.add(pane);
        frame.setTitle("Historial de Significados");
        frame.setSize(1350, 700);
        frame.setIconImage(icon);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        scroll.setViewportView(table);  
    }
    public static Object [][]get_data(){
        Object data[][] = new Object[][]{
            {"\u4e00", "Uno, 1", true},
            {"\u4e01", "Uno, 1", true},
            {"\u4e02", "Uno, 1", true},
            {"\u4e03", "Uno, 1", true},
            {"\u4e04", "Uno, 1", true},
            {"\u4e05", "Uno, 1", true},
            {"\u4e06", "Uno, 1", true},
            {"\u4e07", "Uno, 1", true},
            {"\u4e08", "Uno, 1", true},
            {"\u4e09", "Uno, 1", true}
        };
        return data;
    }
    public static String []get_header(){
        String header [] = new String[]{"KANJI", "SIGNIFICADO", "Agregar"};
        return header;
    }
}

But I want to add 3 JButton after JTable is created, I want to have a JTable that don't take all JFrame size, in my code I get a Button but I want to have the possibility to set buttons bounds.

I've tried adding a JPanel and with another try with "setPreferredSize" where JScrollPane don't work, I mean I doesn't appear so I can't see all table content; and I also tried with "setSize" that doesn't work for me.

Maybe I have to make a second panel or something like that, I hope you can help me solving this issue.

I guess an Image says more than 1,000 words, so:

This is what I get and have and what I want to have

在此处输入图片说明

So in plain english, all I want to know is how to render table and add button at the bottom after rendered table (but I don't want buttons to have all the frame width, I want them away from table, not together as I have them and with the possibility to change their size and position).

在此处输入图片说明

private void createUI() {
        JFrame frame = new JFrame();

        frame.setLayout(new BorderLayout());

        final JTable table = new JTable(10, 4);

        JPanel btnPnl = new JPanel(new BorderLayout());
        JPanel topBtnPnl = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        JPanel bottombtnPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));

        topBtnPnl.add(new JButton("Select All"));
        bottombtnPnl.add(new JButton("Cancel"));
        bottombtnPnl.add(new JButton("Add Selected"));

        btnPnl.add(topBtnPnl, BorderLayout.NORTH);
        btnPnl.add(bottombtnPnl, BorderLayout.CENTER);

        table.getTableHeader().setReorderingAllowed(false);



        frame.add(table.getTableHeader(), BorderLayout.NORTH);
        frame.add(table, BorderLayout.CENTER);
        frame.add(btnPnl, BorderLayout.SOUTH);

        frame.setTitle("JTable Example.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

CheckABunch is an example that shows how to update the TableModel based on the current selection.

图片

You could take a look at GridLayout .

In short I've made some changes to your class, and I've got this:

  • First group all JButtons into a JPanel , which have a GridLayout inside
  • Also, your JFrame container must have a BorderLayout
  • So you need add each button to the auxiliary pane and at the end add both JPanels to the frame.

Here's the code:

//Another import sentences
import java.awt.GridLayout;

class kanji{
    public static void main(String args[]){
        // More code goes here
        JPanel allowedOperations = new JPanel(new GridLayout(2, 2));
        pane.setLayout(new BorderLayout());
        // More code goes here

        // All buttons are grouped here
        allowedOperations.add(new JButton()); // Here's a trick 
        allowedOperations.add(agreg);
        allowedOperations.add(new JButton("Save selected"));
        allowedOperations.add(new JButton("Cancel"));

        // Add the pane object to the frame
        frame.add(pane, BorderLayout.CENTER);

        // And finally add the allowedOperations object to the frame
        frame.add(allowedOperations, BorderLayout.SOUTH);
        // More code goes here
} //End class

I've just posted the modifications, so you need to group the whole class.

I know it isn't the best approach, but you can improve your code from here. Also note you can create your buttons before added to the allowedOperations pane, as the agreg button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM