Using JButtons in a JTable

I'm trying to find some information about how to use a JButton within a cell of a JTable. I'm able to insert the button using a custom cell renderer. Basically I create my own renderer which is triggered for JButton.class. That works well.
Of course I do a addActionListener() on the JButton so something will happen when it is pressed.
But the problem is that events aren't getting through to the button at all. The table is getting all the mouse events and is not passing them on.
I'm sure I'm not the first to run into this. Any suggestions on how to get the events to the button, or otherwise deal with this problem?
Thanks

Those example are from 1998. I don't think they work anymore.
Basically, if I could just find a way to get the JTable to send any kind of event to any class that I can write, I would be happy. That would do the trick.
One possibility is to just use a mouse listener, although that seems like a very low-level way to do something which should be at a higher level. But that seems like the only way. Try as I might, I can't find a way to set an editor for these cells.
I used table.setDefaultEditor(JButton.class, myTableCellEditor), and I tried to set it on the column, too, but myTableCellEditor never gets called in response to events.
Any other approaches?
Thanks

Similar Messages

  • How to use JButton as a JTable column header?

    I'd like to use JButtons as my JTable's column headers.
    I built a JButton subclass which implemented TableCellRenderer and passed it to one of my table column's setHeaderRender method. The resulting header is a component that looks like a JButton but when clicked, it does not visually depress or fire any ActionEvents.

    You might want to check this example and use it accordingly for your requirements.
    http://www2.gol.com/users/tame/swing/examples/JTableExamples5.html
    Reason: The reason you're unable to perform actions on JButton is JTableHeader doesn't relay the mouse events to your JButtons.
    I hope this helps.
    have fun, ganesh.

  • Changing the size of a jbutton inside a jtable

    Hi,
    I have found this example for adding a jbutton to a jtable:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=715330
    however, I have cannot seem to figure out how to set the size of the jbutton. Currently, the jbutton is filled to the size of the cell within the jtable, but i want to have it a bit smaller and centered (the rows of my table are rather large in hieght)
    I tried the setSize()
    setPrefferedSize()
    setMinimumSize()
    setMaximumSize()methods, but nothing seems to work. Has anyone been able to do this?

    Use a JPanel instead of a JButton as cell renderer. Put a button inside the panel - use FlowLayout.

  • How to include a jButton in the jTable CellEditor?

    sir,
    how can include a jButton in the jTable cell Editor as we include checkbox & Combo inthe Cell Editor?
    thks.

    There is c:import tag in the JSTL that will include the HTML from a file on another server.

  • Multiple clicks using JButtons

    I am using JButtons to trigger events in an experiment and then logging data using the System.time method.
    However I seem to be getting multiple printouts when a button is only clicked a single time. I have setup the process using the ActionListener class and then have an if statement and a few else if's to determine when a button has been pressed. Not quite sure why I am getting this multiple output, it also seems to be proportional to the amount of times other buttons have been pressed.
    Eg. the printout i get is like below
    First test: 1133 milliseconds
    Second test: 764 milliseconds
    Second test: 767 milliseconds
    Third test: 1461 milliseconds
    Third test: 1464 milliseconds
    Third test: 1466 milliseconds
    Fourth test: 1533 milliseconds
    Fourth test: 1536 milliseconds
    Fourth test: 1538 milliseconds
    Fourth test: 1539 milliseconds
    Fifth test: 669 milliseconds
    Fifth test: 672 milliseconds
    Fifth test: 673 milliseconds
    Fifth test: 674 milliseconds
    Fifth test: 676 milliseconds
    Sixth test: 549 milliseconds
    Sixth test: 552 milliseconds
    Sixth test: 554 milliseconds
    Sixth test: 555 milliseconds
    Sixth test: 556 milliseconds
    Sixth test: 558 milliseconds
    Cheers
    Aaron

    Here's the code, have two variables that store the time before the experiment is done and one that gets the time after the experiment. It then subtracts the two to work out the time taken and then prints this out to an output file.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.awt.geom.*;
    import java.io.*;
    //defines JPanel in which graphic objects will be placed
    class Experiment extends JPanel
       //defines all the Global variables to be used in the function
       private Rectangle background;
       private long before, after;
       private String comment = "Please press the Start button and then Select button";
       private String output = "", error = "";
       //sets up a listener to monitor when a button has been pressed
       MyListener listener = new MyListener();
       //defines all the start buttons and links them to the listener
       private JButton button1 = button("Start", this, listener, "centre");
       private JButton button2 = button("Start", this, listener, "centre");
       private JButton button3 = button("Start", this, listener, "centre");
       private JButton button4 = button("Start", this, listener, "centre");
       private JButton button5 = button("Start", this, listener, "centre");
       private JButton button16 = button("Start", this, listener, "centre");
       private JButton button17 = button("Start", this, listener, "centre");
       private JButton button18 = button("Start", this, listener, "centre");
       private JButton button19 = button("Start", this, listener, "centre");
       //defines all the buttons to be used in the layout
       private JButton button6 = button("System", this, listener, "");
       private JButton button7 = button("Click", this, listener, "");
       private JButton button8 = button("Window", this, listener, "");
       private JButton button9 = button("Timer", this, listener, "");
       private JButton button10 = button("Calc", this, listener, "");
       private JButton button11 = button("Press", this, listener, "");
       private JButton button12 = button("Select", this, listener, "");
       private JButton button13 = button("Start", this, listener, "");
       private JButton button14 = button("Menu", this, listener, "");
       private JButton button15 = button("Don't", this, listener, "");
       //this function initialises all the graphic objects that will be in the
       //picture and their default colors
       Experiment()
          background = new Rectangle(0,0,1275,765);
          setVisible(true);
          output = "Scatter starting list Test Results: ";
          tryWriteFile(output);
       //function to paint all of the graphics onto the JPanel
       public void paintComponent(Graphics g)
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(new Color(255,255,255));
          g2.fill(background);
          g2.setColor(new Color(0,0,0));
          g2.drawString(comment, 500, 300);
       //function to add buttons onto the current JPanel
       JButton button(String name, JPanel panel, MyListener lis, String position)
          JButton b = new JButton(name);
          if (position == "centre")
             b.setBounds(600,325,75,40);
             panel.add(b);
             b.addActionListener(lis);
          else {}//requires other positions for buttons
          return b;
       //listener module to perform tasks when buttons are pressed
       class MyListener implements ActionListener
          public void actionPerformed(ActionEvent e)
             Object source = e.getSource();
             if(source == button1)
                setupBoard1st();
                before = System.currentTimeMillis();
             else if(source == button12)
                after = System.currentTimeMillis();
                output = "First test: " + (after - before) + " milliseconds";
                tryWriteFile(output);
                resetBoard1();
             else if(source == button2)
                setupBoard2();
                before = System.currentTimeMillis();
             else if(source == button8)
                after = System.currentTimeMillis();
                output = "Second test: " + (after - before) + " milliseconds";
                tryWriteFile(output);
                resetBoard2();
             else if(source == button3)
                setupBoard3();
                before = System.currentTimeMillis();
             else if(source == button6)
                after = System.currentTimeMillis();
                output = "Third test: " + (after - before) + " milliseconds";
                tryWriteFile(output);
                resetBoard3();
             else if(source == button17)
                setupBoard4();
                before = System.currentTimeMillis();
             else if(source == button18)
                setupBoard5();
                before = System.currentTimeMillis();
             else if(source == button19)
                setupBoard6();
                before = System.currentTimeMillis();
             else if(source == button9)
                after = System.currentTimeMillis();
                output = "Fourth test: " + (after - before) + " milliseconds";
                tryWriteFile(output);
                resetBoard4();
             else if(source == button10)
                after = System.currentTimeMillis();
                output = "Fifth test: " + (after - before) + " milliseconds";
                tryWriteFile(output);
                resetBoard5();
             else if(source == button14)
                after = System.currentTimeMillis();
                output = "Sixth test: " + (after - before) + " milliseconds";
                tryWriteFile(output);
                finish();
             repaint();
       void setupBoard1st()
          hideStartButtons();
          comment = "";
          // needs to setup where all the other buttons are
          listButtons(20, 20);
       void setupBoard2()
          hideStartButtons();
          comment = "";
          scatterButtons1();
       void setupBoard3()
          hideStartButtons();
          comment = "";
          listButtons1(20,20);
       void setupBoard4()
          hideStartButtons();
          comment = "";
          scatterButtons();
       void setupBoard5()
          hideStartButtons();
          comment = "";
          listButtons2(20, 20);
       void setupBoard6()
          hideStartButtons();
          comment = "";
          scatterButtons2();
       //next 8 methods all reset the board to contain a start button in the centre
       void resetBoard1()
          hideButtons();
          button2.setVisible(true);
          comment = "Please press Start button and then Window button";
       void resetBoard2()
          hideButtons();
          button3.setVisible(true);
          comment = "Please press Start button and then System button";
       void resetBoard3()
          hideButtons();
          button17.setVisible(true);
          comment = "Please press Start button and then Timer button";
       void resetBoard4()
          hideButtons();
          button18.setVisible(true);
          comment = "Please press Start button and then Calc button";
       void resetBoard5()
          hideButtons();
          button19.setVisible(true);
          comment = "Please press Start button and then Menu button";
       //adds a button onto the current JPanel
       JButton addButton(JButton select, int pos, int align)
          //Set button size and position
          select.setBounds(align,pos,100,40);
          this.add(select);
          select.addActionListener(listener);
          return select;
       //outputs error message to saved file
       void fail(String message)
          message = "Invalid" + message;
          tryWriteFile(message);
       //hides all the buttons used on the selection screens
       void hideButtons()
          button6.setVisible(false);
          button7.setVisible(false);
          button8.setVisible(false);
          button9.setVisible(false);
          button10.setVisible(false);
          button11.setVisible(false);
          button12.setVisible(false);
          button13.setVisible(false);
          button14.setVisible(false);
          button15.setVisible(false);
       //makes all the buttons on the selection screen visible
       void showButtons()
          button6.setVisible(true);
          button7.setVisible(true);
          button8.setVisible(true);
          button9.setVisible(true);
          button10.setVisible(true);
          button11.setVisible(true);
          button12.setVisible(true);
          button13.setVisible(true);
          button14.setVisible(true);
          button15.setVisible(true);
       //sets up order of buttons in list
       void listButtons(int start, int align)
          addButton(button6, start, align);
          addButton(button7, start + 60, align);
          addButton(button8, start + 300, align);
          addButton(button9, start + 180, align);
          addButton(button10, start + 240, align);
          addButton(button11, start + 120, align);
          addButton(button12, start + 360, align);
          addButton(button13, start + 420, align);
          addButton(button14, start + 480, align);
          addButton(button15, start + 540, align);
          showButtons();
       //sets up re-arranged order of buttons in list
       void listButtons1(int start, int align)
          addButton(button6, start + 180, align);
          addButton(button7, start + 540, align);
          addButton(button8, start + 360, align);
          addButton(button9, start + 480, align);
          addButton(button10, start, align);
          addButton(button11, start + 300, align);
          addButton(button12, start + 120, align);
          addButton(button13, start + 420, align);
          addButton(button14, start + 240, align);
          addButton(button15, start + 60, align);
          showButtons();
       //sets up different order of buttons in list
       void listButtons2(int start, int align)
          addButton(button6, start + 120, align);
          addButton(button7, start + 240, align);
          addButton(button8, start, align);
          addButton(button9, start + 480, align);
          addButton(button10, start + 60, align);
          addButton(button11, start + 420, align);
          addButton(button12, start + 360, align);
          addButton(button13, start + 300, align);
          addButton(button14, start + 180, align);
          addButton(button15, start + 540, align);
          showButtons();
       //sets up scattering of buttons
       void scatterButtons()
          addButton(button6, 200, 760);
          addButton(button7, 400, 100);
          addButton(button8, 50, 450);
          addButton(button9, 675, 1068);
          addButton(button10, 125, 250);
          addButton(button11, 350, 20);
          addButton(button12, 180, 200);
          addButton(button13, 500, 600);
          addButton(button14, 450, 885);
          addButton(button15, 300, 300);
          showButtons();
       //sets up different scattering of buttons
       void scatterButtons1()
          addButton(button6, 400, 1000);
          addButton(button7, 500, 300);
          addButton(button8, 200, 250);
          addButton(button9, 250, 750);
          addButton(button10, 100, 1166);
          addButton(button11, 600, 100);
          addButton(button12, 500, 500);
          addButton(button13, 250, 400);
          addButton(button14, 300, 605);
          addButton(button15, 600, 900);
          showButtons();
       //sets up third type of scattering of buttons
       void scatterButtons2()
          addButton(button6, 50, 200);
          addButton(button7, 100, 900);
          addButton(button8, 600, 250);
          addButton(button9, 500, 700);
          addButton(button10, 300, 500);
          addButton(button11, 400, 300);
          addButton(button12, 650, 400);
          addButton(button13, 200, 650);
          addButton(button14, 425, 0);
          addButton(button15, 450, 825);
          showButtons();
       //tries to write to file otherwise calls error code
       void tryWriteFile(String data)
          try
             saveFile(output);
          catch(IOException error)
             fail("output file could not be written to correctly");
       //saves data passed to it into file
       void saveFile(String data) throws IOException
          PrintWriter output = new PrintWriter(new FileWriter ("HCIout.txt", true));
          output.println(data);
          output.close();
       //method for finishing the experiment
       void finish()
          comment = "Thank you for your time, test is now finished";
          hideButtons();
       void hideStartButtons()
          button1.setVisible(false);
          button2.setVisible(false);
          button3.setVisible(false);
          button4.setVisible(false);
          button5.setVisible(false);
          button16.setVisible(false);
          button17.setVisible(false);
          button18.setVisible(false);
          button19.setVisible(false);
    }

  • JButtons into a JTable

    Is it possible for me to get a JButton into and JTable? The Table contents come from the code below.
    server myserver= new server();//make object of this class
    network mynetwork= new network(myserver);// make network objectand pass server as refrence
    int row,col;
              //initialise string database for hosts
              for (row=0;row<50;row++){               
                   myserver.hostdb[row][0]=""+iconButtons;
                   for (col=1;col<8;col++){
                   myserver.hostdb[row][col]="";
    Any help would be appriciated thanks.

    Yes
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender

  • Headache: Data Xchange from JDialog to JTable using JButton

    My head is spinning about how to get this to work. What is really confusing me is the data exchange from the JDialog.
    I have a JTable where one of the columns uses a JButton as the cell editor for each cell in that column. That part seems to be no problem. The JButton is labeled "Define Staff..."
    When the user clicks on the JButton, a JDialog containing a JList is to appear. If the user clicks cancel, the JDialog is disposed and nothing happens. If the user clicks OK, the selected items are to be returned to the cell in some way and the label of the JButton is to change to "Modify Staff..."
    How can I return the list from the JDialog to the "cell" so that when I access the cell using getValueAt() I get the list of values, and not the JButton object?

    Thanks.
    The actual JButton is inside a JTable cell though.
    Where would I add the event handler for the button? In the editor? In the renderer?
    Excerpt from the JTable file:
         PositionTable.getColumn("Staff").setCellRenderer(new ButtonRenderer());
         PositionTable.getColumn("Staff").setCellEditor(new ButtonEditor(new JCheckBox()));ButtonEditor.java
    public class ButtonEditor extends DefaultCellEditor {
      protected JButton button;
      private String    label;
      private boolean   isPushed;
      public ButtonEditor(JCheckBox checkBox) {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            fireEditingStopped();
      public Component getTableCellEditorComponent(JTable table, Object value,
                       boolean isSelected, int row, int column) {
        if (isSelected) {
        } else{
          button.setForeground(table.getForeground());
          button.setBackground(table.getBackground());
        label = (value ==null) ? "" : value.toString();
        isPushed = true;
        return button;
      public Vector<String> getCellEditorValue() {
        Vector<String> staff = new Vector<String>();
        if (isPushed)  {
          staffSelector temp = new staffSelector(null,true,"Building Supervisor");
          temp.setVisible(true);
          staff = temp.getSelectedStaff(true);
        isPushed = false;
       // return new String( label ) ;
       //Return vector people selected.
       return staff;
      public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
      protected void fireEditingStopped() {
        super.fireEditingStopped();
    }ButtonRenderer.java
    public class ButtonRenderer extends JButton implements TableCellRenderer {
      public ButtonRenderer() {
        setOpaque(true);
      public Component getTableCellRendererComponent(JTable table, Object value,
                       boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
          setForeground(table.getForeground());
          setBackground(table.getBackground());
        } else{
          setForeground(table.getForeground());
          setBackground(UIManager.getColor("Button.background"));
        setText( (value ==null) ? "Define Staff..." : "Modify Staff..." );
        return this;
    }Message was edited by:
    AsymptoticCoder
    Made an error.

  • Multiple JButtons in a JTable cell: handling events

    Hello!
    I'm trying to develop a Swing application which makes use of tables in several panels.
    Each row of each table should present the user two buttons, one for editing the row, one for viewing details of that row. Both editing and viewing are done in another panel.
    I thought I could add the buttons as the last column of each row, so I made a panel which holds the two buttons and the id of the row, so that I know what I have to edit or view.
    I managed to insert the panel as the last column, but I can't have the buttons click.
    I studied cell renderers and editors from several tutorials and examples, but evidently I can't understand either of them: whatever I try doesn't change the outcome... :(
    Below is the code I use, except for imports and packages:
    ActionPanel.java - The panel which holds the buttons and the row id
    public class ActionPanel extends JPanel{
      private static final long serialVersionUID = 1L;
      public static String ACTION_VIEW="view";
      public static String ACTION_EDIT="edit";
      private String id;
      private JButton editButton;
      private JButton viewButton;
      public String getId() {
        return id;
      public void setId(String id) {
        this.id = id;
      public JButton getEditButton() {
        return editButton;
      public void setEditButton(JButton editButton) {
        this.editButton = editButton;
      public JButton getViewButton() {
        return viewButton;
      public void setViewButton(JButton viewButton) {
        this.viewButton = viewButton;
      public ActionPanel() {
        super();
        init();
      public ActionPanel(String id) {
        super();
        this.id = id;
        init();
      private void init(){
        setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0))
        editButton=new JButton(new ImageIcon("./images/icons/editButtonIcon.png"));
        editButton.setBorderPainted(false);
        editButton.setOpaque(false);
        editButton.setAlignmentX(TOP_ALIGNMENT);
        editButton.setMargin(new Insets(0,0,0,0));
        editButton.setSize(new Dimension(16,16));
        editButton.setMaximumSize(new Dimension(16, 16));
        editButton.setActionCommand(ACTION_EDIT);
        editButton.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, id, "Editing", JOptionPane.INFORMATION_MESSAGE);
        viewButton=new JButton(new ImageIcon("./images/icons/viewButtonIcon.png"));
        viewButton.setMaximumSize(new Dimension(16, 16));
        viewButton.setActionCommand(ACTION_VIEW);
        viewButton.setBorderPainted(false);
        viewButton.setOpaque(false);
        viewButton.setMargin(new Insets(0,0,0,0));
        viewButton.setSize(new Dimension(16,16));
        viewButton.setMaximumSize(new Dimension(16, 16));
        viewButton.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, id, "Viewing", JOptionPane.INFORMATION_MESSAGE);
        add(viewButton);
        add(editButton);
    ActionPanelRenerer.java - the renderer for the above panel
    public class ActionPanelRenderer implements TableCellRenderer{
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component ret=(Component)value;
        if (isSelected) {
          ret.setForeground(table.getSelectionForeground());
          ret.setBackground(table.getSelectionBackground());
        } else {
          ret.setForeground(table.getForeground());
          ret.setBackground(UIManager.getColor("Button.background"));
        return ret;
    ActionPanelEditor.java - this is the editor, I can't figure out how to implement it!!!!
    public class ActionPanelEditor extends AbstractCellEditor implements TableCellEditor{
      public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column) {
        return (ActionPanel)value;
      public Object getCellEditorValue() {
        return null;
    ServicesModel.java - The way I fill the table is through a model:
    public class ServicesModel extends AbstractTableModel  {
      private Object[][] data;
      private String[] headers;
      public ServicesModel(Object[][] services, String[] headers) {
        this.data=services;
        this.headers=headers;
      public int getColumnCount() {
        return headers.length;
      public int getRowCount() {
        return data.length;
      public Object getValueAt(int row, int col) {
        if(col==data.length-1)
          return new ActionPanel(""+col);
        else
          return data[row][col];
      public boolean isCellEditable(int row, int col) {
        return false;
      public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    ServicesList.java - The panel which holds the table (BasePanel is a custom class, not related to the table)
    public class ServicesList extends BasePanel {
      private JLabel label;
      private JTable grid;
      public ServicesList(SessionManager sessionManager){
        super(sessionManager);
        grid=new JTable() ;
        add(new JScrollPane(grid), BorderLayout.CENTER);
        layoutComponents();
      public void layoutComponents(){
        ConfigAccessor dao=new ConfigAccessor(connectionUrl, connectionUser, connectionPass);
        String[] headers=I18N.get(dao.getServiceLabels());
        grid.setModel(new ServicesModel(dao.getServices(), headers));
        grid.setRowHeight(20);
        grid.getColumnModel().getColumn(headers.length-1).setCellRenderer(new ActionPanelRenderer());
        grid.setDefaultEditor(ActionPanel.class, new ActionPanelEditor());
        grid.removeColumn(grid.getColumnModel().getColumn(0));
        dao.close();
    }Please can anyone at least address me to what I'm doing wrong? Code would be better, but examples or hints will do... ;)
    Thank you very much in advance!!!

    Hello!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class MultipleButtonsInCellTest {
      public JComponent makeUI() {
        String[] columnNames = {"String", "Button"};
        Object[][] data = {{"AAA", null}, {"BBB", null}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {
          @Override public Class<?> getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        JTable table = new JTable(model);
        table.setRowHeight(36);
        ActionPanelEditorRenderer er = new ActionPanelEditorRenderer();
        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellRenderer(er);
        column.setCellEditor(er);
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JScrollPane(table));
        p.setPreferredSize(new Dimension(320, 200));
        return p;
      public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() { createAndShowGUI(); }
      public static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new MultipleButtonsInCellTest().makeUI());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    class ActionPanelEditorRenderer extends AbstractCellEditor
                       implements TableCellRenderer, TableCellEditor {
      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      public ActionPanelEditorRenderer() {
        super();
        JButton viewButton2 = new JButton(new AbstractAction("view2") {;
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Viewing");
        JButton editButton2 = new JButton(new AbstractAction("edit2") {;
          public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Editing");
        panel1.setOpaque(true);
        panel1.add(new JButton("view1"));
        panel1.add(new JButton("edit1"));
        panel2.setOpaque(true);
        panel2.add(viewButton2);
        panel2.add(editButton2);
      @Override
      public Component getTableCellRendererComponent(JTable table, Object value,
                   boolean isSelected, boolean hasFocus, int row, int column) {
        panel1.setBackground(isSelected?table.getSelectionBackground()
                                       :table.getBackground());
        return panel1;
      @Override
      public Component getTableCellEditorComponent(JTable table, Object value,
                                    boolean isSelected, int row, int column) {
        panel2.setBackground(table.getSelectionBackground());
        return panel2;
      @Override
      public Object getCellEditorValue() {
        return null;
    }

  • Opening new forms using jbutton

    Hi i'm new to java, and i am currentley building an interface which has three buttons(cars,vans,red car). I have managed to create a class that opens a new window when the car button is clicked. However i am unsure of how to create different windows for when the other buttons are clicked.
    Heres my code ive based it on an example i found on here.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    public class Interface extends JPanel implements ActionListener {
    JDialog dialog;
    public Interface(JFrame f)
    dialog = new JDialog(f, "dialog");
    dialog.getContentPane().add(new JLabel("hello world", JLabel.CENTER));
    dialog.setSize(400,300);
    dialog.setLocation(425,200);
    public void actionPerformed(ActionEvent e)
    if(!dialog.isVisible())
    dialog.setVisible(true);
    else
    dialog.toFront();
    public JPanel getPanel()
    JPanel panel = new JPanel();
    JPanel selectPanel = new JPanel();
    JPanel ControlPanel = new JPanel();
    selectPanel.setBorder(BorderFactory.createCompoundBorder(
    BorderFactory.createTitledBorder("Select Piece Type:"),
    BorderFactory.createEmptyBorder(5,5,5,5)));
    JButton cars = new JButton("Cars");
    cars.addActionListener(this);
    JButton vans = new JButton("Vans");
    JButton redcar = new JButton("Red Car");
    selectPanel.add(cars);
    selectPanel.add(vans);
    selectPanel.add(redcar);
    panel.add(selectPanel);
    return panel;
    public static void main(String[] args)
    JFrame f = new JFrame();
    Interface ae = new Interface(f);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(ae.getPanel(), "North");
    f.setSize(400,200);
    f.setLocation(200,200);
    f.setVisible(true);
    Thanks for any help

    Thanks for the reply, i am now having trouble adding buttons to the dialog box. This is the code i am using:
    dialog = new JDialog(f, "dialog");
            dialog.getContentPane().add(new JLabel("hello world", JLabel.CENTER));
            dialog.setSize(400,300);
            dialog.setLocation(425,200);
            JButton buttonQuestion = new JButton( "Question" );
            dialog.getContentPane().add(buttonQuestion);For some reasson this turns the whole dialog pane into a button. So all i get is a dialog box with question in the middle. What i need is the hello world label to be displayed, with the questin button underneath
    Thanks for any help

  • JButton / JCalendar on JTable

    Hi,
    I want to add JButtons and or JCalendar components in my JTable.
    How can I do this?
    I have the "How to Use Tables" from sun in printed format but cannot figure out how to do this...

    An example of using a JButton can be found in the tutorial you mentioned
    (from said tut)
    public class ColorEditor extends AbstractCellEditor
                             implements TableCellEditor,
                                        ActionListener {
        Color currentColor;
        JButton button;
        JColorChooser colorChooser;
        JDialog dialog;
        protected static final String EDIT = "edit";
        public ColorEditor() {
            button = new JButton();
            button.setActionCommand(EDIT);
            button.addActionListener(this);
            button.setBorderPainted(false);
            //Set up the dialog that the button brings up.
            colorChooser = new JColorChooser();
            dialog = JColorChooser.createDialog(button,
                                            "Pick a Color",
                                            true,  //modal
                                            colorChooser,
                                            this,  //OK button handler
                                            null); //no CANCEL button handler
        public void actionPerformed(ActionEvent e) {
            if (EDIT.equals(e.getActionCommand())) {
                //The user has clicked the cell, so
                //bring up the dialog.
                button.setBackground(currentColor);
                colorChooser.setColor(currentColor);
                dialog.setVisible(true);
                fireEditingStopped(); //Make the renderer reappear.
            } else { //User pressed dialog's "OK" button.
                currentColor = colorChooser.getColor();
        //Implement the one CellEditor method that AbstractCellEditor doesn't.
        public Object getCellEditorValue() {
            return currentColor;
        //Implement the one method defined by TableCellEditor.
        public Component getTableCellEditorComponent(JTable table,
                                                     Object value,
                                                     boolean isSelected,
                                                     int row,
                                                     int column) {
            currentColor = (Color)value;
            return button;
    }

  • How do i use jbutton for mutiple frames(2frames)???

    im an creating a movie database program and i have a problem with the jButtons on my second frame i dont know how to make them work when clicked on. i managed to make the jButtons work on my first frame but not on the second....
    here is that code so far----
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    * real2.java
    * Created on December 7, 2007, 9:00 AM
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    * @author J
    public class real2 extends JPanel  implements ActionListener{
        private JButton addnew;
        private JButton help;
        private JButton exit;
        private JFrame frame1;
        private JButton save;
        private JButton cancel;
        private JButton save2;
        private JLabel moviename;
        private JTextField moviename2;
        private JLabel director;
        private JTextField director2;
        private JLabel year;
        private JTextField year2;
        private JLabel genre;
        private JTextField genre2;
        private JLabel plot;
        private JTextField plot2;
        private JLabel rating;
        private JTextField rating2;
        /** Creates a new instance of real2 */
        public real2() {
            super(new GridBagLayout());
            //Create the Buttons.
            addnew = new JButton("Add New");
            addnew.addActionListener(this);
            addnew.setMnemonic(KeyEvent.VK_E);
            addnew.setActionCommand("Add New");
            help = new JButton("Help");
            help.addActionListener(this);
            help.setActionCommand("Help");
            exit = new JButton("Exit");
            exit.addActionListener(this);
            exit.setActionCommand("Exit");
           String[] columnNames = {"First Name",
                                    "Last Name",
                                    "Sport",
                                    "# of Years",
                                    "Vegetarian"};
            Object[][] data = {
                {"Mary", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"Alison", "Huml",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Kathy", "Walrath",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Sharon", "Zakhour",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Philip", "Milne",
                 "Pool", new Integer(10), new Boolean(false)}
            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(600, 100));
            table.setFillsViewportHeight(true);
            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);
            //Add the scroll pane to this panel.
            add(scrollPane);
            GridBagConstraints c = new GridBagConstraints();
            c.weightx = 1;
            c.gridx = 0;
            c.gridy = 1;
            add(addnew, c);
            c.gridx = 0;
            c.gridy = 2;
            add(help, c);
            c.gridx = 0;
            c.gridy = 3;
            add(exit, c);
        public static void addComponentsToPane(Container pane){
            pane.setLayout(null);
            //creating the components for the new frame
            JButton save = new JButton("Save");
            JButton save2 = new JButton("Save and add another");
            JButton cancel = new JButton("Cancel");
            JLabel moviename= new JLabel("Movie Name");
            JTextField moviename2 = new JTextField(8);
            JLabel director = new JLabel("Director");
            JTextField director2 = new JTextField(8);
            JLabel genre = new JLabel("Genre");
            JTextField genre2 = new JTextField(8);
            JLabel year = new JLabel("year");
            JTextField year2 = new JTextField(8);
            JLabel plot = new JLabel("Plot");
            JTextField plot2 = new JTextField(8);
            JLabel rating = new JLabel("Rating(of 10)");
            JTextField rating2 = new JTextField(8);
            //adding components to new frame
            pane.add(save);
            pane.add(save2);
            pane.add(cancel);
            pane.add(moviename);
            pane.add(moviename2);
            pane.add(director);
            pane.add(director2);
            pane.add(genre);
            pane.add(genre2);
            pane.add(year);
            pane.add(year2);
            pane.add(plot);
            pane.add(plot2);
            pane.add(rating);
            pane.add(rating2);
            //setting positions of components for new frame
                Insets insets = pane.getInsets();
                Dimension size = save.getPreferredSize();
                save.setBounds(100 , 50 ,
                         size.width, size.height);
                 size = save2.getPreferredSize();
                save2.setBounds(200 , 50 ,
                         size.width, size.height);
                 size = cancel.getPreferredSize();
                cancel.setBounds(400 , 50 ,
                         size.width, size.height);
                 size = moviename.getPreferredSize();
                moviename.setBounds(100 , 100 ,
                         size.width, size.height);
                size = moviename2.getPreferredSize();
                moviename2.setBounds(200 , 100 ,
                         size.width, size.height);
                 size = director.getPreferredSize();
                director.setBounds(100, 150 ,
                         size.width, size.height);
                 size = director2.getPreferredSize();
                director2.setBounds(200 , 150 ,
                         size.width, size.height);
                size = genre.getPreferredSize();
                genre.setBounds(100 , 200 ,
                         size.width, size.height);
                 size = genre2.getPreferredSize();
                genre2.setBounds(200 , 200 ,
                         size.width, size.height);
                 size = year.getPreferredSize();
                year.setBounds(100 , 250 ,
                         size.width, size.height);
                size = year2.getPreferredSize();
                year2.setBounds(200 , 250 ,
                         size.width, size.height);
                 size = plot.getPreferredSize();
                plot.setBounds(100 , 300 ,
                         size.width, size.height);
                 size = plot2.getPreferredSize();
                plot2.setBounds(200 , 300 ,
                         size.width, size.height);
                size = rating.getPreferredSize();
                rating.setBounds(100 , 350 ,
                         size.width, size.height);
                 size = rating2.getPreferredSize();
                rating2.setBounds(200 , 350 ,
                         size.width, size.height);
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            frame.add(new real2());
            //Display the window.
            frame.setSize(600, 360);
            frame.setVisible(true);
            frame.pack();
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
        public void actionPerformed(ActionEvent e) {
            if ("Add New".equals(e.getActionCommand())){
               frame1 = new JFrame("add");
               frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
               addComponentsToPane(frame1.getContentPane());
               frame1.setSize(600, 500);
               frame1.setVisible(true);
                //disableing first frame etc:-
                if (frame1.isShowing()){
                addnew.setEnabled(false);
                help.setEnabled(false);
                exit.setEnabled(true);
                frame1.setVisible(true);
               }else{
                addnew.setEnabled(true);
                help.setEnabled(true);
                exit.setEnabled(true);           
            if ("Exit".equals(e.getActionCommand())){
                System.exit(0);
            if ("Save".equals(e.getActionCommand())){
                // whatever i ask it to do it wont for example---
                help.setEnabled(true);
            if ("Save" == e.getSource()) {
                //i tried this way too but it dint work here either
                help.setEnabled(true);
    }so if someone could help me by either telling me what to type or by replacing what iv done wrong that would be great thanks...

    (1)Java class name should begin with a capital letter. See: http://java.sun.com/docs/codeconv/
            JButton save = new JButton("Save");
            JButton save2 = new JButton("Save and add another");
            // ... etc. ...(2)Don't redeclare class members in a method as its local variable. Your class members become eternally null, a hellish null.
    how to make them work when clicked on(3)Add action listener to them.

  • My JButton inside a JTable cell does not respond to clicks

    I have a Jtable which extends AbstractTableModel and uses an arraylist to fill the data in the model. The first column of this table is my button column. I have a class for my button coumn which is:
    class MyButtonCol extends AbstractCellEditor
            implements TableCellRenderer, TableCellEditor, ActionListenerMy button column class has getTableCellRendererComponent and getTableCellEditorComponent. My problem is that the button shows on the table but it does not respond to clicks. Any help will be appreciated.

    That does not seem to be the problem. I have the method in my class but it still does not respond to clicks. This is my AbstractTableModel class:
    class WorklisttableModel extends AbstractTableModel{
         protected static List<Worklist> transaction;
         protected String[] columnNames = new String[]{" ", "Modality", "Status","Patient name",
                "Patient ID","Date of birth","Study date","Referring physician","Description"};
         public WorklisttableModel(){
             transaction = new ArrayList<Worklist>();
             fillmodel();
          @Override
          public boolean isCellEditable(int row, int column) {
                return false;
          @Override
          public int getColumnCount() {
                return 9;
          @Override
          public int getRowCount() {
                return (transaction!=null) ? transaction.size() : 0;
          @Override
          public Object getValueAt(int rowIndex, int columnIndex) {
              if(rowIndex < 0 || rowIndex>=getRowCount())
                  return" ";
                  Worklist row = (Worklist)transaction.get(rowIndex);
                  switch(columnIndex){
                      //case 0:return "";
                      case 1:return " "+row.getModality();
                      case 2: return row.getStatus();
                      case 3:return row.getName();
                      case 4:return row.getID();
                      case 5:return row.getDOB();
                      case 6:return row.getStudyDate();
                      case 7:return row.getReferringP();
                      case 8:return row.getDescription();
               return " ";
          public Class getColumnClass(int col){
              return getValueAt(0,col).getClass();
          @Override
          public String getColumnName(int columnIndex) {
                return columnNames[ columnIndex ];
          protected void fillmodel(){
              transaction.add(new Worklist("","US","active","Simpson","1232222",new java.util.Date(73,8,12),new Date(18,8,13),"Dr. Francis","Brain"));
              transaction.add(new Worklist("","US","inactive","Dodggy","3498222",new java.util.Date(83,8,12),new Date(16,8,17),"Dr. Francis","Heart"));
              transaction.add(new Worklist("","CT","active","Williams","7892222",new java.util.Date(98,9,5),new Date(19,2,13),"Dr. Evans","Dental"));
              transaction.add(new Worklist("","MR","inactive","Brian","89765412",new java.util.Date(65,5,23),new Date(19,1,18),"Dr. Evans","Brain"));
              Collections.sort( transaction, new Comparator<Worklist>(){
              public int compare( Worklist a, Worklist b) {
                  return a.getName().compareTo( b.getName() );
    }

  • Renderer problem for a JButton in a JTable

    Hi all,
    Here is my requirement -
    When the value of a particular cell in a JTable gets changed, another cell in the same selected row should display a JButton. The button should remain displayed for that row. Other rows should not have the button displayed unless there is any change in value of other columns in that row.
    I have the problem in displaying the button only when the values of a particular row is changed. The button gets displayed if I click that button cell which should not happen as the values are not changed for the corresponding row. Also, once the button is displayed for a particular row, if i click some other row, button gets disappeared for the previously selected row.
    Here is the code I have implemented. Please correct me where I am going wrong.
    Renderer code for the button-
    ==================
    class ButtonRenderer extends JButton implements TableCellRenderer {
    public ButtonRenderer() {
    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(UIManager.getColor("Button.background"));
    TableModel model = table.getModel();
    // get values from the table model's selected row and specific column
    if(// check if values are changed from old ones)
    setText("Undo");
    setVisible(true);
    return (JButton) value;
    else
    JButton button = new JButton("");
    button.setBackground(Color.white);
    button.setBorderPainted(false);
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setEnabled(false);
    return button;
    Editor code for the button -
    =======================
    class ButtonCellEditor extends DefaultCellEditor
    public ButtonCellEditor(JButton aButton)
    super(new JTextField());
    setClickCountToStart(0);
    aButton.setOpaque(true);
    aButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    fireEditingStopped();
    editorComponent = aButton;
    protected void fireEditingStopped()
    super.fireEditingStopped();
    public Object getCellEditorValue()
    return editorComponent;
    public boolean stopCellEditing()
    return super.stopCellEditing();
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    return editorComponent;
    Code setting the renderer/editor -
    ==================================
    TableColumn column = colModel.getColumn(6);
    button.setVisible(true);
    button.setEnabled(true);
    column .setCellRenderer(new ButtonRenderer());
    column .setCellEditor(new ButtonCellEditor(button));
    Please correct me where I am going wrong.
    Thanks in advance!!

    Hope this is what you want...edit the column "Value" and type "Show" to get the desired result.
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    public class TableEdit2 extends JFrame
         Object[] columns =
         { "A", "Value", "Action" };
         Object[][] data =
         { "A0", "B0", "" },
         { "A1", "B1", "" },
         { "A2", "B2", "" },
         { "A3", "B3", "" },
         { "A4", "B4", "" },
         { "A5", "B5", "" },
         { "A6", "B6", "" },
         { "A7", "B7", "" },
         { "A8", "B8", "" },
         { "A9", "B9", "" },
         { "A10", "B10", "" },
         { "A11", "B11", "" } };
         JTable table = null;
         public TableEdit2()
              super("Table Edit");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              createGUI();
              setLocationRelativeTo(null);
              setSize(400, 300);
              setVisible(true);
         private void createGUI()
              table = new JTable(data, columns)
                   public void setValueAt(Object value, int row, int column)
                        super.setValueAt(value, row, column);
                        ((AbstractTableModel) table.getModel()).fireTableRowsUpdated(
                                  row, row);
              table.getColumn("Action").setCellRenderer(new ButtonRenderer());
              table.getColumn("Action").setCellEditor(new MyTableEditor(new JTextField()));
              JScrollPane sc = new JScrollPane(table);
              add(sc);
         class ButtonRenderer extends DefaultTableCellRenderer
              JButton undoButton = new JButton("Undo");
              public ButtonRenderer()
                   super();
              public Component getTableCellRendererComponent(JTable table,
                        Object value, boolean isSelected, boolean hasFocus, int row,
                        int column)
                   Component comp = super.getTableCellRendererComponent(table, value,
                             isSelected, hasFocus, row, column);
                   if (column == 2)// The Action Column
                        Object checkValue = table.getValueAt(row, 1);
                        if ("Show".equals(String.valueOf(checkValue)))
                             return undoButton;
                   return comp;
         class MyTableEditor extends DefaultCellEditor
              JButton undoButton = new JButton("Undo");
              public MyTableEditor(JTextField dummy)
                   super(dummy);
                   setClickCountToStart(0);
                   undoButton.addActionListener(new ActionListener()
                        public void actionPerformed(ActionEvent e)
                             JOptionPane.showMessageDialog(null, "Do your Action Here");
              public Component getTableCellEditorComponent(JTable table,
                        Object value, boolean isSelected, int row, int column)
                   Component comp = super.getTableCellEditorComponent(table, value, isSelected, row, column);
                   if (column == 2)// The Action Column
                        Object checkValue = table.getValueAt(row, 1);
                        if ("Show".equals(String.valueOf(checkValue)))
                             return undoButton;
                   return comp;
              public Object getCellEditorValue()
                   return "";
          * @param args
         public static void main(String[] args)
              // TODO Auto-generated method stub
              new TableEdit2();
    }

  • Adding JButton in a JTable

    hi
    i know this has been discussed quite a number of times before, but i still couldn't figure it out..
    basically i just want to add a button to the 5th column of every row which has data in it.
    this is how i create my table (partially)
         private JTable clientTable;
         private DefaultTableModel clientTableModel;
    private JScrollPane scrollTable;
    clientTableModel = new DefaultTableModel(columnNames,100);
              clientTable = new JTable(clientTableModel);
              TableColumn tblColumn1 = clientTable.getColumn("Request ID");
              tblColumn1.setPreferredWidth(70);
              tblColumn1 = clientTable.getColumn("Given Name");
              tblColumn1.setPreferredWidth(300);
              tblColumn1 = clientTable.getColumn("Address");
              tblColumn1.setPreferredWidth(350);
              tblColumn1 = clientTable.getColumn("Card Serial");
              tblColumn1.setPreferredWidth(100);
              tblColumn1 = clientTable.getColumn("Print Count");
              tblColumn1.setPreferredWidth(70);
              tblColumn1 = clientTable.getColumn("Print?");
              tblColumn1.setPreferredWidth(40);
              clientTableModel.insertRow(0,data);
              //clientTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
              scrollTable = new JScrollPane(clientTable);and i call this function void listInfoInTable(){
              JButton cmdPrint[];
              Vector columnNames = new Vector();
              Object[] data={"","","","","",""};
              Statement stmt=null;
              ResultSet rs=null;
              PreparedStatement ps;
              String query=null;
              String retrieve=null;
              int i,j=0;
              TableColumnModel modelCol = clientTable.getColumnModel();
              try{
                   con = DriverManager.getConnection(url);
                   JOptionPane.showMessageDialog(null,"Please wait while the program retrieves data.");
                   query="select seqNo, givenName, address1, address2, address3, address4, cardSerNr, PIN1, PrintFlag from PendPINMail where seqNo<250;";
                 ps = con.prepareStatement(query);
                 rs=ps.executeQuery();
                 while (rs.next()){
                      data[0]= rs.getString("seqNo");
                      data[1]= rs.getString("givenName");
                      data[2]= rs.getString("address1");
                      data[3]= rs.getString("cardSerNr");
                      data[4]= rs.getString("PrintFlag");
    //                  modelCol.getColumn(5).setCellRenderer();
                      clientTableModel.insertRow(j,data);
                      j++;
              }catch (SQLException ex){
                   JOptionPane.showMessageDialog(null,"Database error: " + ex.getMessage());
         } to display data from database inside the table.
    How do I add JButton to the 5th column of each row? This documentation here http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#width says that i need to implement TableCellEditor to put JButton in the table. How do i really do it? I want the button to call another function which prints the data from the row (but this is another story).
    Any help is greatly appreciated. Thanks

    you would need CellRenderer i think that it is in
    javax.swing.table.*;
    see if you can get started with that.
    Davidthanks, i'll try and have a look at it
    Yes, that's definitely what you need to start with,
    but you also need a CellEditor to return the
    button as well, otherwise the button will not be
    clickable (the renderer just paints the cell for
    display, it doesn't allow you to interact with it).
    You could maintain a list of components for rendering
    each cell of the table so that your
    CellRenderer and CellEditor always
    return the same object (i.e. a JButton for any
    given cell).
    CB.thanks for the info.. could you point me to some examples? is sounds quite complicated for me....
    thanks again

  • Linking two JFrames using JButton-very,very urgent

    Hello,
    I have two JFrames I created using javax.swing.I am new to ActionListener.The two classes are the frames.Could someone please link them for me?Their code is as follows:-
    1.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class rr extends JFrame
    JButton rest=new JButton("Ground Chilli-Restaurant");
    JButton spa=new JButton("Carribean Spa");
    JButton gift =new JButton("Antiquo-Gift Shop");
    JButton pool=new JButton("Poolside Parlour");
    JButton recep=new JButton("Reception");
    JButton th=new JButton("Travel House");
    public rr()
    super( "Welcome");
    setSize(370,270);
    setBackground(Color.RED);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pane=new JPanel();
    pane.setBackground(Color.MAGENTA);
    GridLayout family=new GridLayout(3,3,10,10);
    pane.setLayout(family);
    pane.add(rest);
    pane.add(spa);
    pane.add(gift);
    pane.add(pool);
    pane.add(recep);
    pane.add(th);
    getContentPane().add(pane);
    setVisible(true);
    public static void main(String[] args)
    rr frame=new rr();
    and the others are:-
    2.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import javax.swing.event.MouseInputListener;
    public class spa extends JFrame
    String[] subs={"Welcome Cocktail","Body Massage","Steam Room","Children's Pool"};
    JList subList=new JList(subs);
    JRadioButton li=new JRadioButton("Executive service");
    JRadioButton oi=new JRadioButton("Economic Service");
    JButton ok=new JButton("Back");
    JButton can=new JButton("Continue");
    public spa()
    super("The Carribean Spa");
    setBackground(Color.RED);
    setSize(230,400);
    JPanel panel=new JPanel();
    panel.setBackground(Color.PINK);
    JLabel label1=new JLabel("Kindly select from our services :-");
    JLabel label2=new JLabel("Which type of service would you like?");
    panel.add(label1);
    subList.setVisibleRowCount(8);
    JScrollPane scroller=new JScrollPane(subList);
    panel.add(scroller);
    panel.add(label2);
    panel.add(li);
    panel.add(oi);
    panel.add(ok);
    panel.add(can);
    getContentPane().add(panel);
    setVisible(true);
    public static void main(String[] arguments)
    spa app=new spa();
    3.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class op extends JFrame
    JProgressBar current;
    JTextArea out;
    JButton ok=new JButton("OK");
    JButton con=new JButton("View our Credits Board.");
    JButton find;
    Thread runner;
    int num=0;
    public op()
    super("Calculatiing your Hotel Bill.Please wait.");
    setSize(222,500);
    setBackground(Color.PINK);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new FlowLayout());
    current=new JProgressBar(2,2000);
    current.setValue(0);
    getContentPane().add(ok);
    getContentPane().add(con);
    current.setStringPainted(true);
    getContentPane().add(current);
    public void iterate()
    while(num<2000)
    current.setValue(num);
    try{
    Thread.sleep(1200);
    }catch(InterruptedException e){}
    num+=95;
    public static void main(String[] arg)
    op frame=new op();
    frame.pack();
    frame.setVisible(true);
    frame.iterate();
    and so on......
    Please link these frames for me as I am not fully familiar with ActionListener () and WindowListener().
    Thanks,
    Bala

    If you have work you urgently need done then you should pay someone to do it.
    This is not a charity work forum. This is a forum for getting guidance and advice.
    So you are in the wrong place. Go elsewhere.

Maybe you are looking for

  • Codepage could not be determined

    Hi all, I am facing a little strange problem in my system, When i run HR Program my background scheduled jobs are getting cancelled due to an error called "Codepage could not be determined". Can any one just tell me their experience if the have encou

  • Some tips/tricks for  KinTwom

    First I want to say I got this phone yesterday, and I just love it. I found some things customizable on there and didn't know if anyone else had taken notice to them themselves. First, I know that the social networks were removed from the sliding opt

  • Monitor heap size

    How do we monitor Java heap size? Is there any way to check whether we are exceeding the heap size or not? Regards, N.S

  • FB50 screen layout to include posting key in line item

    hi, could anybody please guide me  : how to include posting key in FB50 screen Layout , basically the line item details another field : Psting Key needs to be implemented . Aldready tehre are about > 10 fields like Document , GL Account , Short text

  • Convert Joda YearMonthDate object to Calender object

    How to i convert a Joda YearMonthObject to Calendar object