Setting Action Listener and changing background - java

I've created 3 buttons. They are each displayed twice in a JFrame. I'm having trouble changing the background of the frame. I've set ActionListeners but upon clicking, nothing is changing. May I ask for some help.
public class MyButtons extends JPanel {
public static JFrame frame;
private JButton Red = new JButton("Red");
private JButton Green = new JButton("Green");
private JButton Blue = new JButton("Blue");
public void InitializeButton()
{
Blue.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setBackground(Color.BLUE);
}
});
Green.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setBackground(Color.GREEN);
}
});
Red.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setBackground(Color.RED);
}
});
}
public MyButtons() {
InitializeButton();
add(Red);
add(Green);
add(Blue);
}
public static void main(String[] args) {
frame = new JFrame();
JPanel row1 = new MyButtons();
JPanel row2 = new MyButtons();
row1.setPreferredSize(new Dimension(250, 100));
row2.setPreferredSize(new Dimension(250, 100));
frame.setLayout(new GridLayout(3,2));
frame.add(row1);
frame.add(row2);
frame.pack();
frame.setVisible(true);
}
}

This code works, but probably not as you expected:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyButtons extends JPanel {
//public static JFrame frame;
// static is rarely a solution of problems in a GUI. ToDo! Change!
static JPanel ui = new JPanel(new GridLayout(2, 0, 20, 20));
private JButton Red = new JButton("Red");
private JButton Green = new JButton("Green");
private JButton Blue = new JButton("Blue");
public void InitializeButton() {
Blue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ui.setBackground(Color.BLUE);
}
});
Green.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ui.setBackground(Color.GREEN);
}
});
Red.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ui.setBackground(Color.RED);
}
});
}
public MyButtons() {
InitializeButton();
add(Red);
add(Green);
add(Blue);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel row1 = new MyButtons();
JPanel row2 = new MyButtons();
//row1.setPreferredSize(new Dimension(250, 100));
//row2.setPreferredSize(new Dimension(250, 100));
//frame.setLayout(new GridLayout(3, 2,10,10));
ui.add(row1);
ui.add(row2);
frame.add(ui);
frame.pack();
frame.setVisible(true);
}
}
N.B. Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

Related

java communication between 2 JFrames

I'm getting an AWT-EventQueue-0 Null Pointer Exception in the below code and I can't get to fix it.
I want to have a main frame, from which by pressing a button
I open a second frame, where I have the option to create new players,
which would show up in the main frame.
I passed the references to the constructors, but I still keep getting the error.
I would be very happy for some help, thanks!
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new App();
}
});
}
}
public class App extends JFrame {
private MainPanel mainPanel;
private SecondPanel secondPanel;
public App() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
secondPanel = new SecondPanel(mainPanel);
mainPanel = new MainPanel(secondPanel);
add(mainPanel);
}
}
public class MainPanel extends JPanel {
private JTextArea textArea;
private JScrollPane scrollPane;
private JButton options;
public MainPanel(SecondPanel secondPanel) {
textArea = new JTextArea();
textArea.setColumns(20);
textArea.setRows(5);
textArea.setSize(300, 300);
textArea.setVisible(true);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
scrollPane.setSize(new Dimension(400, 400));
options = new JButton("Options");
options.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
secondPanel.setVisible(true);
}
});
add(scrollPane);
add(options);
}
public JTextArea getTextArea() {
return textArea;
}
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
}
public class SecondPanel extends JFrame {
private JButton create, remove;
private JPanel panel;
public SecondPanel(MainPanel mainPanel) {
setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
panel = new JPanel();
panel.setLayout(new BorderLayout());
create = new JButton("create");
create.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mainPanel.getTextArea().append("New Player Created");
}
});
remove = new JButton("remove");
remove.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mp.getTextArea().setText("Player removed");
}
});
add(panel);
panel.add(create, BorderLayout.EAST);
panel.add(remove, BorderLayout.WEST);
}
}
Well, it has to be "null", because you don't initialise your MainPanel before you hand it over to the secondPanel.
Instead, make an own method for setting the MainPanel on the secondPanel and to set the secondPanel on the MainPanel.
I see that you need the secondPanel for instance in your constructor to set an ActionListener. Do that in your "setSecondPanel(SecondPanel sPanel)"-Method which, as I mentioned before, you should create.

How to use CardLayout with multiple JButtons?

