Im trying to get it so that when i click on a menu option, the windows changes from the 'welcome text' to 4 buttons which the user can then click on. However, when i click on the simulation button, nothing happens.. the window doesnt change at all.. Ive summarized my code to the basic stuff by the way. anyone see anything i cant?
JFrame GUI = new JFrame("Graphical User Interface");
public gui()
{
JMenuBar menubar = new JMenuBar();
JMenu Simulation = new JMenu("Simulation");
theLabel = new JLabel("Welcome to the Main Menu. ",JLabel.CENTER);
GUI.add(theLabel);
menubar.add(Simulation);
Simulation.add(Simulationmenu);
Simulationmenu.addActionListener(this);
GUI.setJMenuBar(menubar);
GUI.setLocation(500,250);
GUI.setSize(300, 200);
GUI.setVisible(true);
}
public void actionPerformed(ActionEvent E){
if(E.getSource() == Simulationmenu){
// Buttons in the menu i want to output once clicked on 'simulation'
thePanel = new JPanel(new GridLayout(4,0));
Run = new JButton("Run");
Pause = new JButton("Pause");
Reset = new JButton("Reset");
DisplayMaps = new JButton("Display Maps?");
// Add the components to the panel:
thePanel.add("West", Run);
thePanel.add("Center", Pause);
thePanel.add("East", Reset);
thePanel.add("West", DisplayMaps);
// Add the panel to the contentPane of the frame:
GUI.add(thePanel);
// add this object as listener to the two buttons:
Run.addActionListener(this);
Seems your problem in next, you add a new panel(thePanel) to your JFrame(GUI) when it is showing, but in this case you must to call revalidate() method of JFrame(GUI).
Add GUI.revalidate() after GUI.add(thePanel);, it helps you.
Related
I am working on making a gui chat app in java (I know too ambitious) that jus sends a message to the connected room. I have not yet done the networking stuff but jus the bare bones gui. I have a text field where the input message will be and a text area where the sent message will go with a button to send it. I want it so that when I press the enter key the sending task takes place. My code is
import javax.swing.*;
import java.awt.*;
public class Jabba {
//Class
public static void main(String args[]) {
//Main Method
//main frame
JFrame frame = new JFrame("Chat Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
//The Menu that will later allow us to connect and create rooms to join a chat
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("Connect");
JMenu m2 = new JMenu("Help");
//This is for help
mb.add(m1);
mb.add(m2);
JMenuItem m11 = new JMenuItem("Create new room");
JMenuItem m22 = new JMenuItem("Join an Existing Room");
m1.add(m11);
m1.add(m22);
//Our panel
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter Text");
//The Message field
JTextField tf = new JTextField(15);
//The Sending button
JButton send = new JButton("Send");
//The resetting button
JButton reset = new JButton("Reset");
//Adding the panel
panel.add(label);
panel.add(tf);
panel.add(send);
panel.add(reset);
//The text area where the messages will go
JTextArea ta = new JTextArea();
//Adding it to the Scroll Pane so that It has scrolls
JScrollPane sp = new JScrollPane(ta);
//Actionlisteners allow us to listen to the actions that take place with
//The Specific components like here when the button is pressed
send.addActionListener(e ->{
//It will first store the text of the the text field in a
//variable called msg
String msg = tf.getText();
//Then Will remove the Text from the field so that new messages can be
//sent
tf.setText(null);
//Now it will send The message to the message text area
ta.append(msg+"\n");
});
reset.addActionListener(e ->{
//This is for the reset option
ta.setText(null);
//It will jus set all the text of the message area to null
//i.e. Nothing
}
);
//adds all the content and components to the main frame.
frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.getContentPane().add(BorderLayout.NORTH, mb);
/*notice the BorderLayout here, it is from the awt package and it
is letting us lay the components in a specific layout.
Also we have changed the component below after the center to sp
i.e. it is the scrollpane instead of our textarea to get the Scroll Bars!!*/
frame.getContentPane().add(BorderLayout.CENTER, sp);
frame.setVisible(true);
//Pretty Self-Explanatory
}
}
enter image description here
Please help me and forgive me if I asked the question wrong as I couldn't quite understand how to us the KeyListener classs...## Heading ##
So as Mad Programmer told me and helped me I used an action Listener in that textfield and copied the code I used for the send message in an action listener for the text field var tf. So in pseudocode it is:
tf.addActionListener(e ->{
String msg = tf.getText();
tf.setText(null);
ta.append(msg+"\n");
});
I've read the similar questions regarding this problem, tried few methods but none is working.
I have 2 JFrame forms. I want to input information in the first form and submit it to the database. When I click a button, the second form will open and load the information
When I re-input new information in the first form and click the the button again, I want the second form to reload the new information inputted from the database.
This is my code so far.
time t = new time();
private void OrderButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(t.isVisible()){
t.dispose();
t.revalidate();
t.repaint();
t.setVisible(true);
t.setLocationRelativeTo(null);
}
else{
t.setVisible(true);
t.setLocationRelativeTo(null);
}
You don't need to play with JFrames for that. See below example :
JLabel toe = new JLabel("I'm primary text");
JFrame cow = new JFrame("Primary Window");
JPanel cowpanel = new JPanel();
cowpanel.add(toe);
cow.setContentPane(cowpanel);
cow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
cow.pack();
cow.setVisible(true);
JButton tow = new JButton("Change");
tow.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
toe.setText("Hi!, I'm secondary text!");
}
});
JFrame dog = new JFrame("Secondary Window");
JPanel dogPanel = new JPanel();
dog.setContentPane(dogPanel);
dogPanel.add(tow);
dog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
dog.pack();
dog.setVisible(true);
Clicking the 'Change' button from Frame 2 will change the JLabel's text in Frame 1.
I have a class that extends JFrame and works by adding in 2 panels with BoxLayout buttons, and one JTabbedPane in the center which displays graphs.
I want one of the buttons to remove all current components in the frame and add new ones.
Here are the methods used.
private void createAndShowGraphs() {
ImageIcon createImageIcon(lsuLettersPath); //simple png file to fill one tab
final JTabbedPane jtp = new JTabbedPane();
JLabel iconLabel = new JLabel();
iconLabel.setOpaque(true);
jtp.addTab(null, icon, iconLabel);
//Here is where the errors begin
JPanel menu = new JPanel();
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
//I want this button to remove all components currently in the JFrame and replace them with new components specified in the createAndShowIntro() method
menu.add(new JButton(new AbstractAction("Intro Pane") {
public void actionPerformed(ActionEvent e) {
//I've also tried putting removeAll in the Intro method
removeAll();
createAndShowIntro();
}
}));
add(jtp, BorderLayout.CENTER);
add(menu, BorderLayout.WEST);
pack();
setVisible(true);
}
private void createAndShowIntro() {
System.out.println("Made it to Intro");
//all I want is a blank JLabel with the String "test" to show up
JPanel test = new JPanel();
test.setLayout(new BorderLayout());
JLabel label = new JLabel();
label.setText("test");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
test.add(label);
add(test, BorderLayout.CENTER);
test.revalidate();
label.revalidate();
validate();
test.repaint();
label.repaint();
repaint();
pack();
setVisible(true);
}
When I call createAndShowGraphs() in main() and then hit the 'Intro' button, everything freezes and nothing is actually removed. I know it makes it the Intro method because of the "Made it to Intro" string output to the terminal.
I've tried all kinds of combinations of invalidate(), validate(), revalidate(), repaint() on the labels and on the frame itself. Really frustrated because I don't know how else I'm going to be able to display 3 different screens to switch back and forth between while only actually displaying one at a time.
Thanks for your time.
I have a JTabbedPane with 5 tabs in it. I also got 3 other buttons in same JFrame where my JTabbedPane is added. I want to make the user able move to particular tab when a particular button is clicked. Here is the image example
Now for example if user clicks Button 1 then tab One should be opened and similarly when button 2 is clicked then tab Two should be opened and so for third one.
Here is my code to add these JTabbedPane and buttons.
public class TabsAndButtons
{
public TabsAndButtons()
{
JTabbedPane tabsPane = new JTabbedPane();
tabsPane.add("One", new JPanel());
tabsPane.add("Two", new JPanel());
tabsPane.add("Three", new JPanel());
tabsPane.add("Four", new JPanel());
tabsPane.add("Five", new JPanel());
JPanel Panel = new JPanel();
Panel.add(tabsPane);
JButton Button1 = new JButton("Button 1");
Panel.add(Button1);
JButton Button2 = new JButton("Button 2");
Panel.add(Button2);
JButton Button3 = new JButton("Button 3");
Panel.add(Button3);
JFrame MainFrame = new JFrame("JTabbedPane and Buttons");
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.getContentPane().add(Panel );
MainFrame.pack();
MainFrame.setVisible(true);
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(() -> {
new TabsAndButtons();
});
}
}
The actual purpose of such action is very lengthy and has a lot details which will make the question dull so I am asking the main task where I stucked. Thanks for your kind support and time.
Use the method button.addActionListener() to execute code when a button is clicked. The code that you want to execute is probablytabsPane. setSelectedIndex(i)wherei` is the index of the tab that you want to show.
You might also want to move the JTabbedPane tabsPane into a member variable, or mark it with final, to make sure that it can be accessed from within the action listener.
Add an ActionListener to each of the buttons. Then in the ActionListener you can invoke the setSelected(...) method of the tabbed pane.
Read the section from the Swing tutorial on How to Write an ActionListener for more information and examples.
Also, variable name should NOT start with an upper case character.
Try in the EventListener for the Buttons the Method setSelectedIndex(int index) on your JTabbedPane.
The Referenz: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setSelectedIndex%28int%29
I just started studying some java GUI techniques and I have created a small program which has a menu bar a menu as well as an item within that menu. Now what I want to do is to implement a file chooser so when the item in the menu is selected then the file chooser is executed. The problem is that the file chooser that I created is in another class. Is there any possible way to start my file chooser's class when the item in the menu is clicked?
Here is my code
public class menu {
public static void main(String[] args){
//to menu
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
frame.setJMenuBar(bar);
JMenu search = new JMenu("Browse");
bar.add(search);
final JMenuItem songstoplay = new JMenuItem("Browse Songs To Play");
search.add(songstoplay);
//to outline
final Container content = frame.getContentPane();
content.setLayout(new GridLayout(3, 1));
JLabel text = new JLabel("My first iteration", JLabel.CENTER);
text.setVerticalAlignment(JLabel.TOP);
text.setFont(new Font("Serif", Font.PLAIN, 30));
content.add(text);
JLabel text2 = new JLabel();
text2.setText("List of drives connected: C:/");
text2.setFont(new Font("Serif", Font.PLAIN, 20));
text2.setVerticalAlignment(JLabel.TOP);
content.add(text2);
}
}
If you want to use the JFileChooser, you have to add an eventlistener on your menuitem. Better you take a look at this: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
or the direct link to the code from the example above:
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java