Using a JButton to change an interface in Java - java

The idea is there are 5 JButtons on the top of the screen. Every time you press one it removes the old menu and produces a new one. To me, my code looks fine. The problem is when i click on "Add CD" nothing happens, but then if i manually resize the window (moving my mouse to the edge of the window and changing the size of it) the new menu pops up... Can anyone give me advice on how to fix this problem... Thanks, the whole runnable code is below.
package ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class mainInterface extends JFrame implements ActionListener
{
// Variables declaration - do not modify
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton4;
private JButton jButton5;
private JButton jButton6;
private JPanel jPanel1;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JTextField text5;
private JTextField text6;
private JTextField text7;
private JTextField text8;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
private JLabel label6;
private JLabel label7;
private JLabel label8;
private JPanel bottomPanel;
private JPanel topPanel; // declaring panels
private JPanel holdAll;
private JPanel one;
private JPanel two;
private JPanel three;
private JPanel four;
//----------------------------------------------------------
//----------------------------------------------------------
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
mainInterface myApplication = new mainInterface();
myApplication.setLocation(100, 100);
myApplication.setSize(700, 400);
myApplication.setTitle("Kevin's Jukebox");
myApplication.setVisible(true);
}
});
}
//----------------------------------------------------------
//----------------------------------------------------------
/** Creates new form mainInterface */
public mainInterface()
{
jButton1 = new JButton("Add CD");
jButton2 = new JButton("Add Video");
jButton3 = new JButton("Total Play Time");
jButton4 = new JButton("Create Playlist");
jButton5 = new JButton("Show Library");
jButton6 = new JButton("Quit");
topPanel = new JPanel();
holdAll = new JPanel();
bottomPanel = new JPanel();
one = new JPanel();
two = new JPanel();
three = new JPanel();
four = new JPanel();
text1 = new JTextField(15);
label1 = new JLabel("Title: ");
text2 = new JTextField(15);
label2 = new JLabel("Artist: ");
text3 = new JTextField(15);
label3 = new JLabel("Length: ");
text4 = new JTextField(15);
label4 = new JLabel("Num of Tracks: ");
label5 = new JLabel("Welcome to Kevins Jukebox");
int flag = 0;
drawApp(flag);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jButton4.addActionListener(this);
jButton5.addActionListener(this);
jButton6.addActionListener(this);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
//----------------------------------------------------------
//----------------------------------------------------------
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jButton1)
{
int flag = 1;
drawApp(flag);
}
}
//----------------------------------------------------------
//----------------------------------------------------------
public void drawApp(int flag)
{
topPanel.setLayout(new FlowLayout());
topPanel.add(jButton1);
topPanel.add(jButton2);
topPanel.add(jButton3);
topPanel.add(jButton4);
topPanel.add(jButton5);
topPanel.add(jButton6);
bottomPanel.add(label5);
holdAll.setLayout(new BorderLayout());
holdAll.add(topPanel, BorderLayout.NORTH);
holdAll.add(bottomPanel, BorderLayout.CENTER);
if (flag == 0)
bottomPanel.add(label5);
else
bottomPanel.remove(label5);
if (flag == 1)
{
one.add(label1);
one.add(text1);
bottomPanel.add(one);
two.add(label2);
two.add(text2);
bottomPanel.add(two);
three.add(label3);
three.add(text3);
bottomPanel.add(three);
four.add(label4);
four.add(text4);
bottomPanel.add(four);
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
}
getContentPane().add(holdAll, BorderLayout.CENTER); // places everything on the frame
}
//----------------------------------------------------------
}

Execute this.validate() after you update the UI's layout. Calling this method tells the UI to relayout its components.

Related

Problem with Border Layout component positioning

