Question

I am getting the free memory of the PC and then make it a slider sliced every 512MB of ram. It works in my PC but on my friends that has 8 GBs ram it looks like that: JSlider with 8GB: labels are unreadable

It works normally on my 4GB ram pc(Laptop).

Here is the code I am using.

    ram = new JSlider();
    ram.setMinimum(512);
    ram.setMaximum((int)Runtime.getRuntime().freeMemory()/1024);
    ram.setPaintTicks(true);
    ram.setMajorTickSpacing(512);
    ram.setValue(512);
    ram.setPreferredSize(new Dimension(750, 50));
    ram.setVisible(true);
    Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
    int mbs = 1024;
    while(mbs <= Runtime.getRuntime().freeMemory()/1024){       
        labelTable.put(mbs, new JLabel(Integer.toString(mbs)));
        mbs += 1024;
    }
    ram.setLabelTable(labelTable);
    ram.setPaintLabels(true);
    frame.add(ram)
Was it helpful?

Solution

Most probably, there are too many labels for 8 GB for the same amount of space. If you have 8 GB, then there will be twice as much labels as for 4 GB in the same visible space. You have to change the number of labels depending on the amount of memory.

You can use a fixed number of labels, 10 for example, and calculate the scale dynamically depending on how much memory is available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top