Comment accéder aux composants Swing depuis une autre class ?
Bonjour,
Pour mon projet, j'ai choisi de construire l'interface utilisateur (GUI) dans une classe séparée.
J'ai donc:
- Soft.java, le moteur de l'application, qui utilise les autres classes et lance les actions principales.
- SGui.java, l'interface utilisateur, contenant juste quelques actions sur les boutons.
Comment accéder aux composants Swing dans SGui depuis Soft ?
Voir les lignes 33, 34 de Soft.java (les 2 lignes commentées //)
Merci
Soft.java
/**
* Main software class.
*/
package com;
/**
* @author alfonso
*/
public class Soft implements Runnable {
SGui gui = new SGui();
SDrive sDrive = SDrive.getInstance();
/**
*
*/
private Soft() {
Thread thead = new Thread(this);
thead.start();
}
/**
* @param args
*/
public static void main(String[] args) {
Soft soft = new Soft();
}
private void checkDrives() {
// Check for pluged-in removable drive.
int driveCounter = sDrive.getRemovable().size();
if (driveCounter<1) {
System.out.println("No drive pluged-in !");
// How to disable addStickButton if no drive pluged-in ?
// System.out.println(gui.addStickButton.setEnabled(false););
} else {
System.out.println("driveCounter : "+driveCounter);
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
while (true) {
try {
checkDrives();
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
SGui.java
/**
* Graphic user interface.
*/
package com;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.SDrive.Item;
/**
* @author alfonso
*/
public class SGui {
private static SDrive drives = SDrive.getInstance();
private static Vector removables = drives.getRemovable();
//
private static final int SERIAL_COLUMN_INDEX = 4;
private JFrame frame;
private JList list;
private JTable table;
private JTextField textField;
/**
* Constructor.
*/
public SGui() {
try {
javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.setMinimumSize(new Dimension(600, 500));
frame.getContentPane().setLayout(new GridBagLayout());
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JMenuBar menuBar = new JMenuBar();
menuBar.setBorderPainted(false);
frame.setJMenuBar(menuBar);
final JMenu fileMenu = new JMenu();
fileMenu.setText("File");
menuBar.add(fileMenu);
fileMenu.addSeparator();
final JMenuItem exitMenuItem = new JMenuItem();
exitMenuItem.setText("Exit");
fileMenu.add(exitMenuItem);
final JMenu stiksMenu = new JMenu();
stiksMenu.setText("Sticks");
menuBar.add(stiksMenu);
final JMenuItem newMenuItem = new JMenuItem();
newMenuItem.setText("New ...");
stiksMenu.add(newMenuItem);
final JMenuItem removeMenuItem = new JMenuItem();
removeMenuItem.setText("Remove");
stiksMenu.add(removeMenuItem);
final JMenu menu = new JMenu();
menu.setText(" ? ");
menuBar.add(menu);
final JMenuItem aboutMenuItem = new JMenuItem();
aboutMenuItem.setText("Guide");
menu.add(aboutMenuItem);
final JMenuItem licenceMenuItem = new JMenuItem();
licenceMenuItem.setText("Licence");
menu.add(licenceMenuItem);
JScrollPane scrollPane;
final JPanel addStickPanel = new JPanel();
addStickPanel.setBorder(new TitledBorder(null, "Add a stick", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.BELOW_TOP, null, null));
addStickPanel.setLayout(new GridBagLayout());
final GridBagConstraints gridBagConstraints_6 = new GridBagConstraints();
gridBagConstraints_6.weighty = 1.0;
gridBagConstraints_6.fill = GridBagConstraints.BOTH;
gridBagConstraints_6.gridx = 0;
gridBagConstraints_6.gridy = 0;
frame.getContentPane().add(addStickPanel, gridBagConstraints_6);
final JButton refreshListButton = new JButton();
refreshListButton.setText("Refresh list");
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new Insets(0, 0, 0, 0);
addStickPanel.add(refreshListButton, gridBagConstraints);
// refreshListButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("me/icon_txt.gif")));
final JButton addStickButton = new JButton();
addStickButton.setText("Add this stick");
final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints_1.gridwidth = 2;
gridBagConstraints_1.anchor = GridBagConstraints.SOUTHEAST;
gridBagConstraints_1.gridx = 2;
gridBagConstraints_1.gridy = 2;
gridBagConstraints_1.insets = new Insets(0, 0, 0, 0);
addStickPanel.add(addStickButton, gridBagConstraints_1);
final JLabel selectStickLabel = new JLabel();
selectStickLabel.setText("Select a stick :");
final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
gridBagConstraints_3.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints_3.gridx = 0;
gridBagConstraints_3.gridy = 0;
addStickPanel.add(selectStickLabel, gridBagConstraints_3);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setMinimumSize(new Dimension(180, 100));
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setPreferredSize(new Dimension(180, 100));
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
final GridBagConstraints gridBagConstraints_10 = new GridBagConstraints();
gridBagConstraints_10.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints_10.insets = new Insets(5, 0, 5, 0);
gridBagConstraints_10.gridy = 1;
gridBagConstraints_10.gridx = 0;
addStickPanel.add(scrollPane_1, gridBagConstraints_10);
list = new JList(removables);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
String nameSuggestion = ((Item)list.getSelectedValue()).getVolumeName();
textField.setText(nameSuggestion);
System.out.println(((Item)list.getSelectedValue()).getValue());
}
});
scrollPane_1.setViewportView(list);
list.setAutoscrolls(false);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JSeparator separator = new JSeparator();
separator.setPreferredSize(new Dimension(20, 0));
final GridBagConstraints gridBagConstraints_12 = new GridBagConstraints();
gridBagConstraints_12.gridy = 1;
gridBagConstraints_12.gridx = 1;
addStickPanel.add(separator, gridBagConstraints_12);
final JLabel choseNameLabel = new JLabel();
choseNameLabel.setText("Chose a name : ");
final GridBagConstraints gridBagConstraints_11 = new GridBagConstraints();
gridBagConstraints_11.insets = new Insets(0, 0, 0, 0);
gridBagConstraints_11.gridy = 1;
gridBagConstraints_11.gridx = 2;
addStickPanel.add(choseNameLabel, gridBagConstraints_11);
textField = new JTextField();
textField.setMinimumSize(new Dimension(0, 0));
final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
gridBagConstraints_2.ipadx = 150;
gridBagConstraints_2.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints_2.insets = new Insets(0, 0, 0, 0);
gridBagConstraints_2.gridy = 1;
gridBagConstraints_2.gridx = 3;
addStickPanel.add(textField, gridBagConstraints_2);
final JPanel registeredSticksPanel = new JPanel();
registeredSticksPanel.setLayout(new GridBagLayout());
registeredSticksPanel.setBorder(new TitledBorder(null, "Registered sticks", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.BELOW_TOP, null, null));
final GridBagConstraints gridBagConstraints_7 = new GridBagConstraints();
gridBagConstraints_7.weighty = 1.0;
gridBagConstraints_7.weightx = 1.0;
gridBagConstraints_7.fill = GridBagConstraints.BOTH;
gridBagConstraints_7.gridx = 0;
gridBagConstraints_7.gridy = 1;
frame.getContentPane().add(registeredSticksPanel, gridBagConstraints_7);
scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
gridBagConstraints_4.gridwidth = 2;
gridBagConstraints_4.weightx = 1.0;
gridBagConstraints_4.fill = GridBagConstraints.BOTH;
gridBagConstraints_4.weighty = 1.0;
gridBagConstraints_4.gridx = 0;
gridBagConstraints_4.gridy = 0;
gridBagConstraints_4.insets = new Insets(0, 0, 0, 0);
registeredSticksPanel.add(scrollPane, gridBagConstraints_4);
scrollPane.setMinimumSize(new Dimension(320, 160));
scrollPane.setPreferredSize(new Dimension(320, 160));
scrollPane.setName("scrollPane");
//
// table = new StickTable();
table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setModel(new STableModel());
table.setAutoCreateRowSorter(true);
//
scrollPane.setViewportView(table);
final JButton refreshTableButton = new JButton();
refreshTableButton.setText("Refresh table");
final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
gridBagConstraints_5.ipadx = 50;
gridBagConstraints_5.fill = GridBagConstraints.VERTICAL;
gridBagConstraints_5.anchor = GridBagConstraints.SOUTHWEST;
gridBagConstraints_5.gridy = 1;
gridBagConstraints_5.gridx = 0;
registeredSticksPanel.add(refreshTableButton, gridBagConstraints_5);
final JButton deleteSelectedRowButton = new JButton();
deleteSelectedRowButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
long serialID = Long.parseLong(table.getValueAt(table.getSelectedRow(), SERIAL_COLUMN_INDEX).toString());
System.out.println(serialID);
}
});
deleteSelectedRowButton.setText("Delete selected row");
final GridBagConstraints gridBagConstraints_9 = new GridBagConstraints();
gridBagConstraints_9.ipadx = 20;
gridBagConstraints_9.anchor = GridBagConstraints.SOUTHEAST;
gridBagConstraints_9.gridy = 1;
gridBagConstraints_9.gridx = 1;
registeredSticksPanel.add(deleteSelectedRowButton, gridBagConstraints_9);
final JPanel panel_3 = new JPanel();
panel_3.setLayout(new GridLayout(3, 0));
final GridBagConstraints gridBagConstraints_8 = new GridBagConstraints();
gridBagConstraints_8.anchor = GridBagConstraints.SOUTH;
gridBagConstraints_8.gridx = 0;
gridBagConstraints_8.gridy = 2;
gridBagConstraints_8.ipadx = 590;
gridBagConstraints_8.ipady = 20;
gridBagConstraints_8.insets = new Insets(0, 0, 0, 0);
frame.getContentPane().add(panel_3, gridBagConstraints_8);
frame.setVisible(true);
}
}
Afficher la suite