首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使按钮按升序排序我的列表(Java swing)?

如何使按钮按升序排序我的列表(Java swing)?
EN

Stack Overflow用户
提问于 2020-12-01 06:12:02
回答 1查看 459关注 0票数 0

我在java swing中创建了一个jtable,如下所示

我增加了6个按钮,其中3个可以正常工作(添加、更新、删除)。我不担心随机匹配按钮,但我希望“按目标排序”按钮,以整理所有记录输入的最高数目的目标。积分也是如此。我试过很多次了,但我似乎找不出答案。

下面是我的表数组的代码;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  // create JFrame and JTable
    JFrame frame = new JFrame();
    JTable table = new JTable();

    Container c = frame.getContentPane();
    c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
    c.add(table);

    // create a table model and set a Column Identifiers to this model
    Object[] columns = {"Team name", "Wins", "Losses", "Goals Difference","Points","Matches played"};
    DefaultTableModel model = new DefaultTableModel();

    model.setColumnIdentifiers(columns);

    // set the model to the table
    table.setModel(model);

    // Change A JTable Background Color, Font Size, Font Color, Row Height
    table.setBackground(Color.LIGHT_GRAY);
    table.setForeground(Color.black);
    Font font = new Font("", 1, 22);
    table.setFont(font);
    table.setRowHeight(30);
  //  table.setVisible(true);

这是我的按钮草稿;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
btnSortPoints.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {


            }
        });

这是我的删除按钮;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 btnDelete.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

                // i = the index of the selected row
                int i = table.getSelectedRow();
                if(i >= 0){
                    // remove a row from jtable
                    model.removeRow(i);
                }
                else{
                    System.out.println("Delete Error");
                }
            }
        });