I need to make a GUI that asks the users for details and then save them in a linked list. I wanted to use the CardLayout to switch from one frame to another, which is something I'm doing for the first time. I have done probably less half of what I need to do and here I am quite lost in this part. The code below compiles and executes but when I click the buttons, the desired change does not happen . What could be wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDatabaseWindow extends JPanel{
public static final String FRONT_PAGE = "Front Page";
public static final String BROWSE_MEMORIES = "Browse Memories";
public static final String ADD_EDIT = "Add Edit";
public static final String VIEW_MEMORY = "View Memory";
public static void createAndShowGUI() {
final MyDatabaseWindow mdbw = new MyDatabaseWindow();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JButton browseButton = new JButton("Browse");
final JButton searchButton = new JButton("Search");
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(browseButton);
buttonPanel.add(searchButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToAddPage();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToSearch();
}
});
JFrame frame = new JFrame("Memory Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 540);
frame.setLocation(250, 100);
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);
public MyDatabaseWindow() {
Window1 win1 = new Window1();
cardShowingPanel.add(win1, FRONT_PAGE);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, BROWSE_MEMORIES);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, ADD_EDIT);
Window4 win4 = new Window4();
cardShowingPanel.add(win4, VIEW_MEMORY);
setLayout(new BorderLayout());
add(cardShowingPanel, BorderLayout.NORTH);
}
public void goToAddPage() {
cardLayout.first(cardShowingPanel);
}
public void goToBrowse() {
cardLayout.first(cardShowingPanel);
cardLayout.next(cardShowingPanel);
}
public void goToSearch() {
cardLayout.last(cardShowingPanel);
}
public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}
}
class Window1 extends JPanel {
public Window1() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window2 extends JPanel {
public Window2() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window3 extends JPanel {
public Window3() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window4 extends JPanel {
public Window4() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
The main problem is the use of null-layouts and these lines of code:
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
First you add the panel using the CardLayout to BorderLayout.CENTER, then you "overlay" it with your buttonPanel, which is using null-layout.
I would go with a simple FlowLayout (the default layout-manager for a JPanel) for the buttonPanel and add it to the BorderLayout.SOUTH of the contentPane. I would also strongly recommend reading this tutorial.
So remove the following lines of code:
buttonPanel.setLayout(null);
...
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
and change frame.getContentPane().add(buttonPanel); to frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);.
Also forget about the null-layout / setBounds() in your Window-classes.
(Note that you still won't see the text change if you press a button because you always add a JLabel with the same text ("Memory Files") to your Windows.)

Can I use JButtons added to a JToolBar to scroll up and down a JPanel?

Can I scroll a JPanel using JButtons added to a JToolBar?
When I generate a large number of thumbnails, they don't all fit onto the JPanel. I want to use an up/down arrow JButton to scroll. Can this be done, and if so, how?
NOTE: I am trying to do this without a JScrollPane because I want the custom arrow icons, not a standard scroll bar.
Here is an SSCCE:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class PicSlider2 {
private JButton thumbs;
private JButton[] thumbnails;
private JLabel picViewer;
private JPanel thumbPanel;
private JToolBar toolBar;
public PicSlider2() {
final JFrame frame = new JFrame("Picture Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 600));
frame.setResizable(false);
frame.setLayout(new BorderLayout());
picViewer = new JLabel();
picViewer.setText("Image here");
picViewer.setHorizontalAlignment(JLabel.CENTER);
picViewer.setVerticalAlignment(JLabel.BOTTOM);
picViewer.setBorder(new LineBorder(Color.BLACK, 2));
JMenuBar frameMenuBar = new JMenuBar();
frame.setJMenuBar(frameMenuBar);
JMenu file = new JMenu("File");
frameMenuBar.add(file);
JMenuBar picViewerMenu = new JMenuBar();
picViewerMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
thumbs = new JButton("THUMBNAILS");//an icon in actual program
thumbs.setPreferredSize(new Dimension(150,45));
thumbs.setToolTipText("Thumbnails");
thumbs.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
picViewer.setVisible(false);
thumbPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
thumbPanel.setBorder(BorderFactory.createEmptyBorder(50,100,50,30));
thumbnails = new JButton[30];//example size, chosen so all buttons won't fit on one page
for (int i = 0; i < 30; i++) {
thumbnails[i] = new JButton(Integer.toString(i));
thumbnails[i].setPreferredSize(new Dimension(100, 100));
thumbnails[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("thumbnail clicked - opens full-size view of pic in the JLabel picViewer");
}
});
thumbPanel.add(thumbnails[i]);
thumbPanel.setVisible(true);
}
toolBar = new JToolBar(null, JToolBar.VERTICAL);
toolBar.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 30));
JButton up = new JButton("Up Arrow");
up.setPreferredSize(new Dimension(80,60));
up.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Up Arrow Stub - NEEDS TO SCROLL UP PAGE, as needed");
}
} );
JButton down = new JButton("Down Arrow");
down.setPreferredSize(new Dimension(80,60));
down.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Down Arrow Stub - NEEDS TO SCROLL DOWN PAGE, as needed");
}
} );
toolBar.add(Box.createGlue());
toolBar.add(up);
toolBar.add(Box.createVerticalStrut(40));
toolBar.add(down);
toolBar.add(Box.createGlue());
frame.getContentPane().add(thumbPanel, BorderLayout.CENTER);
frame.getContentPane().add(toolBar, BorderLayout.LINE_END);
}
});
picViewerMenu.add(thumbs);
frame.getContentPane().add(picViewerMenu, BorderLayout.SOUTH);
frame.add(picViewer, BorderLayout.CENTER);
frame.setLocation(300, 50);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
PicSlider2 ps = new PicSlider2();
}
});
}
}
I am trying to do this without a JScrollPane because I want the custom arrow icons
You can use a JScrollPane and use the default scroll Action to create a button with your custom Icon:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
public class ScrollPaneSSCCE extends JPanel
{
public ScrollPaneSSCCE()
{
setLayout( new BorderLayout() );
JTable table = new JTable(50, 5);
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
add(scrollPane);
JScrollBar vertical = scrollPane.getVerticalScrollBar();
JPanel east = new JPanel( new BorderLayout() );
add(east, BorderLayout.EAST);
JButton north = new JButton( new ActionMapAction("UP", vertical, "negativeUnitIncrement") );
east.add(north, BorderLayout.NORTH);
JButton south = new JButton( new ActionMapAction("DOWN", vertical, "positiveUnitIncrement") );
east.add(south, BorderLayout.SOUTH);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("ScrollPaneSSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ScrollPaneSSCCE());
frame.setSize(200, 300);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
You will need the Action Map Action class which is just a simple wrapper class that gets the default Action from the ActionMap of the specified component.
You will need to set the scroll increment for you panel. Since your image size is 100 you might want to use:
vertical.setUnitIncrement( 100 );
I found a way to achieve your desired result by reordering the items on the JPanel. The JFrame needs to be declared outside of the constructor. I added an index variable for tracking the scroll position and a reloadThumbs() method for reloading the reordered thumbs.
This is the entire code after the changes:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class PicSlider2 {
private JButton thumbs;
private JButton[] thumbnails;
private JLabel picViewer;
private JPanel thumbPanel;
private JToolBar toolBar;
private int scrollIndex;
private final JFrame frame;
public PicSlider2() {
scrollIndex = 0;
frame = new JFrame("Picture Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 600));
frame.setResizable(false);
frame.setLayout(new BorderLayout());
picViewer = new JLabel();
picViewer.setText("Image here");
picViewer.setHorizontalAlignment(JLabel.CENTER);
picViewer.setVerticalAlignment(JLabel.BOTTOM);
picViewer.setBorder(new LineBorder(Color.BLACK, 2));
JMenuBar frameMenuBar = new JMenuBar();
frame.setJMenuBar(frameMenuBar);
JMenu file = new JMenu("File");
frameMenuBar.add(file);
JMenuBar picViewerMenu = new JMenuBar();
picViewerMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
thumbs = new JButton("THUMBNAILS");//an icon in actual program
thumbs.setPreferredSize(new Dimension(150,45));
thumbs.setToolTipText("Thumbnails");
thumbs.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
picViewer.setVisible(false);
thumbPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
thumbPanel.setBorder(BorderFactory.createEmptyBorder(50,100,50,30));
thumbnails = new JButton[30];//example size, chosen so all buttons won't fit on one page
for (int i = 0; i < 30; i++) {
thumbnails[i] = new JButton(Integer.toString(i));
thumbnails[i].setPreferredSize(new Dimension(100, 100));
thumbnails[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("thumbnail clicked - opens full-size view of pic in the JLabel picViewer");
}
});
thumbPanel.add(thumbnails[i]);
thumbPanel.setVisible(true);
}
toolBar = new JToolBar(null, JToolBar.VERTICAL);
toolBar.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 30));
JButton up = new JButton("Up Arrow");
up.setPreferredSize(new Dimension(80,60));
up.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Up Arrow Stub - NEEDS TO SCROLL UP PAGE, as needed");
if(scrollIndex > 3) scrollIndex -= 4;
else scrollIndex = 0;
reloadThumbs();
}
} );
JButton down = new JButton("Down Arrow");
down.setPreferredSize(new Dimension(80,60));
down.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Down Arrow Stub - NEEDS TO SCROLL DOWN PAGE, as needed");
if(scrollIndex < 27) scrollIndex += 4;
else scrollIndex = 30;
reloadThumbs();
}
} );
toolBar.add(Box.createGlue());
toolBar.add(up);
toolBar.add(Box.createVerticalStrut(40));
toolBar.add(down);
toolBar.add(Box.createGlue());
frame.getContentPane().add(thumbPanel, BorderLayout.CENTER);
frame.getContentPane().add(toolBar, BorderLayout.LINE_END);
}
});
picViewerMenu.add(thumbs);
frame.getContentPane().add(picViewerMenu, BorderLayout.SOUTH);
frame.add(picViewer, BorderLayout.CENTER);
frame.setLocation(300, 50);
frame.pack();
frame.setVisible(true);
}
private void reloadThumbs(){
thumbPanel.removeAll();
for(int i = scrollIndex; i < 30; ++i){
thumbPanel.add(thumbnails[i]);
}
for(int i = 0; i < scrollIndex; ++i){
thumbPanel.add(thumbnails[i]);
}
thumbPanel.revalidate();
frame.revalidate();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
PicSlider2 ps = new PicSlider2();
}
});
}
}

