如何在JTable之后添加JButton

时间:2013-06-21 00:31:02

标签: java swing jtable jbutton

我有一个JTable,其中有一些像这样的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;
    }
}

但是我想在创建JTable之后添加3个JButton,我希望有一个不能采用所有JFrame大小的JTable,在我的代码中我得到一个Button但是我希望有可能设置按钮界限

我尝试添加一个JPanel,另一个尝试使用&#34; setPreferredSize&#34; JScrollPane不工作的地方,我的意思是我没有出现,所以我看不到所有的表格内容;我也试过&#34; setSize&#34;这对我不起作用。

也许我必须制作第二个小组或类似的东西,我希望你能帮助我解决这个问题。

我想一张图片说的超过1000个单词,所以:

这就是我得到和拥有的以及我想拥有的东西

enter image description here

所以用简单的英语,我想知道的是如何在渲染表后渲染表格并在底部添加按钮(但我不希望按钮具有所有帧宽度,我希望它们远离表格,不是因为我拥有它们,并且有可能改变它们的大小和位置。)

3 个答案:

答案 0 :(得分:8)

enter image description here

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);
    }

答案 1 :(得分:2)

CheckABunch是一个示例,显示如何根据当前选择更新TableModel

image

答案 2 :(得分:1)

您可以查看GridLayout

总之,我对你的班级做了一些改动,我得到了这个:

  • 首先将所有JButtons分组到JPanel,其内部有GridLayout
  • 此外,您的JFrame容器必须具有BorderLayout
  • 因此,您需要将每个按钮添加到辅助窗格,最后将JPanels添加到框架中。

以下是代码:

//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

我刚刚发布了修改内容,因此您需要对整个班级进行分组。

我知道这不是最好的方法,但您可以从这里改进代码。另请注意,您可以在添加到allowedOperations窗格之前创建按钮,作为agreg按钮