I really don't know how to make the south panel appear without make the buttons go up. Maybe i need to change the layout from border layout to another one, I don't know...
---------->My Code:
public class a1211 extends JFrame {
private final JButton OK;
private final JButton cancel;
private final JButton setup;
private final JButton help;
private final JPanel buttonPanel;
private final JLabel printQuality;
private final JComboBox<String> quality;
private final String[] qualityNames = { "High", "Low", "Medium" };
private final JCheckBox printToFile;
private final JPanel southPanel;
public a1211() {
super("Printer");
OK = new JButton("OK");
cancel = new JButton("Cancel");
setup = new JButton("Setup...");
help = new JButton("Help");
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 1, 5, 5));
buttonPanel.add(OK);
buttonPanel.add(cancel);
buttonPanel.add(setup);
buttonPanel.add(help);
printQuality = new JLabel("Print Quality:");
quality = new JComboBox<String>(qualityNames);
printToFile = new JCheckBox("Print To File");
southPanel = new JPanel();
southPanel.add(printQuality);
southPanel.add(quality);
southPanel.add(printToFile);
add(buttonPanel, BorderLayout.EAST);
add(southPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
a1211 a1211 = new a1211();
a1211.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1211.setSize(400, 150);
a1211.setVisible(true);
}
}
---------->What this code make:
---------->What it should make:

How can I switch JFrames by clicking a JButton in Java?

I am writing a program and am unable to figure this out but I have a JButton called nextDay and I need it set up so once I click it, it switches to the JFrame day2 as the program starts out on day1. Any help is appreciated.
Here is my code. I have a separate Main method which I wont include as it shouldn't need to be changed
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SoldierSimTest extends JFrame {
private final JButton decision1;
private final JButton decision2;
private final JButton decision3;
private final JButton decision4;
private final JTextField situation;
private JFrame day1;
private JFrame day2;
private JFrame day3;
private JFrame day4;
private JFrame day5;
private JFrame day6;
private JFrame day7;
private final JButton nextDay;
private final JButton exitGame;
private final JButton newGame;
public SoldierSimTest() {
decision1 = new JButton("Storm it");
decision2 = new JButton("Sneak around the flank");
decision3 = new JButton("Sneak up and grenade spam 'em");
decision4 = new JButton("Just dont");
situation = new JTextField("You and your squad are ordered to take
an enemy fort. How will you do so?");
situation.setEditable(false);
nextDay = new JButton("Next Day");
exitGame = new JButton("Exit Game");
newGame = new JButton("New Game");
JPanel decisionsPanel = new JPanel();
decisionsPanel.setLayout(new GridLayout(2, 2));
decisionsPanel.add(decision1);
decisionsPanel.add(decision2);
decisionsPanel.add(decision3);
decisionsPanel.add(decision4);
JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(1, 3));
optionsPanel.add(newGame);
optionsPanel.add(exitGame);
optionsPanel.add(nextDay);
JPanel situationsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(1, 1));
situationsPanel.add(situation);
Container contentPane = getContentPane();
contentPane.add(decisionsPanel, "South");
contentPane.add(optionsPanel, "North");
contentPane.add(situationsPanel, "Center");
}
}
You can use card layout to switch panels. I used Jpanels as it seems to be the best option to use. For this example I used 2 panels.
public class SoldierSimTest extends JFrame
{
private final JButton decision1;
private final JButton decision2;
private final JButton decision3;
private final JButton decision4;
private final JTextField situation;
private JPanel day1Panel = new JPanel();
private JPanel day2Panel = new JPanel();
private final JButton nextDay;
private final JButton exitGame;
private final JButton newGame;
final static String DAY1 = "Day1";
final static String DAY2 = "Day2";
public SoldierSimTest()
{
decision1 = new JButton("Storm it");
decision2 = new JButton("Sneak around the flank");
decision3 = new JButton("Sneak up and grenade spam 'em");
decision4 = new JButton("Just dont");
situation = new JTextField("You and your squad are ordered to take an enemy fort. How will you do so?");
situation.setEditable(false);
JPanel decisionsPanel = new JPanel();
decisionsPanel.setLayout(new GridLayout(2, 2));
decisionsPanel.add(decision1);
decisionsPanel.add(decision2);
decisionsPanel.add(decision3);
decisionsPanel.add(decision4);
JPanel situationsPanel = new JPanel();
situationsPanel.add(situation);
day1Panel.setLayout(new GridLayout(2, 1));
day1Panel.add(situationsPanel);
day1Panel.add(decisionsPanel);
JPanel cards = new JPanel(new CardLayout());
cards.add(day1Panel, DAY1);
cards.add(day2Panel, DAY2);
nextDay = new JButton("Next Day");
exitGame = new JButton("Exit Game");
newGame = new JButton("New Game");
nextDay.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, DAY2);
}
});
JPanel optionsPanel = new JPanel();
optionsPanel.setLayout(new GridLayout(1, 3));
optionsPanel.add(newGame);
optionsPanel.add(exitGame);
optionsPanel.add(nextDay);
Container contentPane = getContentPane();
contentPane.add(optionsPanel, "North");
contentPane.add(cards, "Center");
}
}
The best way to do this if you don't necessarily need a new JFrame is the way #pavithraCS described. What I want to add is that normally, when you want a new "window" with different components to appear, you don't use a new JFrame because that opens a new window. Instead, using a new JPanel is more useful because you can stack them without having to switch to another window.
I hope this helps for the future.

