Java JTable - Transparence with Nimbus/System L&F - java

I have this JTable:
List<MyTuple> l= new ArrayList<>();
l.add(new Element(1,"One"));
l.add(new Element(2,"Two"));
l.add(new Element(3,"Three"));
l.add(new Element(4,"Four"));
l.add(new Element(5,"Five"));
l.add(new Element(6,"Six"));
l.add(new Element(7,"Seven"));
JScrollPane pane = new JScrollPane();
pane.setOpaque(false);
pane.setBorder(new EmptyBorder(0, 0, 0, 0));
pane.setBackground(new Color(0,0,0,0));
pane.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
pane.setBounds(3, 101, 707, 297);
MyTableModel tm=new MyTableModel(l);
table = new MyTable(tm);
table.setBorder(new EmptyBorder(0, 0, 0, 0));
table.setOpaque(false);
//table.getTableHeader().setBackground(new Color(0,0,0,0));
table.getTableHeader().setOpaque(true);
table.getTableHeader().setBorder(new EmptyBorder(0,0,0,0));
table.setBackground(new Color(0,0,0,0));
table.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
table.setFillsViewportHeight(false);
table.getTableHeader().setFont(new Font("Segoe UI Semibold", Font.PLAIN, 13));
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
pane.setViewportView(table);
pane.getViewport().setOpaque(false);
table.setGridColor(Color.GRAY);
table.setShowGrid(true);
getContentPane().add(pane);
MyTuple, MyTable and MyTableModel classes don't set any custom rendering.
This is how my table looks like under System L&F (which I really like):
This is how my table looks like under Nimbus L&F (note the added big rectangle):
I want my table to look like the one with System L&F.
The problem is that if I set one L&F like I want, the other gets ugly, I want a sort of "compatibility", is there a way to do so?
Maybe using the UIManager.put() method?

You forgot to set the border around the viewport of the scrollpane. Possible solutions:
pane.setViewportBorder(null);
pane.setViewportBorder(BorderFactory.createEmptyBorder());
pane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
Note that pane.getViewport().setBorder(...) will not work, because a JViewport doesn't actually have a border, setViewportBorder just draws a border around the viewport. (Reference)
To make the table header transparent:
pane.setColumnHeader(new JViewport());
pane.getColumnHeader().setOpaque(false);
table.getTableHeader().setOpaque(false);

Related

In JFreeChart, how to position the Domain Axis Label next to the Legend?

Essentially, I have a chart that looks like this:
but I want the Domain axis label to show on the left, on the same line as the legend, on the right:
so I can better utilize the small space I got.
My chart source looks like this:
Color lightGray = new Color(200, 200, 200);
Color gray = new Color(150, 150, 150);
JFreeChart chart = createChart(createDataset(), yAxisTitle, xAxisTitle);
chart.setBackgroundPaint(Color.BLACK);
chart.getPlot().setBackgroundPaint(Color.BLACK);
chart.getPlot().setInsets(new RectangleInsets(0,2,2,2));
chart.getXYPlot().setBackgroundPaint(Color.BLACK);
chart.getXYPlot().getDomainAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getDomainAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getDomainAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getRangeAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setLabelPaint(lightGray);
chart.getTitle().setFont(new Font("Arial", Font.PLAIN, 12));
chart.getTitle().setBackgroundPaint(Color.BLACK);
chart.getTitle().setPaint(lightGray);
chart.getLegend().setItemFont(new Font("Arial", Font.PLAIN, 10));
chart.getLegend().setBackgroundPaint(Color.BLACK);
chart.getLegend().setItemPaint(gray);
chart.setTextAntiAlias(true);
chart.setAntiAlias(true);
chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);
panel.add(new ChartPanel(chart, false));
How can I accomplish this? Is there a way?
Thank you!
Found it.
To move the axis to the desired position, I used:
chart.getXYPlot().getDomainAxis().setLabelLocation(AxisLabelLocation.LOW_END);
Then to move the legend up in the same line, I had to adjust it's padding to use a negative value, which essentially moves it up, like so:
RectangleInsets padding = chart.getLegend().getPadding();
chart.getLegend().setPadding(new RectangleInsets(padding.getTop() - 20, padding.getRight(), padding.getBottom(), padding.getLeft()));
chart.getLegend().setBounds(chart.getLegend().getBounds());
This results in what I was looking for:
Hope this helps someone else out there!

