i am trying to get it so when a button is pressed a combo box is displayed in java. here is what i have tried.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class main {
public static void main(String[] args) {
final JFrame testFrame = new JFrame();
testFrame.setSize(300,450);
testFrame.setLocation(150,250);
testFrame.setTitle("My frame");
testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
testFrame.setResizable(true);
testFrame.setVisible(true);
testFrame.setLayout(new FlowLayout());
final JButton testButton = new JButton("show");
testFrame.add(testButton);
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == testButton) {
String[] myArray = {"test","test2"};
JComboBox testingCom = new JComboBox(myArray);
testFrame.add(testingCom);
}
}
}
}
}
Any help would be much appreciated. thank you.
Based in your code, you never addActionListener
to your button. In your ActionListener
, you are creating a new instance of a combobox each time actionPerformed
is called, and i don't if it's that what you want, you may be interested in if it's visible or not.
So you can change your code like this:
final JButton testButton = new JButton("show");
final JComboBox combo = new JComboBox(new String[]{"test1","test2"});
testButton.addActionListener(new ActionListener(){
// this is anonymous class
@Override
public void actionPerformed(ActionEvent evt){
//then you know that is attached to this button
combo.setVisible(!combo.isVisible());
}
});
combo.setVisible(Boolean.FALSE);
testFrame.add(testButton);
testFrame.add(combo);
Simply set the combo visible or invisible in the ActionListener and then revalidate and repaint its container. eg,
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});
final JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(250, 100));
mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {
@Override
public void actionPerformed(ActionEvent arg0) {
myCombo.setVisible(!myCombo.isVisible());
mainPanel.revalidate();
mainPanel.repaint();
}
}));
mainPanel.add(myCombo);
JOptionPane.showMessageDialog(null, mainPanel);
}
}
Perhaps a cleaner way to do it is to use a CardLayout. If you use this, your components won't be shifting on removal and reviewing of the combo. For example:
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Main2 {
public static void main(String[] args) {
final JComboBox<String> myCombo = new JComboBox<String>(new String[]{"Foo", "Bar"});
final CardLayout cardLayout = new CardLayout();
final JPanel cardPanel = new JPanel(cardLayout);
cardPanel.add(myCombo, "combo");
cardPanel.add(new JLabel(), "empty");
final JPanel mainPanel = new JPanel();
mainPanel.add(new JButton(new AbstractAction("Toggle Combo") {
@Override
public void actionPerformed(ActionEvent evt) {
cardLayout.next(cardPanel);
}
}));
mainPanel.add(cardPanel);
JOptionPane.showMessageDialog(null, mainPanel);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.