Cannot see anything in Flow Layout

I coded a application so far so it only shows the widgets so I can see if everything is in order before I add Action Listeners and such.
The problem is when I run it, nothing shows up at all, just a blank screen.
I attached a picture of what the layout should look like. I am new to this website so apparently I cannot post any images until 10 rep. Here is a link to PhotoBucket.
http://i930.photobucket.com/albums/ad149/Wolverino5/pic_zpsa4599592.png
Here is my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Project9 extends JFrame
{
private JTextField idLabel;
private JTextField idField;
private JTextField priceLabel;
private JTextField priceField;
private JTextField numInStockLabel;
private JTextField numInStockField;
private JTextField codeLabel;
private JTextField codeField;
private JTextField transaction;
private JButton insert;
private JButton delete;
private JButton display;
private JButton displayOne;
private JButton hide;
private JButton clear;
private Container c = getContentPane();
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(650,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Project9()
{
setTitle("BOOK DataBase Application");
c.setLayout(new FlowLayout());
Label main = new Label("Data Entry: Best Bargian Book Store");
c.add(main);
idLabel = new JTextField("Enter ID",15);
idLabel.setEditable(false);
c.add(idLabel);
idField = new JTextField();
c.add(idField);
priceLabel = new JTextField("Enter Price:",15);
priceLabel.setEditable(false);
c.add(priceLabel);
priceField = new JTextField();
c.add(priceField);
numInStockLabel = new JTextField("Enter Number In Stock:",15);
numInStockLabel.setEditable(false);
c.add(numInStockLabel);
numInStockField = new JTextField();
c.add(numInStockField);
codeLabel = new JTextField("Enter Code: 1,2,3 or 4:",15);
codeLabel.setEditable(false);
c.add(codeLabel);
codeField = new JTextField();
c.add(codeField);
insert = new JButton("Insert");
c.add(insert);
delete = new JButton("Delete");
c.add(delete);
display = new JButton("Display");
c.add(display);
displayOne = new JButton("displayOne");
c.add(displayOne);
hide = new JButton("Hide");
c.add(hide);
clear = new JButton("Clear");
c.add(clear);
Label messages = new Label ("Messages");
c.add(messages);
transaction = new JTextField();
c.add(transaction);
}
}
You never create an instance of the Project9 class, so the only four lines that run are those in main that create an empty JFrame.
Change:
JFrame frame = new JFrame();
To:
JFrame frame = new Project9();

how to select JRadioButton depending on button

see my problem start form this piece of code i add all the addActionListener for the button
but when it come to the Radio button it use addItemListenet but i implements ActionListener only how i will implements ItemListener so i can set Law when ever the user Select sw form the radio button and click on add item~ it will add the item to the right array i made before
exitButton.addActionListener(new ButtonWatcher());
addButton.addActionListener(new ButtonWatcher());
copyButton.addActionListener(new ButtonWatcher());
showButton.addActionListener(new ButtonWatcher());
rButton.addItemListenet(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(exitButton))
{
System.exit(0);
}
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
//do the action
}
full code
package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* #author isslam
*/
public class MyFrameMain extends JFrame{
Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title){
setSize(500, 500);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15,20);
reSoulte.setEditable(false);
reSoulte.setText("Array is empty");
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel rbPanel = new JPanel(new GridLayout(5,1));
rbPanel.add(nLabel);
rbPanel.add(iLabel);
rbPanel.add(rButton1);
rbPanel.add(rButton2);
rbPanel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2,2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5,1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(rbPanel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
pane.add(mainPanel);
exitButton.addActionListener(new ButtonWatcher());
addButton.addActionListener(new ButtonWatcher());
copyButton.addActionListener(new ButtonWatcher());
showButton.addActionListener(new ButtonWatcher());
//rButton.addItemListenet(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(exitButton))
{
System.exit(0);
}
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
//do the action
}
}
}
}
I'm not sure what array you want to fill but get the text with getText()
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
String s1 = iJTextField.getText();
String s2 = nJTextField.getText();
String s3 = swTextField.getText();
String s4 = hwTextField.getText();
// something with these strings
}
If any of the inputs are numbers and you want the numerical value, you need to parse.
Also, these need to be declared as class memebers. You have them declared in the constructor
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
Declared in the constructor, they are not within the scope of the listener class
public class MyFrameMain extends JFrame{
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
public MyFrameMain(String title){
Also, doesn't really look like you need a listener for the radio button, since an event is not necessary. The JButton listens for an event, and in the actionPerformed, it checks if the radio button is selected. Therefore no need for the radio button to listen for any event, the JButton does that.
Try following code. I ahve added a List item and adding values from swTextField TextFiled to item when user select rButton1 and click on addButton button
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
/**
*
* #author isslam
*/
public class Test extends JFrame {
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
JRadioButton rButton1;
java.util.List<String> item = new ArrayList<String>();
public Test(String title) {
setSize(500, 500);
setTitle(title);
setDefaultCloseOperation(Test.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15, 20);
reSoulte.setEditable(false);
reSoulte.setText("Array is empty");
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
rButton1 = new JRadioButton("SW Version", false);
JRadioButton rButton2 = new JRadioButton("HW Type", false);
JRadioButton rButton3 = new JRadioButton("General", true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel rbPanel = new JPanel(new GridLayout(5, 1));
rbPanel.add(nLabel);
rbPanel.add(iLabel);
rbPanel.add(rButton1);
rbPanel.add(rButton2);
rbPanel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2, 2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(rbPanel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
pane.add(mainPanel);
exitButton.addActionListener(new ButtonWatcher());
addButton.addActionListener(new ButtonWatcher());
copyButton.addActionListener(new ButtonWatcher());
showButton.addActionListener(new ButtonWatcher());
//rButton.addItemListenet(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener {
public void actionPerformed(ActionEvent a) {
Object buttonPressed = a.getSource();
if (buttonPressed.equals(exitButton)) {
System.exit(0);
}
if (buttonPressed.equals(addButton) && rButton1.isSelected()) {
item.add(swTextField.getText());
System.out.println(item);
}
}
}
public static void main(String args[]) {
Test t = new Test("Test");
t.setVisible(true);
}
}
Use ButtonGroup with the JRadioButton of your context.
Use jRadioButton.setActionCommand(String) to set their corresponding action name: for your context "SW Version" and anything such.
Make use of an ArrayList to add the item of your context. Try mapping each such array list using a HashMap<Key, Val> i.e., HashMap<String, ArrayList<Equipment>> where the "SW Version" or anything such name will be the key
Try adding listeners to each action button in-line using the means of anonymous class.
So a sample coding for add action would become depicting the usage(usefulness) of ButtonGroup:
addButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String actionCommand = buttonGroup1.getSelection()
.getActionCommand();
// actionCommand = "SW Version"
map.get(actionCmmand).add(equipment);
}
});
Tutorial and reference:
How to use Radio Button, check the demo for ButtonGroup
ButtonGroup class
HashMap

Can't figure out how to output result to JLabel instead of System.out.println

I'm stuck again trying to figure out how to out the button clicked to label1.
When I click button01 I get A printed out. I need it to print to the label1 though and say Folder A when A is pressed and B when B is pressed and so on. Any nudge in the right direction?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JFileCabinet extends JFrame {
private JButton button01 = new JButton("A");
private JButton button02 = new JButton("B");
private JButton button03 = new JButton("C");
private JButton button04 = new JButton("D");
private JButton button05 = new JButton("E");
private JButton button06 = new JButton("F");
private JButton button07 = new JButton("G");
private JButton button08 = new JButton("H");
private JButton button09 = new JButton("I");
private JButton button10 = new JButton("J");
private JButton button11 = new JButton("K");
private JButton button12 = new JButton("L");
private JButton button13 = new JButton("M");
private JButton button14 = new JButton("N");
private JButton button15 = new JButton("O");
private JButton button16 = new JButton("P");
private JButton button17 = new JButton("Q");
private JButton button18 = new JButton("R");
private JButton button19 = new JButton("S");
private JButton button20 = new JButton("T");
private JButton button21 = new JButton("U");
private JButton button22 = new JButton("V");
private JButton button23 = new JButton("W");
private JButton button24 = new JButton("X");
private JButton button25 = new JButton("Y");
private JButton button26 = new JButton("Z");
private JButton button27 = new JButton(" ");
private JButton button28 = new JButton(" ");
private JButton button29 = new JButton(" ");
private JButton button30 = new JButton(" ");
private static JLabel label1 = new JLabel("Folder ");
private JPanel panel01 = new JPanel(new GridLayout(1, 6));
private JPanel panel02 = new JPanel(new GridLayout(1, 6));
private JPanel panel03 = new JPanel(new GridLayout(1, 6));
private JPanel panel04 = new JPanel(new GridLayout(1, 6));
private JPanel panel05 = new JPanel(new GridLayout(1, 2));
private GridLayout layout = new GridLayout(5, 1, 5, 5);
public JFileCabinet() {
setLayout(layout);
add(panel01);
add(panel02);
add(panel03);
add(panel04);
add(panel05);
button01.addActionListener(new ButtonListener());
panel01.add(button01);
panel01.add(button02);
panel01.add(button03);
panel01.add(button04);
panel01.add(button05);
panel01.add(button06);
panel02.add(button07);
panel02.add(button08);
panel02.add(button09);
panel02.add(button10);
panel02.add(button11);
panel02.add(button12);
panel03.add(button13);
panel03.add(button14);
panel03.add(button15);
panel03.add(button16);
panel03.add(button17);
panel03.add(button18);
panel04.add(button19);
panel04.add(button20);
panel04.add(button21);
panel04.add(button22);
panel04.add(button23);
panel04.add(button24);
panel05.add(button25);
panel05.add(button26);
panel05.add(button27).setVisible(false);
panel05.add(button28).setVisible(false);
//panel05.add(button29).setVisible(false);
panel05.add(label1);
panel05.add(button30).setVisible(false);
setSize(400, 350);
setVisible(true);
}
public static void main(String[] args) {
JFileCabinet frame = new JFileCabinet();
}
String selection;
class ButtonListener implements ActionListener {
private String e;
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
String clicked = null;
if (e.getActionCommand().equals("A")) { //clicked.equals("A");
System.out.println("A");
}
}
}
}
label1.setText("Folder " + e.getActionCommand());
The nudge - and not the full blown answer is this:
Go and read the public API for JLabel
Identify the method that sets the text on JLabel
Instead of checking for an A string value, just set the value of folder to the value of e.getActionCommand().

Categories