Make TextField normal size using LayoutManagers

In the image above, this is what my GUI looks like, i have a problem fixing up the textfield as it takes up the most percentage of space in my frame, and i want to correct the error I have.
I know layout managers is a pain in the ass, but does anyone know how i can make the text field smaller so that my frame looks nicer.
I am using jpanels to hold most of my components
Here is the necessary part of my code
panel1 = new JPanel();
panel1.setLayout(new GridLayout());
frame.getContentPane().add(panel1, BorderLayout.NORTH);
label2 = new JLabel("Score: " + score);
label2.setFont(new Font("SimSun", Font.BOLD, 22));
label2.setLocation(0,0);
label2.setSize(117,37);
panel1.add(label2);
label3 = new JLabel("Lives: " + lives);
label3.setFont(new Font("SimSun", Font.BOLD, 22));
label3.setLocation(0,70);
label3.setSize(105,49);
panel1.add(label3);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
frame.getContentPane().add(panel2, BorderLayout.CENTER);
label1 = new JLabel(a +" + " +b);
label1.setFont(new Font("SimSun", Font.BOLD, 22));
label1.setHorizontalAlignment(SwingConstants.CENTER);
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
label1.setBorder(BorderFactory.createCompoundBorder(null,paddingBorder));
label1.setLocation(249,105);
label1.setSize(183,60);
panel2.add(label1, BorderLayout.NORTH);
box1 = new JTextField();
box1.setPreferredSize(new Dimension(50, 50));
box1.setLocation(249,176);
box1.setSize(96,25);
panel2.add(box1, BorderLayout.CENTER);
panel3 = new JPanel();
panel3.setLayout(new GridLayout());
frame.getContentPane().add(panel3, BorderLayout.SOUTH);
button1 = new JButton("Answer");
panel3.add(button1);
button1.addActionListener(this);
button1.setLocation(187,236);
button1.setSize(105,50);
button2 = new JButton("Reset");
panel3.add(button2);
button2.addActionListener(this);
button2.setVisible(false);
button2.setLocation(302,236);
button2.setSize(105,50);
The reason for this is you are adding your JTextField to BorderLayout.CENTER, which will stretch it to fill all the space available to this particular layout manager. Although I am not exactly sure how you want your GUI to look, perhaps a simple FlowLayout (the default layout) or a BoxLayout coupled with setAlignmentX(Component.CENTER_ALIGNMENT); on the JTextField will solve your problem.
You can find more information on layouts here: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Additionally, note that you shouldn't be using things like setLocation() as the point of the layout manager is to allow it to choose how components should be laid out on the screen, and as such the layout manager may ignore this as well as methods like setSize.

Jdatechooser text alignement to RIGHT

I need to align the text in Jdatechooser text field. It always aligns the text(the selected date) to LEFT but i need at RIGHT side.
I have tried this but its not working,
StartJCal.setAlignmentX(RIGHT_ALIGNMENT);
The setTextAlignment method is not available for Jdatechooser.
StartJCal = new JDateChooser();
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));
This is the piece of code am using.How can i align the text please help
StartJCal = new JDateChooser();
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)StartJCal.getComponent(1);
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));
try this. it will solve your problem
Similar but not passing through getComponent()
StartJCal = new JDateChooser();
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)StartJCal.getDateEditor();
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
StartJCal.setDateFormatString("yyyyMMdd");
StartJCal.setFont(new Font("Dialog", Font.PLAIN, 11));
StartJCal.setSize(new Dimension(105, 0));

Java: Can't apply Gridlayout to a Jscrollpane. Get Get java.lang.ClassCastException