这是我的全部代码;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 public class table{
        public static void main(String[] args) {
    
            // create JFrame and JTable
            JFrame frame = new JFrame();
            JTable table = new JTable();
    
            Container c = frame.getContentPane();
            c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
            c.add(table);
    
            // create a table model and set a Column Identifiers to this model
            Object[] columns = {"Team name", "Wins", "Losses", "Goals Difference","Points","Matches played"};
            DefaultTableModel model = new DefaultTableModel();
    
            model.setColumnIdentifiers(columns);
    
            // set the model to the table
            table.setModel(model);
    
            // Change A JTable Background Color, Font Size, Font Color, Row Height
            table.setBackground(Color.LIGHT_GRAY);
            table.setForeground(Color.black);
            Font font = new Font("", 1, 22);
            table.setFont(font);
            table.setRowHeight(30);
          //  table.setVisible(true);
    
            // create JTextFields
            JTextField txtTeamName = new JTextField();
            JTextField txtWins = new JTextField();
            JTextField txtLosses = new JTextField();
            JTextField txtGD = new JTextField();
            JTextField txtPoints = new JTextField();
            JTextField txtMatches = new JTextField();
    
            JLabel lblTeamName = new JLabel("Team name");
            JLabel lblWins = new JLabel("Wins");
            JLabel lblLosses = new JLabel("Losses");
            JLabel lblGD = new JLabel("Goal difference");
            JLabel lblPoints = new JLabel("Points");
            JLabel lblMatches = new JLabel("Matches");
    
            // create JButtons
            JButton btnAdd = new JButton("Add");
            JButton btnDelete = new JButton("Delete");
            JButton btnUpdate = new JButton("Update");
            JButton btnSortPoints = new JButton("Sort by points");
            JButton btnSortGoals = new JButton("Sort by goals");
            JButton btnRandomMatch = new JButton("Add a random data");
    
    
            txtTeamName.setBounds(1200, 230, 100, 25);
            txtWins.setBounds(1200, 260, 100, 25);
            txtLosses.setBounds(1200, 290, 100, 25);
            txtGD.setBounds(1200, 320, 100, 25);
            txtMatches.setBounds(1200,350,100,25);
            txtPoints.setBounds(1200,380,100,25);
    
            lblTeamName.setBounds(1100,230,100,25);
            lblWins.setBounds(1100,260,100,25);
            lblLosses.setBounds(1100,290,100,25);
            lblGD.setBounds(1100,320,100,25);
            lblMatches.setBounds(1100,350,100,25);
            lblPoints.setBounds(1100,380,100,25);
    
    
    
            btnAdd.setBounds(1150, 20, 200, 50);
            btnUpdate.setBounds(1150, 80, 200, 50);
            btnDelete.setBounds(1150, 140, 200, 50);
            btnSortGoals.setBounds(920,20,200,50);
            btnSortPoints.setBounds(920,80,200,50);
            btnRandomMatch.setBounds(920,140,200,50);
    
            // create JScrollPane
            JScrollPane pane = new JScrollPane(table);
            pane.setBounds(0, 0, 880, 400);
    
            frame.setLayout(null);
    
            frame.add(pane);
    
            // add JTextFields to the jframe
            frame.add(txtTeamName);
            frame.add(txtWins);
            frame.add(txtLosses);
            frame.add(txtGD);
            frame.add(txtMatches);
            frame.add(txtPoints);
    
    
            //Adding the labels to the frame
            frame.add(lblTeamName);
            frame.add(lblWins);
            frame.add(lblLosses);
            frame.add(lblGD);
            frame.add(lblMatches);
            frame.add(lblPoints);
    
            // add JButtons to the jframe
            frame.add(btnAdd);
            frame.add(btnDelete);
            frame.add(btnUpdate);
            frame.add(btnSortGoals);
            frame.add(btnSortPoints);
            frame.add(btnRandomMatch);
    
            // create an array of objects to set the row data
            Object[] row = new Object[6];
    
            // button add row
            btnAdd.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    row[0] = txtTeamName.getText();
                    row[1] = txtWins.getText();
                    row[2] = txtLosses.getText();
                    row[3] = txtGD.getText();
                    row[4] = txtMatches.getText();
                    row[5] = txtPoints.getText();
    
                    // add row to the model
                    model.addRow(row);
                }
            });
    
            // Event listener for button remove row
            btnDelete.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
                    // i = the index of the selected row
                    int i = table.getSelectedRow();
                    if(i >= 0){
                        // remove a row from jtable
                        model.removeRow(i);
                    }
                    else{
                        System.out.println("Delete Error");
                    }
                }
            });
    
            // get selected row data From table to textfields
            table.addMouseListener(new MouseAdapter(){
    
                @Override
                public void mouseClicked(MouseEvent e){
    
                    // i = the index of the selected row
                    int i = table.getSelectedRow();
    
                    txtTeamName.setText(model.getValueAt(i, 0).toString());
                    txtWins.setText(model.getValueAt(i, 1).toString());
                    txtLosses.setText(model.getValueAt(i, 2).toString());
                    txtGD.setText(model.getValueAt(i, 3).toString());
                    txtMatches.setText(model.getValueAt(i,4).toString());
                    txtPoints.setText(model.getValueAt(i,5).toString());
                }
            });
    
            // button update row
            btnUpdate.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed (ActionEvent e) {
    
                    // i = the index of the selected row
                    int i = table.getSelectedRow();
    
                    if(i >= 0)
                    {
                        model.setValueAt(txtTeamName.getText(), i, 0);
                        model.setValueAt(txtWins.getText(), i, 1);
                        model.setValueAt(txtLosses.getText(), i, 2);
                        model.setValueAt(txtGD.getText(), i, 3);
                        model.setValueAt(txtMatches.getText(),i,4);
                        model.setValueAt(txtPoints.getText(),i,5);
                    }
                    else{
                        System.out.println("Update Error");
                    }
                }
            });
    
            btnSortPoints.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
    
                }
            });
    
    
                    frame.setSize(1400, 500);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    
        }
    
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-01 07:38:59

  1. 您首先需要向JTable添加排序支持。阅读教程中的Sorting and Filtering链接,了解如何做到这一点。

  1. 完成后,用户将能够通过单击列的列标题对任何列进行排序。

通过手动单击列标题来验证排序是否有效。

我想要“按目标排序”按钮来整理按最高目标和点数输入的所有记录

既然您的JTable支持排序,就可以向按钮中添加一个ActionListener以执行特定的排序:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
btnSortPoints.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        // Points column in the model is converted to the view column

        int viewColumn = table.convertColumnIndexToView(4); 

        // Set up the columns you want to sort on

        List<RowSorter.SortKey> sortKeys = new ArrayList<>();
        sortKeys.add(new RowSorter.SortKey(viewColumn, SortOrder.ASCENDING));

        // Do the sorting

        TableRowSorter sorter = (TableRowSorter)table.getRowSorter();
        sorter.setSortKeys(sortKeys);    
    }
});

