The Artima Developer Community
Sponsored Link

Java Answers Forum
changing Buttons in program help

2 replies on 1 page. Most recent reply: Apr 13, 2003 6:18 AM by Blake MacKay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Blake MacKay

Posts: 5
Nickname: newtojava
Registered: Feb, 2003

changing Buttons in program help Posted: Apr 12, 2003 6:21 AM
Reply to this message Reply
Advertisement
I would like to change this code so that instead of using JRadioButtons it displays and uses JButtons. Can anyone help me out on this?



// Look And Feel Demonstration
// Changing the look and feel.

// Java core packages
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;

public class LookAndFeel extends JFrame {

private String strings[] = { "Metal", "Motif", "Windows" };
private UIManager.LookAndFeelInfo looks[];
private JRadioButton radio[];
private ButtonGroup group;
// private JButton button;
private JLabel label;
// private JComboBox comboBox;

// set up GUI
public LookAndFeel()
{
super( "Look and Feel Demo" );

Container container = getContentPane();

// set up panel for NORTH of BorderLayout
JPanel northPanel = new JPanel();
northPanel.setLayout( new GridLayout( 3, 1, 0, 5 ) );

// set up label for NORTH panel
label = new JLabel( "This is a Metal look-and-feel",
SwingConstants.CENTER );
northPanel.add( label );

// set up button for NORTH panel
// button = new JButton( "JButton" );
// northPanel.add( button );

// set up combo box for NORTH panel
// comboBox = new JComboBox( strings );
// northPanel.add( comboBox );

// attach NORTH panel to content pane
container.add( northPanel, BorderLayout.NORTH );

// create array for radio buttons
radio = new JRadioButton[ strings.length ];

// set up panel for SOUTH of BorderLayout
JPanel southPanel = new JPanel();
southPanel.setLayout(
new GridLayout( 1, radio.length ) );

// set up radio buttons for SOUTH panel
radio = new JRadioButton[ strings.length ];
group = new ButtonGroup();
ItemHandler handler = new ItemHandler();

for ( int count = 0; count < radio.length; count++ ) {
radio[ count ] = new JRadioButton( strings[ count ] );
radio[ count ].addItemListener( handler );
group.add( radio[ count ] );
southPanel.add( radio[ count ] );
}

// attach SOUTH panel to content pane
container.add( southPanel, BorderLayout.SOUTH );

// get installed look-and-feel information
looks = UIManager.getInstalledLookAndFeels();

setSize( 300, 200 );
setVisible( true );

radio[ 0 ].setSelected( true );
}

// use UIManager to change look-and-feel of GUI
private void changeTheLookAndFeel( int value )
{
// change look and feel
try {
UIManager.setLookAndFeel(
looks[ value ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}

// process problems changing look and feel
catch ( Exception exception ) {
exception.printStackTrace();
}
}

// execute application
public static void main( String args[] )
{
LookAndFeelDemo application = new LookAndFeelDemo();

application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}

// private inner class to handle radio button events
private class ItemHandler implements ItemListener {

// process user's look-and-feel selection
public void itemStateChanged( ItemEvent event )
{
for ( int count = 0; count < radio.length; count++ )

if ( radio[ count ].isSelected() ) {
label.setText( "Look and Feel = " +
strings[ count ] + " " );
// comboBox.setSelectedIndex( count );

changeTheLookAndFeel( count );
}
}

} // end private inner class ItemHandler

} // end class LookAndFeel


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: changing Buttons in program help Posted: Apr 12, 2003 6:30 PM
Reply to this message Reply
// Look And Feel Demonstration 
// Changing the look and feel.
 
// Java core packages
import java.awt.*;
import java.awt.event.*;
 
// Java extension packages
import javax.swing.*;
 
public class LookAndFeelDemo extends JFrame {
 
private String strings[] = { "Metal", "Motif", "Windows" };
private UIManager.LookAndFeelInfo looks[];
private JButton button[];
//private ButtonGroup group;
// private JButton button;
private JLabel label;
// private JComboBox comboBox;
 
// set up GUI
public LookAndFeelDemo()
{
super( "Look and Feel Demo" );
 
Container container = getContentPane();
 
// set up panel for NORTH of BorderLayout
JPanel northPanel = new JPanel();
northPanel.setLayout( new GridLayout( 3, 1, 0, 5 ) );
 
// set up label for NORTH panel
label = new JLabel( "This is a Metal look-and-feel",
SwingConstants.CENTER );
northPanel.add( label );
 
// set up button for NORTH panel
// button = new JButton( "JButton" );
// northPanel.add( button );
 
// set up combo box for NORTH panel
// comboBox = new JComboBox( strings );
// northPanel.add( comboBox );
 
// attach NORTH panel to content pane
container.add( northPanel, BorderLayout.NORTH );
 
// create array for buttons
button = new JButton[ strings.length ];
 
// set up panel for SOUTH of BorderLayout
JPanel southPanel = new JPanel();
southPanel.setLayout( 
new GridLayout( 1, button.length ) );
 
// set up buttons for SOUTH panel
button = new JButton[ strings.length ];
//group = new ButtonGroup();
ButtonHandler handler = new ButtonHandler();
 
for ( int count = 0; count < button.length; count++ ) {
button[ count ] = new JButton( strings[ count ] );
button[ count ].addActionListener( handler );
//group.add( button[ count ] );
southPanel.add( button[ count ] );
}
 
// attach SOUTH panel to content pane
container.add( southPanel, BorderLayout.SOUTH );
 
// get installed look-and-feel information
looks = UIManager.getInstalledLookAndFeels();
 
setSize( 300, 200 );
setVisible( true );
 
button[ 0 ].setSelected( true );
}
 
// use UIManager to change look-and-feel of GUI
private void changeTheLookAndFeel( int value )
{
// change look and feel
try {
UIManager.setLookAndFeel(
looks[ value ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}
 
// process problems changing look and feel
catch ( Exception exception ) {
exception.printStackTrace();
}
}
 
// execute application
public static void main( String args[] )
{
LookAndFeelDemo application = new LookAndFeelDemo();
 
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
 
// private inner class to handle button events
private class ButtonHandler implements ActionListener {
 
// process user's look-and-feel selection
public void actionPerformed( ActionEvent event )
{
//for ( int count = 0; count < button.length; count++ )
 
//if ( button[ count ].isSelected() ) {
//label.setText( "Look and Feel = " +
//strings[ count ] + " " );
// comboBox.setSelectedIndex( count );
//changeTheLookAndFeel( count );
//}
    label.setText( "Look and Feel = " + event.getActionCommand());
    for (int i = 0; i < strings.length; i++){
        if (event.getActionCommand().equals(strings[i])) changeTheLookAndFeel(i);
    }
 
}
 
} // end private inner class ItemHandler
 
} // end class LookAndFeel 

Blake MacKay

Posts: 5
Nickname: newtojava
Registered: Feb, 2003

Re: changing Buttons in program help Posted: Apr 13, 2003 6:18 AM
Reply to this message Reply
Thank you for your help Charles, your modifications work perfectly.
B.M.

Flat View: This topic has 2 replies on 1 page
Topic: Java Certification Web Site Previous Topic   Next Topic Topic: dynamic memory allocation in stack and heap

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use