How to add checkbox to the right side of jButton?

I want to create a button with checkbox in the right side of it.
i tried this but checkbox stays on the center of button on the top of button label text.
Any ideas welkom.
thanks in advance:
public class MainTest extends JPanel {
JButton button;
JPanel panel;
public MainTest() {
createComponents();
layoutComponents();
}
public void createComponents() {
// attempting to add checkbox to button
button = new JButton("Print with Edge");
JCheckBox checkBox = new JCheckBox();
jcb.setHorizontalAlignment(SwingConstants.RIGHT);
button.add(checkBox,new BorderLayout());
panel = new JPanel(new BorderLayout());
}
public void layoutComponents() {
panel.add(button,BorderLayout.SOUTH);
add(panel);
}
public static void main(String[] args) {
MainTest demo = new MainTest();
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.add(demo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
You can wrap a JCheckBox inside a JPanel and make the JPanel look like a button. For example:
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(100, 100));
JCheckBox button = new JCheckBox();
final JPanel buttonWrapper = new JPanel();
buttonWrapper.add(new JLabel("Button Text"));
buttonWrapper.add(button);
buttonWrapper.setBorder(BorderFactory.createRaisedBevelBorder());
buttonWrapper.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent me) {
buttonWrapper.setBorder(BorderFactory.createEtchedBorder());
}
#Override
public void mouseReleased(MouseEvent me) {
buttonWrapper.setBorder(BorderFactory.createRaisedBevelBorder());
}
#Override
public void mouseClicked(MouseEvent me) {
System.out.println("mouse clicked");
}
});
JPanel mainPanel = new JPanel();
mainPanel.add(buttonWrapper);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
}
}
I want to create a button with checkbox in the right side of it.
Maybe you just want the checkbox on the right side of the text?
If so you can do:
JCheckBox cb = new JCheckBox("Print with Edge");
cb.setHorizontalTextPosition(SwingConstants.LEADING);