`

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65092141

复制
相关文章
前端性能优化—将CSS文件放在顶部
CSS文件放置在head中和放在body底部,对CSS本身的下载时间不会有影响,但对页面的呈现有非常大的影响,与用户体验密切相关。
红目香薰
2022/11/29
8910
iOS AutoLayout全解
AutoLayout简介 Autolayout是一种全新的布局技术,专门用来布局UI界面的,用来取代Frame布局在遇见屏幕尺寸多重多样的问题。Autolayout自iOS 6开始引入, 但是由于X
xiangzhihong
2018/02/06
4.6K0
iOS AutoLayout全解
iOS布局之AutoresizingMask和AutoLayout
关于iOS的布局主要有两种方式,分别是AutoResizing和AutoLayout。其中AutoResizing作为一种旧的布局方式,在AutoLayout被推广之后已经很少被使用。为了更加清晰的了解iOS的布局方式,本篇针对于这两种布局方法进行简要的总结。 一.AutoResizing 我们在使用AutoResizing进行布局的时候,其主要思想就是设置子视图跟随父视图的frame变化而变化。具体的情况,我们可以设置左跟随,右跟随等等。下面是AutoResizing在代码中的使用。 //父视图 UIVi
梧雨北辰
2018/04/24
1.9K0
iOS布局之AutoresizingMask和AutoLayout
Autolayout
Autolayout Autolayout是一种“自动布局”技术,专门用来布局UI界面的 Autolayout自iOS6开始引入,由于Xcode4的不给力,当时并没有得到很大推广自iOS7(Xcode5)开始,Autolayout的开发效率得到很大的提升 苹果官方也推荐开发者尽量使用Autolayout来布局UI界面 Autolayout能很轻松地解决屏幕适配的问题 Autolayout的2个核心概念 参照 约束 与 Autoresizing 区别 在Autolayout之前,有Autoresizing可以
用户1941540
2018/05/11
9280
iOS开发之UIScrollView在Autolayout下的使用
一、使用的基本原则: 原则1:UIScrollView的size依赖于subviews 首先在StoryBoard中拖入一个UIScrollView,用Pin按钮,随意设置其布局。因为设置UIScrollView的布局约束是没有用的,UIScrollView的size(即contentSize)是根据其中的subviews所占据的size来计算的。当然,如果contentSize的内容不足以布满整个UIScrollView时,滚动条将不会出现,UIScrollView也不会滚动。 原则2:subviews的
YungFan
2018/04/24
9880
firefox 使用侧边标签栏替换顶部标签栏
推荐安装使用 Tree Style Tab 插件实现侧边插件,其他插件也可自行探索。
宋天伦
2023/10/21
6280
firefox 使用侧边标签栏替换顶部标签栏
【IOS开发基础系列】Autolayout自动布局专题
        bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系的坐标和大小区别主要在坐标系这一块。很明显一个是自己为原点的坐标系,一个是以屏幕为原点的坐标系。
江中散人_Jun
2023/10/16
3500
【IOS开发基础系列】Autolayout自动布局专题
iOS 布局进阶:你真的会用 autolayout 么?
iOS系统已经迎来了10.3.2版本,iOS软件开发发展至今已经相当成熟了。布局的方式从frame、size、center到如今强大的autolayout,将UI布局尽量的“自动化”和“智能化”,在很大程度上减少了程序员的工作量。
波儿菜
2018/05/07
1.4K0
iOS 布局进阶:你真的会用 autolayout 么?
iOS界面布局之二——初识autolayout布局模型
     在上一篇博客中介绍了传统的布局方式:autoresizing。随着iphone型号的越来越多,屏幕的标准也更加多样化,通过autoresizing已经不能满足开发的需求,而进行两套布局或者动态代码控制又大大增加了开发者的工作量,autolayout的出现拯救个这一切,它让动态布局变的十分简单便捷。
珲少
2018/08/16
1K0
iOS界面布局之二——初识autolayout布局模型
将网站程序放在tmpfs下
将网站程序放在tmpfs下 然后用nginx直接做对外服务呢 varnish或者squid都是利用内存和它的连接数来做到加速服务. 但是如果是squid->nginx->fastcgi->mysql 这样当中很多连接是开销在内部的连接之中 而且如果客户端请求php.squid还需要将请求再转发至nginx,然后nginx再转发至fastcgi 对于动态内容的多加了一个步骤. 考虑到nginx有了不低于squid以及varnish的连接能力 那么可以将网站程序直接放在tmpfs中 这样如果是静态的.就会直接从内存读取后返回给用户(和其他缓冲服务器的效果一样) 如果是PHP就丢给后面的fastcgi处理 这样更快.
Java架构师必看
2021/03/22
1.1K0
Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局
简介 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstraints. 最新示例: 点击下载 项目简议: 如果再看到关于纯代码,xib或storyboa
ios122
2018/01/02
2.1K0
Autolayout
只要设置leftLabel的right距rightLabel的mas_left间距即可。需要谁不被压缩,就设置谁距离另外一个被压缩的的间距即可。
用户1890628
2019/08/07
4450
Autolayout
他们主动布局(autolayout)环境的图像编辑器
上的,今天我将其pull到github上来了,大家能够自行下载:git clone git@github.com:lihux/twentyThousandTomatoes.git没有安装git或者不会用的童鞋,
全栈程序员站长
2022/07/05
8110
他们主动布局(autolayout)环境的图像编辑器
浅谈 iOS AutoLayout 中 Label 的抗拉伸和抗压缩
UIView 中关于 Content Hugging 和 Content Compression Resistance 的方法有:
s_在路上
2018/09/11
5.5K0
浅谈 iOS AutoLayout 中 Label 的抗拉伸和抗压缩
Texture
Texture原名是AsyncDisplayKit,是Facebook的paper团队发布的一个基于UIKit的库,这个库能够将图片加载、布局计算以及UI渲染等操作均放在后台线程,进而可以极大地优化APP界面的流畅度。
拉维
2019/08/12
2.4K1
Texture
iOS Autolayout 修改约束优先级崩溃问题
我们从xib中拖出约束,动态的修改优先级程序崩溃。 分析 ---- 试了几次发现,修改到1000就会崩溃 崩溃原因 ---- 不允许优先级跨越1000修改(包括1000) 例: 从1000修改到750,250或者250修改到1000以上 解决 ---- 把要修改的优先级更改为小于1000例如999
星宇大前端
2019/01/15
8690
IOS开发系列——Masonry手写Autolayout专题【整理,部分原创】
Masonry常规开发指导整理自此篇文档(可别说我转载不注明出处哦,^_^),后面加入了笔者在实际开发使用过程中遇到过的一些问题,希望对你有所帮助。
江中散人_Jun
2022/03/13
7960
IOS开发系列——Masonry手写Autolayout专题【整理,部分原创】
iOS界面布局之三——纯代码的autoLayout及布局动画
        关于界面布局,apple的策略已经趋于成熟,autolayout的优势在开发中也已经展现的淋漓尽致。除了使用storyBoard进行布局约束的拖拽,有时我们也需要在代码中进行autolayout的布局设置,Masonry库可以方便的创建约束属性,实际上,我们也没有必要再使用系统原生的代码来创建和设置约束,这篇博客只作为使用的方法备忘。前几篇布局介绍的链接如下:
珲少
2018/08/15
2.9K0
iOS界面布局之三——纯代码的autoLayout及布局动画
云数据隐私:将密钥放在哪里?
在任何工作负载迁移项目计划中,最容易被忽视的项目通常是跨多个云服务的密钥管理和合规性。增强自带密钥(BYOK)服务使企业可以将数据位置与加密密钥分开。加密最佳实践有助于提高数据隐私性。
静一
2021/06/01
2.8K0
iOS给TabBar顶部黑线添加发光的阴影
技术实现关键点:通过layer.shadowOpacity和View.layer.shadowOffset实现
公众号iOS逆向
2021/07/29
1.4K0

相似问题

调用回调时,Moq回调值不会更改。

11

使用回调值重新启动动画

112

单独调用回调函数?

33

AngularJS调用回调函数

133

未调用回调函数

11
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文