I use a Gridlayout to place 4 elements in one Line. First I had a JPanel and everything worked fine. For the case that the number of lines get to big and I have to be able to scroll down, I changed it a bit. Now I have my JPanel with one JScrollPane added on it. I used the same code, now I just add the elements to the viewport of the Jscrollpane, but now I get this exception Get java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout: at javax.swing.JScrollPane.setLayout(Unknown Source) and I dont know exactly why. Why shouldnt be Gridlayout's be unknown for Jscrollpane?
Here is the code:
public objectDetails() {
setTitle("LLI_MC_Solver");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setLayout(new GridLayout());
setBounds(100, 100, 510, 401);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setVisible(true);
contentPane.setPreferredSize(new Dimension(500, 390));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0), 2));
scrollPane.setBounds(10, 10, 474, 342);
scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
scrollPane.setPreferredSize(new Dimension(465, 330));
contentPane.add(scrollPane);
JPanel view = (JPanel)scrollPane.getViewport().getView();
for(Values v : colDetails)
{
JLabel lblC = new JLabel();
lblC.setText(k);
view.add(lblC);
view.validate();
JLabel lblN = new JLabel();
lblN.setText(v.getName());
view.add(lblN);
view.validate();
JLabel lblT = new JLabel();
lblT.setText(v.getType());
view.add(lblT);
view.validate();
JTextField valueBox = new JTextField();
valueBox.setText(v.getValue());
view.add(valueBox);
view.validate();
}
}
I marked the line which causes the Problem according to the compiler. I dont understand why, with the JPanel the same code worked fine. The for-loop where the elements are added I posted for completion purposes, the issue must be somewhere in the setLayout()-Method.
Thanks in advance, appreciate every help.
scrollPane.setLayout(new GridLayout(0,4)); //Line which causes the error
You can't change the layout manager of a scrollpane.
A JScrollPane has its own custom layout manager because it needs to manage the horizontal/vertical scrollbars as well as the row/column headers etc..
Instead you add a panel that uses a GridLayout:
JPanel panel = new JPanel( new GridLayout(0, 4) );
panel.add( component1 );
panel.add( component2 );
panel.add( component3 );
panel.add( component4 );
JScrollPane = new JScrollPane( panel );

How to make a JFrame scrollable in Java?

I have this code in which I am trying to fit a scrollable Panel (JPanel) but I don't get it. Here is my code:
public class Sniffer_GUI extends JFrame {
Canvas c = new Canvas();
ConnectorPropertiesPanel props;
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
init();
getContentPane().add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
getContentPane().add(initConnectors(),
new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
getContentPane().add(props,
new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
setSize(800, 600);
setLocationRelativeTo(null);
}
Thanks in advance.
I edit to add a code that partially seems to work...
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane);
scrPane.setLayout(new ScrollPaneLayout());
init();
add(initConnectors());
setSize(800, 600);
setLocationRelativeTo(null);
}
But it isn't still scrollable, at least it makes its function inside a JScrollPane, is a good step.
Make a JPanel scrollable and use it as a container, something like this:
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane); // similar to getContentPane().add(scrPane);
// Now, you can add whatever you want to the container
To extend #Eng.Fouad answer:
public class Sniffer_GUI extends JFrame {
Canvas c = new Canvas();
ConnectorPropertiesPanel props;
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
getContentPane().add(scrPane);
container.setLayout(new GridBagLayout());
init();
container.add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
container.add(initConnectors(),
new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
container .add(props,
new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
setSize(800, 600);
setLocationRelativeTo(null);
}
}
Maybe this will help...
JFrame frame = new JFrame();
JPanel panel = new JPanel();
// add something to you panel...
// panel.add(...);
// add the panel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane(panel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Then, add the jScrollPane to your frame
frame.getContentPane().add(jScrollPane);
To make a component of a JFrame scrollable, wrap the component in a JScrollPane:
JScrollPane myJScrollPane = new JScrollPane(myJLabel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
and replace mentions of myJLabel with myJScrollPane. Worked for me.
Just chiming in, and feel free to correct me if I'm off base, but using a JScrollPane like that has an unintended consequence of requiring more frequent window resizing.
For example, I had a program where I set up a JFrame-wide scrollpane like that. I also had a JTextArea in a tab in the frame that was the same size as the content pane. This textArea was also in its own scrollpane (this was more of a screwing around project than anything else). When I loaded content from a file to deposit in the textArea, it triggered the scrollbars around the text area.
The result was that my, let's call it the innerScrollPane, was now larger than the JFrame because of the scrollbars, which hadn't been visible before. This then triggered what I'm now going to call the outerScrollPane, to display its scrollbars, which then covered up the inner scrollbars.
This was easily resolved by adding an extra window.pack() argument to the end of my file opening method, but I just wanted to throw this out there. The scrollbars can potentially cover up content in the window if you're not careful. But... well there are a million ways to prevent this problem, so it's not a huge deal. Just something to be aware of.
Try this:
JScrollPane sp = new JScrollPane();
this.add(sp).
sp.add( *GUI elements for your applications.*)
Something like that should work for you. Take a look at this as well:

Categories