Drawing on JPanel with JButton, JSpinner, etc - java

So i am trying to make a graphical interface for school, which involves a JMenuBar, JSpinner, and 2 JButtons. Below these objects I'm trying to draw a simple rectangle. I've already tried using the paintComponent method in the class which extends JPanel but it stil doesn't appear.
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
public class A1 {
public final static int DEFAULT_WIDTH = 640;
public final static int DEFAULT_HEIGHT = DEFAULT_WIDTH /12*9;
public static void main(String[] args) {
JFrame frame = new JFrame("A1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT));
frame.requestFocus();
frame.setLayout(new FlowLayout());
frame.setLocationRelativeTo(null);
frame.setResizable(false);
lePanel panel = new lePanel();
frame.add(panel);
frame.setJMenuBar(panel.getMenuBar());
frame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class lePanel extends JPanel {
JMenuBar menuBar;
JButton submit;
JButton check;
JSpinner spinner;
public String test;
public lePanel() {
menuBar = new JMenuBar();
menuBar.add(createFileMenu(new JMenu("File")));
menuBar.add(createHelpMenu(new JMenu("Help")));
spinner = new JSpinner(new SpinnerNumberModel(1, 1, 10, 1));
add(spinner);
submit = new JButton("Submit");
add(submit);
check = new JButton("Check");
add(check);
submit.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit)
{
test = spinner.getValue().toString();
System.out.println("Spinner Submitted");
}
}
});
check.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == check)
{
JOptionPane.showMessageDialog(lePanel.this, test, "Information",JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
private JMenu createHelpMenu(JMenu jMenu) {
JMenu menu = jMenu;
JMenuItem helpItem = new JMenuItem("Help");
menu.add(helpItem);
return menu;
}
private JMenu createFileMenu(JMenu jMenu) {
JMenu menu = jMenu;
JMenuItem newItem = new JMenuItem("New");
JMenuItem clearItem = new JMenuItem("Clear");
JMenuItem chooseFileItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(newItem);
menu.add(clearItem);
menu.add(chooseFileItem);
menu.add(saveItem);
menu.add(exitItem);
return menu;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.drawRect(200, 200, 50, 50);
}
public JMenuBar getMenuBar() {
return menuBar;
}
}

Your problem is here:
frame.setLayout(new FlowLayout());
By doing this, your lePanel JPanel sizes to its preferred size, a size much too small to show the rectangle. Delete this line and your JFrame's contentPane will use its default BorderLayout, and the drawing JPanel will fill the lower part of your GUI, as per the BordrLayout rules, and you'll see the drawing.
Note a useful debugging technique is to add a border around a component of interest to see where it is and how big it is. For example, I placed this in your lepanel constructor
setBorder(BorderFactory.createTitledBorder("le panel"));
and it visually showed me your problem.
Other unrelated issues:
You will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
Remember to use the #Override annotation above any methods that you think might be overriding another, such as your paintComponent method. This will allow the compiler to notify you if you are in fact overriding things incorrectly.

Related

Using button in a panel class to call ActionListener in the frame class

I started codig Java last weekend and I've read most of the basic stuff. I'm trying to separate my frame from main method, and panels from the frame so they are all in separate class files. I have trouble calling ActionLister in "Frame1" class with a button (buttonBack) in the "TheGame" class. The button should trigger the Listener which in turn should remove theGame panel and add mainMenu panel to frame1. I know that CardLayout is better suited for swapping panels but i want to learn the limits and workarounds before i go do it the "easy" way, i feel that you learn much more that way.
Here is some of my code:
Main:
public class Main {
public static void main(String[] args){
Frame1 frame1 = new Frame1();
frame1.frame1();
}
}
Frame1:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
class Frame1 {
private JFrame frame1 = new JFrame("Name");
public void frame1() {
TheGame theGame = new TheGame();
MainMenu mainMenu = new MainMenu();
// Frame options
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLocationRelativeTo(null);
// Creating a top menu
JMenuBar menubar = new JMenuBar();
frame1.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenuItem about = new JMenuItem("About");
help.add(about);
// Creating action for the menuitem "exit".
class exitaction implements ActionListener{
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction());
// Creating listener for the menuitem "about".
class aboutaction implements ActionListener{
public void actionPerformed (ActionEvent e){
JDialog dialogabout = new JDialog();
JOptionPane.showMessageDialog(dialogabout, "Made by: ");
}
}
about.addActionListener(new aboutaction());
// Add the panels, pack and setVisible
theGame.theGame();
mainMenu.mainMenu();
frame1.add(theGame.getGUI());
// This is the ActionListener i have trouble connecting with the buttonBack in the "theGame" class
class Action implements ActionListener{
public void actionPerformed (ActionEvent e){
frame1.remove(theGame.getGUI());
frame1.add(MainMenu.getGUI());
}
}
frame1.pack();
frame1.setVisible(true);
}
public JFrame getGUI() {
return frame1;
}
}
MainMenu:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
class MainMenu {
private JPanel mainMenu = new JPanel (new GridBagLayout());
public void mainMenu() {
// Using the GridBagLayout therefore creating the constraints "grid"
GridBagConstraints grid = new GridBagConstraints();
// Adjusting grid insets
grid.insets = new Insets(10, 10, 10, 10);
// Creating Label
JLabel introduction = new JLabel("Name");
grid.gridx = 1;
grid.gridy = 3;
mainMenu.add(introduction, grid);
// Creating buttons Start Game, Highscore and Exit Game
JButton buttonNewGame = new JButton("New Game");
buttonNewGame.setPreferredSize(new Dimension(200, 50));
grid.gridx = 1;
grid.gridy = 5;
mainMenu.add(buttonNewGame, grid);
JButton buttonHighscore = new JButton("Highscore");
buttonHighscore.setPreferredSize(new Dimension(200, 50));
grid.gridx = 1;
grid.gridy = 6;
mainMenu.add(buttonHighscore, grid);
JButton buttonExit = new JButton("Exit Game");
buttonExit.setPreferredSize(new Dimension(200, 50));
grid.gridx = 1;
grid.gridy = 7;
mainMenu.add(buttonExit, grid);
}
public JComponent getGUI() {
return mainMenu;
}
}
TheGame:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
class TheGame {
private JPanel theGame = new JPanel (new GridBagLayout());
public void theGame() {
// Using the GridBagLayout therefore creating the constraints "grid"
GridBagConstraints grid = new GridBagConstraints();
// Adjusting grid insets
grid.insets = new Insets(10, 10, 10, 10);
// Creating a label
JLabel label1 = new JLabel("Press the BACK button to go back to Main Menu");
label1.setVisible(true);
grid.gridx = 1;
grid.gridy = 0;
theGame.add(label1,grid);
// Creating BACK button
JButton buttonBack = new JButton("BACK");
buttonBack.setVisible(true);
grid.gridx = 1;
grid.gridy = 1;
buttonBack.addActionListener(new --); // This is the button i want to connect with the ActionListener on Frame1 class
theGame.add(buttonBack, grid);
}
public JComponent getGUI() {
return theGame;
}
}
I've tried moving the ActionListener outside of methods, inside the Main, declaring it static, but haven't been able to call it anyways. I've also looked at other posts like this: Add an actionListener to a JButton from another class but have not been able to implement it in to my code.
Any help is appreciated.
The best answer -- use MVC (model-view-controller) structure (and CardLayout) for the swapping of your views. If you don't want to do that, then your listener should have a reference to the container that does the swapping, and so that the listener can notify this container that a swap should occur. The container will then call its own code to do the swapping. To do this you need to pass references around including a reference to the main GUI to wherever it is needed. This can get messy, which is why MVC, which is more work, is usually better -- fewer connections/complexity in the long term.
Side note -- don't pass a JDialog into a JOptionPane as a JOptionPane is a specialized JDialog, and you shouldn't have a top level window displaying a top level window. Instead pass in a JPanel.
For example:
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class PassRef {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
private static void createAndShowGui() {
MyMain mainPanel = new MyMain();
JFrame frame = new JFrame("Pass Reference");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
class MyMain extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 450;
private CardLayout cardLayout = new CardLayout();
private MenuView menuView = new MenuView(this);
private ActionView1 actionView1 = new ActionView1(this);
public MyMain() {
setLayout(cardLayout);
add(menuView, MenuView.NAME);
add(actionView1, ActionView1.NAME);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
} else {
return new Dimension(PREF_W, PREF_H);
}
}
public void showCard(String key) {
cardLayout.show(this, key);
// or swap by hand if you don't want to use CardLayout
// but remember to revalidate and repaint whenever doing it by hand
}
}
class MenuView extends JPanel {
public static final String NAME = "Menu View";
public MenuView(MyMain myMain) {
setName(NAME);
setBorder(BorderFactory.createTitledBorder("Menu"));
add(new JButton(new GoToAction("Action 1", ActionView1.NAME, myMain)));
}
}
class ActionView1 extends JPanel {
public static final String NAME = "Action View 1";
public ActionView1(MyMain myMain) {
setName(NAME);
setBorder(BorderFactory.createTitledBorder(NAME));
add(new JButton(new GoToAction("Main Menu", MenuView.NAME, myMain)));
}
}
class GoToAction extends AbstractAction {
private String key;
private MyMain myMain;
public GoToAction(String name, String key, MyMain myMain) {
super(name);
this.key = key;
this.myMain = myMain;
}
#Override
public void actionPerformed(ActionEvent e) {
myMain.showCard(key);
}
}

JOptionPane not showing up (null parameter, called from inner class)

I am completely new to JAVA, I am trying to make a simple application and there is no way I can get my JOptionPane to show correctly and I guess I am missing something:
here's the code:
import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Frame extends JFrame
{
private static final long serialVersionUID = 1L;
final static int WIDTH = 400;
final static int HEIGHT = 400;
public Frame()
{
super("Convert to Dxf alpha ver. - survey apps 2014");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setResizable(false);
setLocation(100, 100);
setBackground(Color.WHITE);
setVisible(true);
WelcomePanel welcomePanel = new WelcomePanel(this);
add(welcomePanel);
}
public void createMenuPanel()
{
MenuPanel menu = new MenuPanel();
setJMenuBar(menu.createMenu(this));
}
}
class MenuPanel extends JPanel implements ActionListener
{
private JMenuItem open,save,close;
private JMenu file,about;
private Frame frame;
private static final long serialVersionUID = 1L;
public JMenuBar createMenu(Frame frame)
{
this.frame = frame;
JMenuBar menuBar = new JMenuBar();
file = new JMenu("File");
about = new JMenu("About");
menuBar.add(file);
menuBar.add(about);
open = new JMenuItem("Open");
save = new JMenuItem("Save");
close = new JMenuItem("Close");
file.add(open);
file.add(save);
file.addSeparator();
file.add(close);
open.addActionListener(this);
save.addActionListener(this);
close.addActionListener(this);
about.addActionListener(this);
return menuBar;
}
public MenuPanel()
{
setVisible(true);
setOpaque(true);
setBackground(Color.WHITE);
setSize(Window.WIDTH,Window.HEIGHT);
}
#Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == open)
{
frame.dispose();
}
if(source == save)
{
}
if(source == close)
{
}
if(source == about)
{
JOptionPane.showMessageDialog(frame, "EasyDxfCreator - alpha version");
}
}
}
(I skipped the WelcomeFrame because it's just like a welcome screen that disappears after a mouse click)
JMenu does not operate in that way. TO get JMenu Events you need to implement MenuListener instead of ActionListener. ActionListener is good for JMenuItem.
Hope this helps.

JMenu Item Action Listener is not being detected

I am making a library database for a school project and am having a little trouble with my menu. So the main problem is that in the Action Listener method when I write
(e.getSource()==m1Frame1)
my program does not detect the menu item and gives me an error. I have looked at multiple tutorials etc. online but cannot seem to find any way to fix it and make it so that if a specific item is clicked a specific action occurs. Any help/resolution regarding this issue would be much appreciated.
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.Icon;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Color;
public class m1 extends JFrame {
JPanel pane = new JPanel();
JFrame a = new JFrame("Main Frame");
JFrame b = new JFrame("Sub Frame");
JButton checkOutButton = new JButton("check");
JButton returnButton = new JButton("return");
JMenu mb2 = new JMenu("Books");
// mb2.setForeground(Color.white);
JMenu open = new JMenu("Students");
// open.setForeground(Color.white);
public m1() {
JMenuBar mb;
mb = new JMenuBar() {
public void paintComponent(Graphics g) {
g.drawImage(Toolkit.getDefaultToolkit().getImage("G:"), 0, 0, this);
}
};
setSize(400, 400);
setBackground(Color.BLACK);
setTitle("Screen 2");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mb.add(open);
JMenuItem m1Frame1 = new JMenuItem("Create");
JMenuItem m1Frame2 = new JMenuItem("Delete");
JMenu m1Frame3 = new JMenu("Look-Up");
JMenuItem m1Frame4 = new JMenuItem("Check Fine");
JMenuItem m1Frame5 = new JMenuItem("Check Borrowed Books");
JMenuItem subM1 = new JMenuItem("Name");
JMenuItem subM2 = new JMenuItem("Student #");
open.add(m1Frame1);
open.add(m1Frame2);
open.add(m1Frame3);
open.add(m1Frame4);
open.add(m1Frame5);
m1Frame3.add(subM1);
m1Frame3.add(subM2);
mb.add(mb2);
JMenuItem m2Frame1 = new JMenuItem("Create");
JMenuItem m2Frame2 = new JMenuItem("Delete");
JMenu m2Frame3 = new JMenu("Look-Up");
JMenuItem subB1 = new JMenuItem("Title");
JMenuItem subB2 = new JMenuItem("Author");
JMenuItem subB3 = new JMenuItem("Category");
JMenuItem subB4 = new JMenuItem("ISBN");
JMenuItem m2Frame4 = new JMenuItem("Compare Star Rating");
JMenuItem m2Frame5 = new JMenuItem("Check If Checked Out");
JMenuItem m2Frame6 = new JMenuItem("Lost Book");
mb2.add(m2Frame1);
mb2.add(m2Frame2);
mb2.add(m2Frame3);
mb2.add(m2Frame4);
mb2.add(m2Frame5);
mb2.add(m2Frame6);
m2Frame3.add(subB1);
m2Frame3.add(subB2);
m2Frame3.add(subB3);
m2Frame3.add(subB4);
a.setJMenuBar(mb);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(1280, 720);
a.setVisible(true);
b.setSize(600, 400);
m handler = new m();
pane.add(checkOutButton);
pane.add(returnButton);
add(pane);
checkOutButton.setVisible(false);
returnButton.setVisible(false);
checkOutButton.setBounds(60, 440, 220, 30);
returnButton.setBounds(60, 404, 100, 50);
checkOutButton.addActionListener(handler);
returnButton.addActionListener(handler);
}
public class m implements ActionListener, ItemListener {
public void actionPerformed(ActionEvent e) {
(e.getSource() == m1Frame1) {
a.setVisible(false);
setVisible(true);
checkOutButton.setVisible(true);
returnButton.setVisible(true);
}
}
public void itemStateChanged(ItemEvent e) {
}
}
public static void main(String[] args) {
m1 aa = new m1();
}
}
Ok, there are a few issues with your code, but I'll go over the two specifics that answer your question:
1) You're not adding your action listener to any of your MenuItems in your code. When I added your handler to the MenuItems using addActionListener(handler); It started triggering.
2) You're adding handler as the actionListener to two buttons that are invisible (and you've got other layout issues)