Changing the text in the title bar

I'm getting an error at the line: jFrame cannot be resolved
jFrame.setTitle(titleName.getText());
public void createOption(){
Option = new JPanel();
Option.setLayout( null );
JLabel TitleLabel = new JLabel("Change the company name");
TitleLabel.setBounds(140, 15, 200, 20);
Option.add(TitleLabel);
titleName = new JTextField();
titleName.setBounds(90,40,260,20);
Option.add(titleName);
JButton Change = new JButton("Change New Name");
Change.setBounds(90,80,150,20);
Change.addActionListener(this);
Change.setBackground(Color.white);
Option.add(Change);
JButton Exit = new JButton("Exit");
Exit.setBounds(270,80,80,20);
Exit.addActionListener(this);
Exit.setBackground(Color.white);
Option.add(Exit);
Change.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(titleName.getText());
}
});
}
You will have to have a reference to your JFrame. Assuming button and textBox for the names for the controls, you can do
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(textBox.getText());
}
});
EDIT:
Here's a full example
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JFrameExample {
public static void main(String[] args) {
final JFrame jFrame = new JFrame("This is a test");
jFrame.setSize(400, 150);
Container content = jFrame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
final JTextField jTextField = new JTextField("TestTitle");
content.add(jTextField);
final JButton button = new JButton("Change");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jFrame.setTitle(jTextField.getText());
}
});
content.add(button);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}

Categories

Resources