Code covered by the BSD License  

Highlights from
JButtonDemo

image thumbnail

JButtonDemo

by

 

09 Jan 2005 (Updated )

A MATLAB implementation of the example "How to use buttons" from the Java Tutorial.

buttondemo(varargin)
function buttondemo(varargin)
% A MATLAB version of the java tutorial example - How to use buttons

% Han Qun 
% College of Precision Instrument & Optoelectronics Engineering,
% Tianjin University, Tianjin 30072, P.R.China
% junziyang@126.com

% 01/10/05

% Creat a MATLAB figure and hide it,so that your GUI can appear normally
% after it is transformed into an EXE by the MATLAB Compiler
h=figure('Visible','off');

% Import the necessary java packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

% Creat a window with the given name
frame = JFrame('ButtonDemo');

% Creat a panel to hold you buttons
panel = JPanel();

% Prepare the button icons
leftButtonIcon = ImageIcon('images/right.gif');
middleButtonIcon = ImageIcon('images/middle.gif');
rightButtonIcon = ImageIcon('images/left.gif');

% Creat the buttons
btn_left = JButton('Disable middle button',leftButtonIcon);
btn_left.setVerticalTextPosition(AbstractButton.CENTER);
btn_left.setHorizontalTextPosition(AbstractButton.LEFT);
btn_left.setMnemonic(KeyEvent.VK_D);
btn_left.setActionCommand('disable');

btn_middle = JButton('Middle button',middleButtonIcon);
btn_middle.setVerticalTextPosition(AbstractButton.BOTTOM);
btn_middle.setHorizontalTextPosition(AbstractButton.CENTER);
btn_middle.setMnemonic(KeyEvent.VK_M);

btn_right = JButton('Enable middle button',rightButtonIcon);
% Use the default text positionof CENTER, RIGHT.
btn_right.setMnemonic(KeyEvent.VK_E);
btn_right.setActionCommand('enable');
btn_right.setEnabled(false);

% Set the tips of the left and right button
btn_left.setToolTipText('Click this button to disable the middle button.');
btn_right.setToolTipText('This middle button does nothing when you click it.');

% Prepare a handle structure of the GUI components so that they can be
% interacted by the callback functions.
handles.frame = frame;
handles.btn_left = btn_left;
handles.btn_middle = btn_middle;
handles.btn_right = btn_right;

%Listen for actions on the left button and the right button.
set(btn_left,'ActionPerformedCallback',{@ButtonCallback,handles});
set(btn_right,'ActionPerformedCallback',{@ButtonCallback,handles});

% Add buttons to the panel.
panel.add(btn_left);
panel.add(btn_middle);
panel.add(btn_right);

% Add panel to the frame.
frame.getContentPane().add(panel);

% Set frame size and show it.
frame.pack;
frame.setLocation(300,300);
frame.show;

% set the closing behavior of the frame
set(0,'CurrentFigure',h);
set(frame,'WindowClosingCallback','delete(gcf);');

% Wait and listen for the user actions, untill the GUI being closed
waitfor(h);
frame.dispose;

% Button callback function
function ButtonCallback(hObject,eventdata,handles)
actionCommand = get(hObject,'ActionCommand');
try
    switch actionCommand
        case 'enable'
            handles.btn_left.setEnabled(true);
            handles.btn_middle.setEnabled(true);
            handles.btn_right.setEnabled(false);
        case 'disable'
            handles.btn_left.setEnabled(false);
            handles.btn_middle.setEnabled(false);
            handles.btn_right.setEnabled(true);
    end
    handles.frame.show;
catch
    err=lasterror;
    msgbox(err.message,'Unrecognized indicator','error','modal');
end

Contact us