gpt4 book ai didi

java - 如何向 JTable 添加 Action 监听器?

转载 作者:行者123 更新时间:2023-11-30 07:52:31 24 4
gpt4 key购买 nike

这是一个 Java 日历。我想在单击日期时向弹出窗口添加功能并编辑 View 。1.如何向JTable的单元格添加 Action 监听器?2.当我点击日期(单元格)时,会弹出一个对话框来存储信息。

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Calender{
static JFrame Main_Frame;
static JButton Prev_button, Next_Button, Today_Button;
static JLabel Month_Label, Year_Label;
static JTable Calender_Table;
static JComboBox Year_Combobox;
static Container Pane_container;
static DefaultTableModel Calender_mTable;
static JScrollPane SCalender_Table;
static JPanel Calender_Panel;
static int Gre_Year, Gre_Month, Gre_Day, Current_Year, Current_Month;

public static void main (String args[]){
//try catch
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
catch (UnsupportedLookAndFeelException e) {}

//create and settup frame
Main_Frame = new JFrame ("Calender");
Main_Frame.setSize(330, 375);
Pane_container = Main_Frame.getContentPane();
Pane_container.setLayout(null);
Main_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


//Create controls
Month_Label = new JLabel ("January");
Year_Label = new JLabel ("Year:");
Year_Combobox = new JComboBox();
Prev_button = new JButton ("<");
Next_Button = new JButton (">");
Today_Button = new JButton("Today");
Calender_mTable = new DefaultTableModel(){public boolean isCellEditable(int rowIndex, int mColIndex){return true;}};
Calender_Table = new JTable(Calender_mTable);
SCalender_Table = new JScrollPane(Calender_Table);
Calender_Panel = new JPanel(null);

//Add all controls to panel
Pane_container.add(Calender_Panel);
Calender_Panel.add(Month_Label);
Calender_Panel.add(Year_Label);
Calender_Panel.add(Year_Combobox);
Calender_Panel.add(Prev_button);
Calender_Panel.add(Next_Button);
Calender_Panel.add(Today_Button);
Calender_Panel.add(SCalender_Table);

//listeners
Prev_button.addActionListener(new Prev_button_Action());
Next_Button.addActionListener(new Next_Button_Action());
Today_Button.addActionListener(new Today_Button_Action());
Year_Combobox.addActionListener(new Year_Combobox_Action());
// Calender_Table.addActionListener(new ActionLitsener());
//Set the border with name
Calender_Panel.setBorder(BorderFactory.createTitledBorder("Calendar"));



//Set all element place and size
Calender_Panel.setBounds(0, 0, 321, 333);
Month_Label.setBounds(160-Month_Label.getPreferredSize().width/2, 25, 100, 25);
Year_Label.setBounds(200, 305, 81, 21);
Year_Combobox.setBounds(230, 305, 81, 21);
Prev_button.setBounds(10, 25, 51, 26);
Today_Button.setBounds(128,5,65,26);
Next_Button.setBounds(260, 25, 51, 26);
SCalender_Table.setBounds(10, 51, 301, 256);

//set visible frame
Main_Frame.setResizable(false);
Main_Frame.setVisible(true);

//Getthe month and day from gregorian
GregorianCalendar calender = new GregorianCalendar(); //Create calendar
Gre_Day = calender.get(GregorianCalendar.DAY_OF_MONTH); //Get day
Gre_Month = calender.get(GregorianCalendar.MONTH); //Get month
Gre_Year = calender.get(GregorianCalendar.YEAR); //Get year
Current_Month = Gre_Month; //Match month and year
Current_Year = Gre_Year;

//Add the headers
String[] headers = {"Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saturday"}; //All headers
for (int i=0; i<7; i++){
Calender_mTable.addColumn(headers[i]);

}
//Set background
Calender_Table.getParent().setBackground(Calender_Table.getBackground());

//No resize or reorder

Calender_Table.getTableHeader().setReorderingAllowed(false);
Calender_Table.getTableHeader().setResizingAllowed(false);


//Single cell selection
Calender_Table.setColumnSelectionAllowed(true);
Calender_Table.setRowSelectionAllowed(true);
Calender_Table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

//Set row column
Calender_Table.setRowHeight(38);
Calender_mTable.setColumnCount(7);
Calender_mTable.setRowCount(6);

//Populate the table
for (int a=Gre_Year-100; a<=Gre_Year+100; a++){
Year_Combobox.addItem(String.valueOf(a));
}

//Refresh calendar
Refresh_Calendar (Gre_Month, Gre_Year); //Refresh calendar
}

public static void Refresh_Calendar(int month, int year){
//Variables
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int dom, dow; //Number Of Days, Start Of Month

//allow the button = true
Prev_button.setEnabled(true);
Next_Button.setEnabled(true);

if (month == 0 && year <= Gre_Year-10){Prev_button.setEnabled(false);}
if (month == 11 && year >= Gre_Year+100){Next_Button.setEnabled(false);}
Month_Label.setText(months[month]);
Month_Label.setBounds(160-Month_Label.getPreferredSize().width/2, 25, 180, 25);
Year_Combobox.setSelectedItem(String.valueOf(year));

//Clear table
for (int a=0; a<6; a++){
for (int b=0; b<7; b++){
Calender_mTable.setValueAt(null, a,b );
}
}

//Get first day of month and number of days
GregorianCalendar calander = new GregorianCalendar(year, month, 1);
dom = calander.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
dow = calander.get(GregorianCalendar.DAY_OF_WEEK);

//Draw the calendar
for (int a=1; a<=dom; a++){
int row = new Integer((a+dow-2)/7);
int column = (a+dow-2)%7;
Calender_mTable.setValueAt(a, row, column);
}

//Apply renderers
Calender_Table.setDefaultRenderer(Calender_Table.getColumnClass(0), new Calender_TableRenderer());
}

static class Calender_TableRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
if (column == 0 || column == 6){ //Week-end
setBackground(new Color(135,206,235));
}
else{ //Week days
setBackground(new Color(255, 255, 255));
}
if (value != null){
if (Integer.parseInt(value.toString()) == Gre_Day && Current_Month == Gre_Month && Current_Year == Gre_Year){ //Today
setBackground(Color.lightGray);
}
}
// getComponent(1).addFocusListener();

setBorder(null);
setForeground(Color.blue);
return this;
}
}


//listener

//set prev button listener
static class Prev_button_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (Current_Month == 0){
Current_Month = 11;
Current_Year -= 1;
}
else{
Current_Month -= 1;
}
Refresh_Calendar(Current_Month, Current_Year);
}
}

//set next button listener
static class Next_Button_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (Current_Month == 11){
Current_Month = 0;
Current_Year += 1;
}
else{ //Foward one month
Current_Month += 1;
}
Refresh_Calendar(Current_Month, Current_Year);
}
}

static class Today_Button_Action implements ActionListener{
public void actionPerformed (ActionEvent e){

Current_Month = Gre_Month ;
Current_Year = Gre_Year;
Refresh_Calendar(Current_Month, Current_Year);
}
}
static class Year_Combobox_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (Year_Combobox.getSelectedItem() != null){
String b = Year_Combobox.getSelectedItem().toString();
Current_Year = Integer.parseInt(b);
Refresh_Calendar(Current_Month, Current_Year);
}
}
}
}

最佳答案

要在 JTable 单元格中添加操作监听器,您可以这样做。您可以将此 Calender_Table 变量重命名为 calenderTable。

calenderTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
// do some stuff
}
}
});

关于java - 如何向 JTable 添加 Action 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33162795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com