Java UIManager - Change ComponentsStyle

I want to change my componentstyle by using UIManager.
For example:
I click on a Button and the Button foreground changes from black to green. The same for a JCheckbox.....
In my example the changes just work for the Button.gradient.... I get no update for Button.foreground and no update for the JCheckbox!
Here my UIManagerClass:
package components;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
public class OwnUiManager {
ButtonDemo Bd;
OwnUiManager(ButtonDemo aThis) {
Bd = aThis;
}
public void setNormal() {
ArrayList<Object> gradients = new ArrayList();
gradients.add(0.3);
gradients.add(0.0);
gradients.add(new Color(221, 232, 243));
gradients.add(new Color(255, 255, 255));
gradients.add(new Color(184, 207, 229));
javax.swing.UIManager.put("RadioButton.background", Color.PINK);
javax.swing.UIManager.put("Button.gradient", gradients);
javax.swing.UIManager.put("Button.foreground", Color.PINK);
SwingUtilities.updateComponentTreeUI(Bd);
}
public void setNightVision() {
System.out.println("Tes");
ArrayList<Object> gradients = new ArrayList();
gradients.add(0.18f);
gradients.add(0.17f);
gradients.add(Color.BLACK);
gradients.add(Color.DARK_GRAY);
gradients.add(Color.DARK_GRAY);
javax.swing.UIManager.put("RadioButton.background", Color.GRAY);
javax.swing.UIManager.put("Button.gradient", gradients);
javax.swing.UIManager.put("Button.foreground", Color.red);
SwingUtilities.updateComponentTreeUI(Bd);
}
}
and here my Buttondemo/Main-Class:
package components;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class ButtonDemo extends JPanel
implements ActionListener {
protected JButton b1,b2;
private JRadioButton b3;
public ButtonDemo() {
b1 = new JButton("ON");
b1.addActionListener(this);
add(b1);
b2 = new JButton("OFF");
b2.addActionListener(this);
add(b2);
//For Testing the Style
b3=new JRadioButton("Test");
add(b3);
}
public void actionPerformed(ActionEvent e) {
OwnUiManager test = new OwnUiManager(this);
if (e.getSource().equals(b1)) {
test.setNormal();
} else {
test.setNightVision();
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonDemo newContentPane = new ButtonDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
JCheckBox / JRadioButton has Icon
have to change own Icon to concrete JCheckBox / JRadioButton or put then to the UIManager, then apply for whole JVM instance
for more infos have to check UIManager Defaults and Key Bindings by camickr
most of your potential issues is descibed in Creating a custom button in Java with JButton

How to configure JComboBox not to select FIRST element when created?

Problem:
Update:
From the Java SE 6 API:
public JComboBox() Creates a JComboBox
with a default data model. The default
data model is an empty list of
objects. Use addItem to add items. By
default the first item in the data
model becomes selected.
So I changed to JComboBox(model) as the API says:
public JComboBox(ComboBoxModel aModel)
Creates a JComboBox that takes its
items from an existing ComboBoxModel.
Since the ComboBoxModel is provided, a
combo box created using this
constructor does not create a default
combo box model and may impact how the
insert, remove and add methods behave.
I tried the following:
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.setSelectedItem(null);
suggestionComboBox = new JComboBox(model);
suggestionComboBox.setModel(model);
But could not get it to work, the first item is still being selected.
Anyone that can come up with a working example would be very much appreciated.
Old part of the post:
I am using JComboBox, and tried using setSelectionIndex(-1) in my code (this code is placed in caretInvoke())
suggestionComboBox.removeAllItems();
for (int i = 0; i < suggestions.length; i++) {
suggestionComboBox.addItem(suggestions[i]);
}
suggestionComboBox.setSelectedIndex(-1);
suggestionComboBox.setEnabled(true);
This is the initial setting when it was added to a pane:
suggestionComboBox = new JComboBox();
suggestionComboBox.setEditable(false);
suggestionComboBox.setPreferredSize(new Dimension(25, 25));
suggestionComboBox.addActionListener(new SuggestionComboBoxListener());
When the caretInvoke triggers the ComboBox initialisation, even before the user selects an element, the actionPerformed is already triggered (I tried a JOptionPane here):
http://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/combo1.png
http://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/combo2.png
http://i126.photobucket.com/albums/p109/eXPeri3nc3/StackOverflow/combo3.png
The problem is: My program autoinserts the selected text when the user selects an element from the ComboBox. So without the user selecting anything, it is automatically inserted already.
How can I overcome the problem in this situation? Thanks.
Here is my SSCCE: (finally)
package components;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
public class Temp extends JFrame {
JTextPane textPane;
AbstractDocument doc;
JTextArea changeLog;
String newline = "\n";
private JComboBox suggestionComboBox;
private JPanel suggestionPanel;
private JLabel suggestionLabel;
private JButton openButton, saveButton, aboutButton;
public Temp() {
super("Snort Ruleset IDE");
//Create the text pane and configure it.
textPane = new JTextPane();
textPane.setCaretPosition(0);
textPane.setMargin(new Insets(5, 5, 5, 5));
StyledDocument styledDoc = textPane.getStyledDocument();
if (styledDoc instanceof AbstractDocument) {
doc = (AbstractDocument) styledDoc;
//doc.setDocumentFilter(new DocumentSizeFilter(MAX_CHARACTERS));
} else {
System.err.println("Text pane's document isn't an AbstractDocument!");
System.exit(-1);
}
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(700, 350));
//Create the text area for the status log and configure it.
//changeLog = new JTextArea(10, 30);
//changeLog.setEditable(false);
//JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a JPanel for the suggestion area
suggestionPanel = new JPanel(new BorderLayout());
suggestionPanel.setVisible(true);
suggestionLabel = new JLabel("Suggestion is not active at the moment.");
suggestionLabel.setPreferredSize(new Dimension(100, 50));
suggestionLabel.setMaximumSize(new Dimension(100, 50));
suggestionComboBox = new JComboBox();
suggestionComboBox.setEditable(false);
suggestionComboBox.setPreferredSize(new Dimension(25, 25));
//suggestionComboBox.addActionListener(new SuggestionComboBoxListener());
suggestionComboBox.addItemListener(new SuggestionComboBoxListener());
//suggestionComboBox.setSelectedIndex(-1);
//add the suggestionLabel and suggestionComboBox to pane
suggestionPanel.add(suggestionLabel, BorderLayout.CENTER);
suggestionPanel.add(suggestionComboBox, BorderLayout.PAGE_END);
JScrollPane sp = new JScrollPane(suggestionPanel);
JScrollPane scrollPaneForSuggestion = new JScrollPane(suggestionPanel);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForSuggestion);
splitPane.setOneTouchExpandable(true);
splitPane.setResizeWeight(1.0);
//Disables the moving of divider
splitPane.setEnabled(false);
//splitPane.setDividerLocation(splitPane.getHeight());
//splitPane.setPreferredSize(new Dimension(640,400));
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Status: Ready");
statusPane.add(caretListenerLabel);
//Create the toolbar
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.setRollover(true);
openButton = new JButton("Open Snort Ruleset");
toolBar.add(openButton);
saveButton = new JButton("Save Ruleset");
toolBar.add(saveButton);
toolBar.addSeparator();
aboutButton = new JButton("About");
toolBar.add(aboutButton);
//Add the components.
getContentPane().add(toolBar, BorderLayout.PAGE_START);
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(statusPane, BorderLayout.PAGE_END);
JMenu editMenu = createEditMenu();
JMenu styleMenu = createStyleMenu();
JMenuBar mb = new JMenuBar();
mb.add(editMenu);
mb.add(styleMenu);
setJMenuBar(mb);
//Put the initial text into the text pane.
//initDocument();
textPane.setCaretPosition(0);
//Start watching for undoable edits and caret changes.
textPane.addCaretListener(caretListenerLabel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textPane.requestFocusInWindow();
}
});
}
//This listens for and reports caret movements.
protected class CaretListenerLabel extends JLabel
implements CaretListener {
public CaretListenerLabel(String label) {
super(label);
}
//Might not be invoked from the event dispatch thread.
public void caretUpdate(CaretEvent e) {
caretInvoke(e.getDot(), e.getMark());
}
protected void caretInvoke(final int dot, final int mark) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Rectangle caretCoords = textPane.modelToView(dot);
//Find suggestion
suggestionComboBox.removeAllItems();
for (int i = 0; i < 5; i++) {
suggestionComboBox.addItem(Integer.toString(i));
}
//suggestionComboBox.setSelectedItem(null);
suggestionComboBox.setEnabled(true);
suggestionLabel.setText("The following keywords are normally used as well. Click to use keyword(s). ");
//changeLog.setText("The following keywords are suggested to be used together: " + str);
} catch (BadLocationException ble) {
setText("caret: text position: " + dot + newline);
System.out.println("Bad Location Exception");
}
}
});
}
}
public class SuggestionComboBoxListener implements ItemListener {
//public void actionPerformed(ActionEvent e) {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox cb = (JComboBox)e.getSource();
String selection = (String) cb.getSelectedItem();
JOptionPane.showMessageDialog(null, "Item is selected", "Information", JOptionPane.INFORMATION_MESSAGE);
}
}
}
/*
* Menu Creation
*/
//Create the edit menu.
protected JMenu createEditMenu() {
JMenu menu = new JMenu("Edit");
return menu;
}
protected JMenu createStyleMenu() {
JMenu menu = new JMenu("Style");
return menu;
}
private static void createAndShowGUI() {
//Create and set up the window.
final Temp frame = new Temp();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//The standard main method.
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
You need to remove the ItemListener before you make any changes to the combo-box and add it back when you are done.
Something like this:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Suggestions {
private JFrame frame;
private JTextPane textPane;
private JComboBox suggestionComboBox;
private SuggestionComboBoxListener selectionListener;
public Suggestions() {
frame = new JFrame("Snort Ruleset IDE");
textPane = new JTextPane();
textPane.setCaretPosition(0);
textPane.setMargin(new Insets(5, 5, 5, 5));
textPane.addCaretListener(new SuggestionCaretListener());
JScrollPane textEntryScrollPane = new JScrollPane(textPane);
textEntryScrollPane.setPreferredSize(new Dimension(300, 400));
selectionListener = new SuggestionComboBoxListener(frame);
suggestionComboBox = new JComboBox();
suggestionComboBox.setEditable(false);
suggestionComboBox.setPreferredSize(new Dimension(25, 25));
suggestionComboBox.addItemListener(selectionListener);
JPanel suggestionPanel = new JPanel(new BorderLayout());
suggestionPanel.add(suggestionComboBox, BorderLayout.PAGE_END);
frame.getContentPane().add(textEntryScrollPane, BorderLayout.NORTH);
frame.getContentPane().add(suggestionPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private final class SuggestionCaretListener implements CaretListener {
#Override
public void caretUpdate(CaretEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
generateSuggestions();
}
});
}
}
public static final class SuggestionComboBoxListener implements ItemListener {
Component parent;
public SuggestionComboBoxListener(Component parent) {
this.parent = parent;
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox cb = (JComboBox) e.getSource();
String selection = (String) cb.getSelectedItem();
JOptionPane.showMessageDialog(parent, "The selected item is: " + selection, "Information",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
void generateSuggestions() {
suggestionComboBox.removeItemListener(selectionListener);
suggestionComboBox.removeAllItems();
for (int i = 0; i < 5; i++) {
suggestionComboBox.addItem(Integer.toString(i));
}
suggestionComboBox.setEnabled(true);
suggestionComboBox.addItemListener(selectionListener);
}
public static void main(String[] args) {
new Suggestions();
}
}
BTW, what you posted is not an SSCCE it is a dump of your code. An SSCCE should only have enough code to reproduce the issue you are experiencing.
use
setSelectedItem(null);
Please try with ItemListener instead of ActionListener.
if You want that after 1st entry you made and immediately you combox is empty then just write down the under mentioned code which is:
jComboBox1.setSelectedIndex(0);
and your combox will reset Automatically

Categories

Resources