JButton problem

hi, i am new to java and need a bit of help. I have 2 frames and when i click a button on one of them the other should get displayed. So far everything works.to do that i am using the following code
public void actionPerformed(ActionEvent e)
new BorderLayoutExample(this);
and then northButton.addActionListener(this); in a method which also draws my components. My class is also implementing ActionListener. The problem is when i create another jButton. I am unable to assing it to any action to perform. I tried something like button.addActionListener(this); and then wrote another actionPerformed method but the button does the same thing as the other button(due to "this" keyword). Can anyone tell me how to differentiate the 2 actionPerformed methods so the jButtons know which one to use??
Thanx

public class Homepage implements ActionListener
public Homepage()
assembleWindow();
private void assembleWindow()
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Make components for GUI
frame.setLayout(new BorderLayout());
JButton northButton = new JButton("North");
northButton.addActionListener(this);
JButton s = new JButton("S");
// Add the buttons to the Frame
frame.add(northButton, BorderLayout.NORTH);
frame.add(s, BorderLayout.SOUTH);
frame.setSize(350,350);
frame.setVisible(true);
public void actionPerformed(ActionEvent e)
new BorderLayoutExample(this);
this is the code i am using for the first frame(where i want to have 2-3 buttons).So far i got one method (public void actionPerformed...)which displays my second frame(named BorderLayoutExample) and the first jButton uses(northButton.addActionListener(this);) that method. The other jButton for example should lets say close the frame. I have an idea of how to write the method but dont know how to make it work when the second jButton is clicked.I should probably have something like s.addActionListener(//something goes here); and that is where i am struggling..

Similar Messages

  • JPasswordField and JButton Problem

    Happy New Year to all of you guys and gals. Here's the problem
    I have a String Array of buttons made already .. The design and measuring of the buttons are similar to a regular calculator.
    Now depending on the button that I press I set the text of the JPasswordField to "whatever", which is wrong because it's not being appended. So there has to be another way to do this.
    Take a calculator or bank for example. You press one button, it shows, and press the same button, so you have two digits appear on screen. That's exactly what I'm looking for. But I'm stuck...
    Here's the code to create the buttons
    for (int i = 0; i < 13; i++)
    button[i] = new JButton(Buttons);
    button[i].addActionListener(this);
    button[i].setFont(new Font("Comic Sans MS", Font.BOLD, 12));
    button[i].setSize(50, 30);
    button[i].setLocation(x, y);
    button[i].setBackground(Color.darkGray);
    button[i].setForeground(Color.white);
    button[i].setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.black, Color.lightGray));
    theKeysP.add(button[i]);
    x = x + 50;
    if((i + 1) % 4 == 0)
    x = 210;
    y = y + 30;
    if(i == 12)
    button[i].setLocation(410, 65);
    button[i].setBackground(Color.green);
    button[i].setForeground(Color.black);
    Lets say starting from button 0 to 5 are numbers starting from 1 - 6
    Since I'm clicking on the button it's an ActionEvent,
    so If(e.getSource() == button[0])
    PF.setText("1"); //Wrong because it's not being appended. Therefore it resets everytime button[0] is clicked.
    I hope this I made clear enough. Can anyone provide suggestions for this problem.

    Try doing it this way instead
    password.setText( password.getText() + "1" );Next time please use the code tags when posting
    ICE

  • Solution to setRollOverIcon (for JButton) problem

    Hi all,
    I've seen a lot of posts asking why setRolloverIcon() didn't work. I had the same problem, and after looking throught the SwingSet example, I found the solution.
    So far, the only way that I've been able to get the rollover icon to work is through the following code:
    JToolBar toolBar = new JToolBar();
    JButton button = new JButton(defaultImage);
    toolBar.add(button);
    button.setRolloverIcon(rolloverImage);
    ...I don't know why, but if you set the rollover icon before you add the button to the toolbar (or whichever component you add the button to), the rollover icon doesn't work.
    Hope this helps.

    This is the class I generally use for toolbars.
    * Toolbar.java
    * Created on 29 September 2002, 21:01
    package org.argouml.ui;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.Insets;
    * @author  Bob Tarling
    public class Toolbar extends JToolBar {
        /** Creates a new instance of Toolbar */
        public Toolbar(String title) {
            this.setFloatable(true);
            this.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
            this.setMargin(new Insets(0,0,0,0));
        /** Creates a new instance of Toolbar */
        public Toolbar() {
            super();
            this.setFloatable(false);
            this.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
            this.setMargin(new Insets(0,0,0,0));
        public Toolbar(int orientation) {
            super(orientation);
            this.setFloatable(false);
            this.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
            this.setMargin(new Insets(0,0,0,0));
    }

  • JButton problem in MVC Using DefaultTableModel

    View:
    I have a JDialog that contains a JTable.
    Model:
    I extend DefaultTableModel
    Problem:
    I want a button at the bottom of the JDialog, that is not in the JTable.
    The button is supposed to change a value in the model, when clicked
    I want the button's text to change when the value in the model changes, so it displays the new value.
    But it seems to me the real view is the JTable, not the enclosing JDialog, so if I try to create the JButton in the Model, I can not access it outside the JTable So how do work around this?
    Any advice??

    Create a panel containing the JTable, say BorderLayout.Center and JButton at say, BorderLayout.south. Add the panel to the JDialog. The button can have actionListener with actionPeformed method to change the value in the table. You should be able to access the button outside of the Table that way.

  • JMenuBar and JButton problem

    The problem is a simple one.
    Simply I don't know how to have the two together, every time I try to create a contentPane with the buttons in it I always get errors I would love it if some one could show me how to do this.
    Thanx

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class Test extends JFrame {
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        JMenuBar jmb = new JMenuBar();
        setJMenuBar(jmb);
        JMenu jm = new JMenu("Menu");
        jmb.add(jm);
        for (int i=0; i<5; i++) {
          JMenuItem jmi = new JMenuItem("MenuItem-"+i);
          jm.add(jmi);
          jmi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              System.out.println(((JMenuItem)ae.getSource()).getText());
          JButton jb = new JButton("Button-"+i);
          content.add(jb);
          jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              System.out.println(((JButton)ae.getSource()).getText());
        setSize(200,400);
        show();
      public static void main(String[] args) { new Test(); }
    }

  • Jcombobox,JButton problem

    Hi,
    i would like my button to show a new java class,
    My button actionlistener
    select2 select = new select2();
    select.createAndShowGUI();but, i want to choose which class i will go through JComboBox.....
    Let's say my string on the JComboBox is frame1,frame2,frame3....
    how should i put it in way that....
    if frame1 is selected....
    i click the button, it will show frame1 class...
    if frame2 is selcted....
    after clicked the button, it will show frame2 class...
    Edited by: vanharu on May 27, 2008 8:38 PM

    i understand the codes u put there...
    but how do i implement it to my button action
    * @(#)select2.java
    * @author
    * @version 1.00 2008/5/28
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import javax.swing.BoxLayout;
    public class select2 extends JFrame  implements ActionListener
         public JComboBox CharList;
         public JLabel Char,title;
         public JButton Play, Preview;
        public select2()
            setTitle("Select Your Character");
            setSize(340, 400);
            getContentPane().setLayout(
                    new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
             Border raisedbevel, loweredbevel, compound;
             raisedbevel = BorderFactory.createRaisedBevelBorder();
            loweredbevel = BorderFactory.createLoweredBevelBorder();
              // Puts in array of strings to the combo box
            // Can select the arrays that is inserted in the combo box
                String hero[] = {"Naruto", "Sasuke", "Ichigo", "Ulqiourra"};
                CharList = new JComboBox(hero);
             //Shows that the combo box will start at 0,
             //which is naruto  
                CharList.setSelectedIndex(0);
                CharList.addActionListener(this);
                 //Set up the animation part
                    Char = new JLabel();
                  Char.setHorizontalAlignment(JLabel.CENTER);
                  updateLabel(hero[CharList.getSelectedIndex()]);
                  compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
                  Char.setBorder(compound);
                  Char.setPreferredSize(new Dimension(320, 266));
                  //Set up button part
                  Play = new JButton("Select This Character");
                 Play.setHorizontalAlignment(4);
                 Play.setPreferredSize(new Dimension(100,40));
                 Play.addActionListener(new confirm());
                      Preview = new JButton("Preview This Character");
                      Preview.setHorizontalAlignment(4);
                      Preview.setPreferredSize(new Dimension(80, 40));
                      Preview.addActionListener(new preview());     
                      getContentPane().add(CharList);
                 CharList.setAlignmentX(Component.CENTER_ALIGNMENT);
                 getContentPane().add(Char);
                 Char.setAlignmentX(Component.CENTER_ALIGNMENT);
                 getContentPane().add(Play);
                 Play.setAlignmentX(Component.CENTER_ALIGNMENT);
                 getContentPane().add(Preview);
                  Preview.setAlignmentX(Component.CENTER_ALIGNMENT);
        public void actionPerformed(ActionEvent e)
                 JComboBox nm = (JComboBox)e.getSource();
                 String CharName = (String)nm.getSelectedItem();
                 updateLabel(CharName);
        class confirm implements ActionListener {
            public void actionPerformed(ActionEvent event)
               System.exit(0);
        class preview implements ActionListener {
            public void actionPerformed(ActionEvent event)
            //lets say, if the combobox selection is naruto...
            //then when i click this button
            //it will show naruto class
             should i put like something like
             combobox = naruto
             show naruto.class
         protected void updateLabel(String name) {
            ImageIcon icon = new ImageIcon("Resources/"+name+"Pose" + ".gif");
            Char.setIcon(icon);
            Char.setToolTipText(name);
            if (icon != null) {
                Char.setText(null);
            } else {
                Char.setText("UNDER CONSTRUCTION");
       public static void createAndShowGUI() {
            JFrame.setDefaultLookAndFeelDecorated(true);
            //Create and set up the window.
            JFrame frame = new JFrame("Choose Your Character");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
              //Display the window.
            select2 sel = new select2();
            sel.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            sel.setVisible(true);
         public static void main(String[] args) {
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    }Edited by: vanharu on May 27, 2008 10:27 PM

  • JButton Problem plz help me

    Sir
    i want to add accelrator method to the JButton can it is possibe
    means
    JButton first=new JButton(new ImageIcon("file.jpg"));
    first.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,ActionEvent.CTRL_MASK));
    How can i do?
    thanks in advance

    JButton doesn't have a setAccelerator() method (this is a JMenuItem method), so no you can't add an accelerator.
    However, you should be able to add a KeyBinding to do what you want:
    http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html

  • Urgent...JButton problem...Please Help

    Hi,
    I have been developing a swing application to run on all of the platforms. I have been coding mostly in Windows. I ran the program several times on the windows machine and its ok there, however, when I try to run the same program on SUN Solaris the some of the buttons have shrunk in height. I have tryied to use absolute positioning and layouts to see if this would make a difference, but still the buttons shrink. I am also using the same JDK and JVM on both operating system. Does anyone know why this is and how to go about fixing it. Also at the beginning of the program I am using UIManager.setLookAndFeel() method to set it to system look and feel. I would realy appreciate your help.
    Thank you,
    Jay

    Solaris is a X-Windows environment. MS-Windows is not an X-Windows environment.
    IT IS IMPOSSIBLE to get the same results with two (really) different Look and Feels.
    JLF is intended to produce the same results independant of peer Wndows environment.
    Therefore use the JLF (Java Look and Feel) and your problem should be solver.
    Alternatively use the Motif LAF on the Windows BOX.

  • About JtextField and JButton problem

    hi, I dont know how to make this, hope to get help:)
    I wanna like this:
    A JtextField object -- for user to input some Integer
    A JButton -- when users click the button, the content in JtextFiled will be delivered to a variable (say, int a, sent to this "a")in my program.
    Here, We dont need to check the content if it is Integer or not for simple.
    Any idea for me? thanks!

    Write a listener class that implements listener interface. in that class implement the actionPerformed method of the super interface as u need.( I.e assign the value to the variable). Then bind this class with ur button using addActionListener() method( look in API).
    ex:
    bottonName.addActionListener( new ActionListener()
              public void actionPerformed(ActionEvent e)
                   var = textarea.val;
         });

  • Simple JButton problem

    dear
    i place a button in a toolbar.
    as another application e.g Explorer when enterd a mouse the button some effect means
    enterd mouse it shows as button otherwise it shows only icon.
    iuse
    setContentAreafilled(true);
    setRolloverEnabled(true);
    BorderPainted already true and also focus but i can not get this effect how can i do

    toolbar.setRollover(...);

  • JButton is Focused but not clicking ...Why??

    Hi evey body:
    Can you please help me with the JButton problem ...??
    I give it the focus using requestFocus() but when I press Enter from the keyboard it doesn't click???
    JButton Btn = new JButton("Ok");
    Btn.requestFocus();
    Btn.addActionListener(new ActionListener()
    { public void actionPerformed(ActionEvent evt)
    JOptionPane.showMessageDialog(null,"Error in user Name and Password ..try again");
    Thanks in advance

    YOu have to check the Key, try something like that
    Btn.addActionListener(new ActionListener()
    { public void actionPerformed(ActionEvent evt)
    if ( (e.getKeyCode() == KeyEvent.VK_ENTER)) {
    // DO what ever you want to do

  • Images not showing in the jlabel/jbutton

    Hello all,
    I've a package in which my cards directory is located along with all my src files and compiled classes. All my images ****.gif files are inside this directory.
    Then I've following the icon object.
    protected static JLabel lblDeck;The following code is used to get the image.
      String imgPath;
         imgPath = isImageExists("imgBG1.gif");
              if (imgPath == "")
              {     // If the image of Card Deck(imgDeck) is not Found
                   lblDeck = new JLabel();
                   lblDeck.setBackground(new Color(0, 64, 128));
                   lblDeck.setOpaque(true);
                   flagImgDeckFound = false;
              } else {
                   // If the image of Card Deck is Found
                   imgDeck     = new ImageIcon(imgPath);
                   lblDeck = new JLabel(imgDeck);
    // Check if the image exists else return "";
      protected String isImageExists(String imgName) {
              java.net.URL imgURL = getClass().getResource("cards/" + imgName);
             if (imgURL != null)
             {     return (imgURL.toString());     }
             else
                  JOptionPane.showMessageDialog(null, "File not Found: " + imgURL);
                  return "";
         }I'm still unable to get the image in the jlabel!
    When i checked the path, it is exactly returning the path of the file.
    But its neither loading the image into the jlabel nor is it returning "".
    This is just the label part i've other jbuttons also. Even they are not displaying any images.

    aLkeshP wrote:
    can everyone see this thread?
    YES FER-CHRISE-SAKE.
    Just how many times do you intend posting the same identical question?
    images not showing in the jlabel/jbutton
    Problem in imageicon
    iconImage on JButton & JLabel
    Re: images not showing in the jlabel/jbutton

  • Question about GridBagConstraints

    Hi,
    As the title of this thread suggests, I have some issued with the GridBagConstraints. I try to set the width and height of a JPanel (that uses said GridBagConstraints), but the JPanel decides to do it in its own way. Instead of listening to my suggestion (see me setting the size of descriptionPanel in my code in the second piece of code), it sets the size of my TlsProblem to the size of the JLabel (see theDescription in MyMain) I put in it.
    Here is my code. First the main:
    import javax.swing.*;
    import java.awt.*;
    public class MyMain {
         static int WIDTH = 1280;
         static int HEIGHT = 800;
         public static void main(String[] args) {
              JFrame frame = new JFrame();
              Container pane = frame.getContentPane();
              frame.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setTitle("ProblemPainter");
              Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
              frame.setBounds (0, 0, dimension.width, dimension.height - 200);
              String theOrganization = "Organization name";
              String theDescription = "10:09:30 ERR expected string or buffer, but that is not everyting, as this problem description is extremely long. And as I am chatty, I add some more length";
              String theIdentifier = "3549";
              String theSeverity = "ERR";
              String theLink = "https://foo.domain.us/mon/index.php?fuseaction=orgtree_monitor_check.dsp_edit&mc_id=3549&org_id=17";
              String theTimeStamp = "10:09:30";
              String theCollector = "28";
              Problem problem = new Problem (theOrganization, theDescription, theIdentifier,
                        theSeverity, theLink, theTimeStamp, theCollector);
              TlsProblem tlsProblem = new TlsProblem (problem);
              frame.add(tlsProblem);
              // frame.pack();
              frame.setVisible(true);
    }Next the code of TlsProblem, that seems to have a life of its own:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TlsProblem extends JPanel
                                implements ActionListener {
         Problem problem;
         JLabel organization;
         JLabel description;
         JLabel timeStamp;
         JLabel severity;
         JButton status;
         JButton csd;
         JButton engineer;
         static int WIDTH = 1280;
         static int HEIGHT = 800;
         static int INSETS = 2;
         public TlsProblem (Problem theProblem) {
              problem = theProblem;
              organization = new JLabel("Organization: " + problem.getOrganization(), SwingConstants.LEFT);
              description = new JLabel("Description: " + problem.getDescription(), SwingConstants.LEFT);
              timeStamp = new JLabel(problem.getTimeStamp(), SwingConstants.LEFT);
              timeStamp.setAlignmentX(Component.CENTER_ALIGNMENT);
              severity = new JLabel(problem.getSeverity(), SwingConstants.LEFT);
              severity.setAlignmentX(Component.CENTER_ALIGNMENT);
              status = new JButton (problem.getState());
              status.setAlignmentX(SwingConstants.CENTER);
              csd = new JButton ("CSD: ");
              csd.setHorizontalAlignment(SwingConstants.LEFT);
              engineer = new JButton ("Beheer: ");
              engineer.setHorizontalAlignment(SwingConstants.LEFT);
              setUp();
         private void setUp () {
              JComponent component = createComponent();
              add(component);
              setVisible (true);
         private JComponent createComponent () {
              JPanel panel = new JPanel();
              panel.setBackground(Color.WHITE);
              panel.setSize(WIDTH, HEIGHT/10);
              logState ("createComponent: panel has dimension " + panel.getSize());
              panel.setLayout(new GridBagLayout());
              panel.setBorder(BorderFactory.createRaisedBevelBorder());
              GridBagConstraints constraints = new GridBagConstraints();
              // Organization name. Fill up available space horizontal
              constraints.fill = GridBagConstraints.HORIZONTAL;
              constraints.gridx = 0;
              constraints.gridy = 0;
              constraints.gridheight = 1;
              constraints.gridwidth = 2; // Take up 2 columns of space
              panel.add(organization, constraints);
              // Description of the problem. Fill up space both horizontal and vertical
              JPanel descriptionPanel = new JPanel();
              descriptionPanel.setOpaque(false);
              descriptionPanel.setSize(WIDTH/4, HEIGHT/10);
              logState ("createComponent: descriptionPanel has dimension " + descriptionPanel.getSize());
              constraints.fill = GridBagConstraints.BOTH;
              constraints.gridx = 0;
              constraints.gridy = 1;
              constraints.gridwidth = 2;
              constraints.gridheight = 2;// Take up 2 rows of space
              descriptionPanel.add(description);
              panel.add(descriptionPanel, constraints);
              logState ("createComponent: panel has dimension " + panel.getSize());
              // Time stamp of creation of the problem
              constraints.fill = GridBagConstraints.HORIZONTAL;
              constraints.gridx = 3;
              constraints.gridy = 0;
              constraints.gridwidth = 1;
              constraints.gridheight = 1;
              panel.add(timeStamp, constraints);
              // Severity of the problem
              constraints.gridx = 3;
              constraints.gridy = 1;
              constraints.gridwidth = 1;
              constraints.gridheight = 1;
              panel.add(severity, constraints);
              // Status of the problem
              constraints.gridx = 3;
              constraints.gridy = 2;
              constraints.gridwidth = 1;
              constraints.gridheight = 1;
              panel.add(status, constraints);
              // CSD engineer following the problem
              constraints.gridx = 4;
              constraints.gridy = 0;
              constraints.gridwidth = 1;
              constraints.gridheight = 1;
              panel.add(csd, constraints);
              // Administrator engineer following the problem
              constraints.gridx = 4;
              constraints.gridy = 1;
              constraints.gridwidth = 1;
              constraints.gridheight = 1;
              panel.add(engineer, constraints);
              return panel;
         public void actionPerformed(ActionEvent event) {
              String command = event.getActionCommand();
         public void logState (String text) {
              System.out.println ("TlsProblem." + text);
    public class Problem extends JPanel {
         private String organization;
         private String description;
         private String identifier;
         private String link;
         private String severity;
         private String state;
         private String timeStamp;
         private String collector;
         private int width;
         private int height;
         public Problem (String theOrganization, String theDescription, String theIdentifier,
                   String theSeverity, String theLink, String theTimeStamp, String theCollector) {
              organization = theOrganization;
              description  = theDescription;
              identifier   = theIdentifier;
              severity     = theSeverity;
              link         = theLink;
              timeStamp    = theTimeStamp;
              collector    = theCollector;
              state = NEW;
         public String getOrganization () {
              return organization;
         public String getDescription () {
              return description;
         public String getIdentifier () {
              return identifier;
         public String getSeverity () {
              return severity;
         public String getState () {
              return state;
         public String getTimeStamp () {
              return timeStamp;
         public String getCollector () {
              return collector;
         public String getLink () {
              return link;
    }What do I need to change in this code so I can set the size of the elements of my TlsProblem to my hearths content?
    TIA
    Abel

    class TlsProblem extends JPanel  implements ActionListener {
        static int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
        static int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
        private JComponent createComponent() {
            JPanel panel = new JPanel();
            panel.setBackground(Color.WHITE);
            panel.setPreferredSize(new Dimension(WIDTH-20, HEIGHT / 5));
            logState("createComponent: panel has dimension " + panel.getPreferredSize());
            panel.setLayout(new GridBagLayout());
            panel.setBorder(BorderFactory.createRaisedBevelBorder());
            GridBagConstraints constraints = new GridBagConstraints();
            // Organization name. Fill up available space horizontal
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridx = 0;
            constraints.gridy = 0;
            constraints.gridheight = 1;
            constraints.gridwidth = 2; // Take up 2 columns of space
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            panel.add(organization, constraints);
            // Description of the problem. Fill up space both horizontal and vertical
            JPanel descriptionPanel = new JPanel();
            descriptionPanel.setOpaque(false);
            descriptionPanel.setPreferredSize(new Dimension(WIDTH / 2, HEIGHT / 10));
            logState("createComponent: descriptionPanel has dimension " + descriptionPanel.getPreferredSize());
            constraints.fill = GridBagConstraints.BOTH;
            constraints.gridx = 0;
            constraints.gridy = 1;
            constraints.gridwidth = 2;
            constraints.gridheight = 2;// Take up 2 rows of space
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            description.setPreferredSize(descriptionPanel.getPreferredSize());
            description.setText("<html>"+description.getText());
            descriptionPanel.add(description);
            panel.add(descriptionPanel, constraints);
            logState("createComponent: panel has dimension " + panel.getPreferredSize());
            // Time stamp of creation of the problem
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridx = 3;
            constraints.gridy = 0;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            panel.add(timeStamp, constraints);
            // Severity of the problem
            constraints.gridx = 3;
            constraints.gridy = 1;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            panel.add(severity, constraints);
            // Status of the problem
            constraints.gridx = 3;
            constraints.gridy = 2;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            panel.add(status, constraints);
            // CSD engineer following the problem
            constraints.gridx = 4;
            constraints.gridy = 0;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            panel.add(csd, constraints);
            // Administrator engineer following the problem
            constraints.gridx = 4;
            constraints.gridy = 1;
            constraints.gridwidth = 1;
            constraints.gridheight = 1;
            constraints.weightx = constraints.gridwidth;
            constraints.weighty = constraints.gridheight;
            panel.add(engineer, constraints);
            return panel;
    }

  • Problem with setCursor for JButton in cell on JTable

    i have a problem with setCursor to JButton that is in a cell of the JTable}
    this is the code
    public class JButtonCellRenderer extends JButton implements TableCellRenderer {
    public JButtonCellRenderer() {
    setOpaque(true);
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus,
    int row, int column) {
    if (isSelected) {
    setForeground(table.getSelectionForeground());
    setBackground(table.getSelectionBackground());
    else {
    setForeground(table.getForeground());
    setBackground(Color.WHITE);
    setBorder(null);
    if(table.getValueAt(row,column) == null){
    setText("");
    if(isSelected){
    setBackground(table.getSelectionBackground());
    }else{
    setBackground(Color.WHITE);
    }else{
    if(table.getValueAt(row,column) instanceof JButton){
    JButton _button = (JButton)table.getValueAt(row,column);
    if(_button.getText().trim().equals("")){
    setText("<html>"+ "<font size=\"3\" COLOR=\"#0000FF\"><u><b>Ver...</b></u></font>"+"");
    }else{
    setText("<html>"+ "<font size=\"3\" COLOR=\"#0000FF\"><u><b>" + _button.getText() + "</b></u></font>"+"");
    return this;
    }

    Not quite sure I understand the problem, but I guess you have tried to call setCursor on your renderer and found it didn't work? The reason, I think, is that the renderer only renders a JButton. The thing that ends up in the table is in fact only a "picture" of a JButton, and among other things it won't receive and interact with any events, such as MouseEvents (so it wouldn't know that the cursor should change).
    There are at least two alternatives for you: You can perhaps use a CellEditor instead of a CellRenderer. Or you can add a MouseListener to the table and then use the methods column/rowAtPoint to figure out if the cursor is over your JButton cell and change the cursor yourself if it is.

  • Problem in Setting Background Color for JButton

    Hi,
    I am using Background color for JButton (color is selected from the customized JColorChooser created by us) where it contains the option of transparency. If we choose light color, then the background is set to JButton, but when we mouse-over the JButton, other components inside the container (JPanel) is getting displayed inside the button.
    Can anyone help to sort out this problem.
    Thanks in advance.

    Hi,
    Here is the code.
    DialogWrapper is JDialog and ColorEditorPane is JPanel.
    DialogWrapper     dlg         = new DialogWrapper();
    ColorEditorPane colorPanel = new ColorEditorPane();
    colorPanel.setValue( _data.getTransparentColor());
    dlg.showWrappedDialog( colorPanel, "color_help", "color_dialog" );
    if( dlg.isOK())
    Color chosenColor = (Color) colorPanel.getValue().getContent();
    _data.setTransparentColor( chosenColor );
    _transparentColorDisplay.setBackground( chosenColor );
    _transparentColorDisplay.setOpaque( true );
    this.repaint();
    this.doLayout();
    this.validate();
    }I've several components like JCheckBox, JRadioButton, JTextField in "this".

Maybe you are looking for