JScrollBar inside a JTable

I've got a JScrollBar inside a JTable, but the L&F is completely different to other JScrollBars that are not in a table.
I'm using the WindowsLookAndFeel (through getSystemLookAndFeelClassName()) on Win2k. While progress bars displayed normally have a solid, light-blue bar, the ones in the tables are black and dashed.
Any clues why it's happening or how to get the in-table bars looking a bit more normal?

Still puzzled over this one, if anyone has any solutions or wild suggestions, they would be appreciated. ;)

Similar Messages

  • How do you set the font color for a specific entire row inside a JTable?

    How do you set the font color for a specific entire row inside a JTable?
    I want to change the font color for only a couple of rows inside a JTable.
    I've seen some ways to possibly do this with an individual cell.
    Clarification on changing the font color in an individual cell would be helpful too if
    there is no easy way to do this for a row.

    hai,
    Try out with this piece of code.Create your table and assign the renderer to each column in the table.
    CellColorRenderer m_CellColorRenderer = new CellColorRenderer();
    for(int i=0;i<your_JTable.getColumnCount();i++)
    your_JTable.getColumnModel().getColumn(i).setCellRenderer(m_CellColorRenderer);
    class CellColorRenderer extends JLabel implements TableCellRenderer
    CellColorRenderer()     
    setOpaque(true);     
    setHorizontalAlignment(LEFT);
    setVerticalAlignment(CENTER);
    setBackground(Color.white);
    setForeground(Color.black);
    protected void setValue(Object value)
         setText((value == null) ? "" : value.toString());
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row,int column)
         if(isSelected == true)
              setForeground(Color.red);
         else
              setForeground(Color.black);
         setValue(value);
         return this;
    regards,
    bala

  • JScrollBar with a JTable?

    Hi!
    How can I get a JTable to scroll sideways? I guess that I need to use the JScrollBar, but how do I get them to work together?
    How do I connect the JScrollBar to the JTable?
    /Tommy

    The best way to do it is like this:
    JTable table = new JTable(.....); // your table
    JPanel tablePanel = new JPanel(); // the main panel
    tablePanel.setLayout(new BorderLayout()); // set the layout
    JScrollPane tableScroller = new JScrollPane(); // Our scroller
    tableScroller.getViewPort().add(table); // Add the table to the scroller
    tablePanel.add(tableScroller, BorderLayout.CENTER); // finally add the scroller to the panel
    And there you go. If you need to update the table's contents, just
    tableScroller.remove(youtrable);
    tableScroller.getViewPort().add(thenewtable);

  • How to make textfield appear inside a jtable ????

    hi,
    is there any way that i can make mi textfield show up inside a jtable which i have created ??

    you may have to describe exactly what you are trying to do
    in this thread, you want a textfield for additional values entered
    http://forum.java.sun.com/thread.jspa?threadID=737376&messageID=4234595#4234595
    in this thread, you want the cells uneditable
    http://forum.java.sun.com/thread.jspa?threadID=740286&messageID=4246562#4246562
    and, by the look of the posted code, you want a textfield separate from the table
    here's a simple uneditable table.
    if I click on 'fred', what do you want to happen?
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    class Testing extends JFrame
      public Testing()
        setLocation(400,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        String colNames[] = {"Name", "Age"};
        Object[][] data = {{"joe","21"},{"fred","31"},{"mary","22"}};
        DefaultTableModel dtm = new DefaultTableModel(data,colNames);
        JTable table = new JTable(dtm){
          public boolean isCellEditable(int row,int column){
            return false;}};
        JScrollPane sp = new JScrollPane(table);
        sp.setPreferredSize(new Dimension(300,100));
        getContentPane().add(sp);
        pack();
      public static void main (String[] args){new Testing().setVisible(true);}
    }

  • How to select JLabel inside a JTable

    hi all,
    I have a Jtable which has own renderer so it can store components in it. I have added a Jlabel one of its cells but when I select that row the label stays upon the selected background color. here is a sample screenshot to decribe my problem clearly [http://www.imagecross.com/06/image-hosting-view-01.php?id=6570label.jpg]
    is there any way to select the label inside selected background color.
    - I have already tried to make the label not opaque but it didnt work -
    thanks...

    I have added bold parts of the code below. if you use other components you can make a type casting and do what you want.
    thanks again..
    class ComponentRenderer *extends JComponent*  implements TableCellRenderer
        public Component getTableCellRendererComponent(JTable table, Object value,
              boolean isSelected, boolean hasFocus, int row, int column) {
        *     JLabel l = (JLabel)value;*
        *     if(isSelected) {*
        *          l.setForeground(Color.BLUE);*
        *          l.setOpaque(true);*
        *          l.setBackground(new Color(184, 207, 229));*
        *     else {*
        *          l.setForeground(Color.BLACK);*
        *          l.setOpaque(true);*
        *          l.setBackground(Color.WHITE);*
            return l;
        /*public ComponentRenderer() {
    }

  • 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.

  • Setting the value of a JScrollBar inside a JScrollPane not working.

    I have a display area (JPanel) inside a JScrollPane. What i want to do is that every time the user closes my application a file is saved with some preferences, like position and size onscreen, and the values of the scrollbars. My problem is that when I try to set the values of the JScrollBars, they seem to be completely ignored.
    I made a small class to illustrate this:
    import java.awt.*;
    import javax.swing.*;
    public class ScrollExample extends JFrame
         private JPanel display;
         private JScrollPane displayScroll;
         public ScrollExample()
              display = new JPanel();
              display.setPreferredSize( new Dimension( 800, 600 ) );
              displayScroll = new JScrollPane( display );
              add( displayScroll );
              setSize( 400, 300 );
              setVisible( true );
              displayScroll.getHorizontalScrollBar().setValue( displayScroll.getHorizontalScrollBar().getMaximum() );
         public static void main( String args[] )
              new ScrollExample();
    }The behaviour of is as intended. However if we switch the two lines to be:
    displayScroll.getHorizontalScrollBar().setValue( displayScroll.getHorizontalScrollBar().getMaximum() );
    setVisible( true );and launch it, the scrollbar appears to have moved only a small amount. This leads me to believe that it has something to do with the main window being visible. In my actual application, this call to setValue is ignored, unlike in this sample one. Even placing it after the setVisible call, it does not do anything.
    When I tried to use:
         Runnable doScroll = new Runnable()
                 public void run()
                      taskDisplayScroll.getHorizontalScrollBar().setValue( 200 );
             SwingUtilities.invokeLater(doScroll);after the setVisible call, as mentioned on http://forum.java.sun.com/thread.jspa?threadID=723424, it rarely (maybe 3 out of 20 times) sets the scrollbar to the indicated value.
    My question is this: What does the setValue() call depend on to run properly, and how do I code it to work in my program?
    If you need any additional information, please ask.
    Note: In the sample class I provided, the scrollbar moves to its proper spot, I could not cause it to not move at all, as is the case in my application that seems to have the same calls.
    Thanks, Serge.
    Edited by: super_serge on Jun 26, 2008 8:23 AM

    Can you please explain to me how b) would be implemented.Read the Swing tutorial on Concurrency for the new approach in JDK1.6, or I still prefer to use SwingUtilities.invokeLater for somthing simple like this.

  • Changing Arrows and Color of a JScrollBar inside of a JScrollPane

    Hi. I can't believe this hasn't been asked and answered before but I can't find it in these forums if it has.
    I am trying to create a JScrollPane in a touchscreen based application - no mouse... The default scrollbar
    in my JScrollPane is too small for large fingers. I have successfully increased the size of the scrollbar
    to a usable size using: UIManager.put("scrollbar.width", 75).
    My next problem is two-fold.
    1) The arrows at top and bottom of the scrollbar are now heavily pixelated (i.e. jagged and blocky) and
    look like we zoomed in too much on a bitmap. How can I change these arrows to a graphic that is
    more related to the current size of my scrollbar?
    2) The color of the scrollbar is shades of 'baby blue'. My color scheme for my application is shades of
    beige or brown. This makes the scrollbar stand out like a sore thumb. How can I change the color
    of the scrollbar?
    Note: This is being coded in NetBeans 6.7.1 but I can't find any property in the visual editor that covers
    my problems.
    Also, I came across the UIManager.put("scrollbar.width", xx) from googling. Is there an official (complete)
    list of the properties available to the UIManager?
    Thanks

    To help out anyone who has been struggling with this as I have, here is the final solution we were able to use. We only needed to work with the vertical scroll bar, but it should be easy to work with now that a better part of the work is done. I do hope this is helpful.
         public ReportDisplayFrame()
              UIManager.put( "ScrollBar.width", 75);
              RDFScrollBarUI rdfScrlBarUI = new RDFScrollBarUI();
              rdfScrlBarUI.setButtonImageIcon( new ImageIcon( "C:/company/images/upArrow.jpg") , rdfScrlBarUI.NORTH);
              rdfScrlBarUI.setButtonImageIcon( new ImageIcon( "C:/company/images/downArrow.jpg") , rdfScrlBarUI.SOUTH);
              tempScrlBar = new JScrollBar();
              tempScrlBar.setBlockIncrement( 100);
              tempScrlBar.setUnitIncrement( 12);
              tempScrlBar.setUI( rdfScrlBarUI);
    // other constructor code //
              rdfScreenRedraw();
         private void rdfScreenRedraw()
              String methodName = "rdfScreenRedraw()";
              logger.log(Level.INFO, methodName + ": Revalidating: mainPanel Thread = ["+Thread.currentThread().getName()+"]");
              jPanelRpt.revalidate();
              jPanelRpt.repaint();
         class RDFScrollBarUI extends BasicScrollBarUI {
              private ImageIcon decImage = null;
              private ImageIcon incImage = null;
              public void setThumbColor(Color thumbColor) {
                   this.thumbColor = thumbColor;
              public void setThumbDarkShadowColor(Color thumbDarkShadowColor) {
                   this.thumbDarkShadowColor = thumbDarkShadowColor;
              public void setThumbHighlightColor(Color thumbHighlightColor) {
                   this.thumbHighlightColor = thumbHighlightColor;
              public void setThumbLightShadowColor(Color thumbLightShadowColor) {
                   this.thumbLightShadowColor = thumbLightShadowColor;
              public void setTrackColor(Color trackColor) {
                   this.trackColor = trackColor;
              public void setTrackHighlightColor(Color trackHighlightColor) {
                   this.trackHighlightColor = trackHighlightColor;
              public void setButtonImageIcon( ImageIcon theIcon, int orientation)
                   switch( orientation)
                        case NORTH:
                             decImage = theIcon;
                             break;
                        case SOUTH:
                             incImage = theIcon;
                             break;
                        case EAST:
              if (scrollbar.getComponentOrientation().isLeftToRight())
                                  decImage = theIcon;
                             else
                                  incImage = theIcon;
                             break;
                        case WEST:
              if (scrollbar.getComponentOrientation().isLeftToRight())
                                  incImage = theIcon;
                             else
                                  decImage = theIcon;
                             break;
              @Override
              protected JButton createDecreaseButton(int orientation) {
                   JButton button = null;
                   if ( decImage != null)
                        button = new JButton( decImage);
                   else
                        button = new BasicArrowButton(orientation);
                   button.setBackground( new Color( 180,180,130));
                   button.setForeground( new Color( 236,233,216));
                   return button;
              @Override
              protected JButton createIncreaseButton(int orientation) {
                   JButton button = null;
                   if ( incImage != null)
                        button = new JButton( incImage);
                   else
                        button = new BasicArrowButton(orientation);
                   button.setBackground( new Color( 180,180,130));
                   button.setForeground( new Color( 236,233,216));
                   return button;
         }

  • 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() );
    }

  • JTextFields inside a JPanel inside a JTable

    I have a JTable of one column, in which each row is a JPanel. The JPanel has JTextFields and JComboBoxs. The JPanel implements TableCellRenderer and TableCellEditor. It also declares an attribute as follows;
    DefaultCellEditor defCellEd = new DefaultCellEditor(new JComboBox);.
    All of the TableCellEditor methods are implemented in the same manner as the following method (except getCellEditorComponent(...)), i.e. they all pass the implementation onto the declared DefaultCellEditor;
    public boolean stopCellEditing()
         return defCellEd.stopCellEditing();
    getCellEditorComponent(...) is implemented to
    return (java.awt.Component)value;
    All seems to work fine, with one minor exception. My textfields are right justified, so for text that exceeds the default size of the textfield it is in, the first n characters are not visible initially. I would rather have it left justified with the trailing characters being not visible.
    If I do not set the cell editor for the panel (just setting the renderer) the text is left justified - problem there is, none of the fields are editable. Furthermore, testing of the JPanel standalone has the textfields left justified, also.
    Any help is appreciated, thanks in advance.

    I'm glad that helped.
    Basically, every text component has a caret, and the caret wants to be visible all the time, setText() moves the caret to the end, that's why setting the caret to null prevents this behavior.
    It appears that Sun doesn't intend to change this behavior so this fix is as far as I know the best we could do for now. See http://developer.java.sun.com/developer/bugParade/bugs/4227520.html for more information.

  • JCheckBox inside a JTable

    hai,
    i was trying to insert a JCheckBox for one column of the JTable. actually its getting inserted but its not visible. whenever the table is shown, only the value of the check box is visible on that cell, but not the checkbox. only if i click on that corresponding cell, as long as the mouse is pressed, i can view the chk box, but normally only the value is visible.
    can anyone please help me in viewing the check box. please do help i'm breaking my head without knowing the reason for this.
    Thanx u in advance.
    regards,
    Fazlina

    hai,
    thanx for ur quick reply. it does work fine. but now i'm not able to either chk it or unchk it.
    here is my code.can u please help me.
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.border.*;
    import java.awt.Dimension;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class TableExample {
    public TableExample() {
    JFrame frame = new JFrame("Table");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {System.exit(0);}});
    final String[] names={"FirstCol","SecondCol"};
    final Object[][] data={                                   {"Black",new Boolean(true)},                              {"White",new Boolean(true)}                         };
              // Create a model of the data.
         TableModel dataModel = new AbstractTableModel()
              public int getColumnCount() { return names.length; }
              public int getRowCount() { return data.length;}
    public Object getValueAt(int row, int col) {return data[row][col];}
              public Class getColumnClass(int col)
                   if (col == 1) return Boolean.class;
                   return super.getColumnClass(col);
    // Create the table
    JTable tableView = new JTable(dataModel);
    tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    // Finish setting up the table.
    JScrollPane scrollpane = new JScrollPane(tableView);
         scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
    scrollpane.setPreferredSize(new Dimension(430, 200));
    frame.getContentPane().add(scrollpane);
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
    new TableExample();
    i donno where i made the mistake can u please help me out as early as possible.
    thanx in advance.
    Regards,
    Fazlina

  • How to display all the files name in same folder inside a JTable?

    Currently i am doing an LAN P2P application (similar to KazAa) which is have a function to search files and list out files name after searched found. I am planning to list all the searched file names in JTable. I am having problem to list the files name on a table field. Can anybodu help : ) ? Please

    I don't understand what your problem is so I'll just refer you to the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to Use Tables.

  • How to retrieve value from ComboBox inside a Jtable

    Hi everyone, I've this problem: I used the code that I founded to this link:
    [http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#combobox|http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#combobox]
    So I inserted a comboBox in my Jtable but I don't retrieve comboBox's values.
    I tried to retrieve these values in this way:
    myTable1.getModel().getValueAt(rowIndex, columnIndex).getClass();but this code produce a NullPointerException.
    thank you for helping me!!!!

    but I don't retrieve comboBox's valuesCorrect, because the combo box and its data is not part of the TableModel. The TableModel only stores the item that was selected from the combo box when the cell was edited.
    If you are getting an exception it is because you haven't set any data in your table model for the given cell.
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

  • Multiple JButtons inside JTable cell - Dispatch mouse clicks

    Hi.
    I know this subject has already some discussions on the forum, but I can't seem to find anything that solves my problem.
    In my application, every JTable cell is a JPanel, that using a GridLayout, places vertically several JPanel's witch using an Overlay layout contains a JLabel and a JButton.
    As you can see, its a fairly complex cell...
    Unfortunately, because I use several JButtons in several locations inside a JTable cell, sometimes I can't get the mouse clicks to make through.
    This is my Table custom renderer:
    public class TimeTableRenderer implements TableCellRenderer {
         Border unselectedBorder = null;
         Border selectedBorder = null;
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                   boolean hasFocus, int row, int column) {
              if (value instanceof BlocoGrid) {
                   if (isSelected) {
                        if (selectedBorder == null)
                             selectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getSelectionBackground());
                        ((BlocoGrid) value).setBorder(selectedBorder);
                   } else {
                        if (unselectedBorder == null)
                             unselectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getBackground());
                        ((BlocoGrid) value).setBorder(unselectedBorder);
              return (Component) value;
    }and this is my custom editor (so clicks can get passed on):
    public class TimeTableEditor extends AbstractCellEditor implements TableCellEditor {
         private TimeTableRenderer render = null;
         public TimeTableEditor() {
              render = new TimeTableRenderer();
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
             if (value instanceof BlocoGrid) {
                  if (((BlocoGrid) value).barras.size() > 0) {
                       return render.getTableCellRendererComponent(table, value, isSelected, true, row, column);
             return null;
        public Object getCellEditorValue() {
            return null;
    }As you can see, both the renderer and editor return the same component that cames from the JTable model (all table values (components) only get instantiated once, so the same component is passed on to the renderer and editor).
    Is this the most correct way to get clicks to the cell component?
    Please check the screenshot below to see how the JButtons get placed inside the cell:
    http://img141.imageshack.us/my.php?image=calendarxo9.jpg
    If you need more info, please say so.
    Thanks.

    My mistake... It worked fine. The cell span code was malfunctioning. Thanks anyway.

  • JList inside JTable

    Hi,
    I have a custom JList Renderer/Editor inside a JTable. Both the renderer and editor seem to be working fine, but if the List contains more data than will fit in the table cell, you can't see the rest of the data. I have a separate class for the renderer and the editor (both of which extend JList) and I have tried using the setAutoscrolls method and creating a JScrollPane in the constructor, neither of which seems to work. How can I put a scrollable JList as a cell in a JTable?

    Hi jhooie,
    Will you please send me the working codes for creating the JList inside JTable. I am in urgent need of the help.
    My email id is [email protected]
    Thank you,
    satheesh kumar

Maybe you are looking for